mode4.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * SMS renderer
  3. * (C) notaz, 2009-2010
  4. * (C) kub, 2021
  5. *
  6. * currently supports VDP mode 4 (SMS and GG) and mode 2
  7. *
  8. * This work is licensed under the terms of MAME license.
  9. * See COPYING file in the top-level directory.
  10. */
  11. /*
  12. * TODO:
  13. * - other TMS9918 modes?
  14. */
  15. #include "pico_int.h"
  16. #include <platform/common/upscale.h>
  17. static void (*FinalizeLineSMS)(int line);
  18. static int skip_next_line;
  19. static int screen_offset, line_offset;
  20. /* Mode 4 */
  21. /*========*/
  22. static void TileBGM4(u16 sx, int pal)
  23. {
  24. u32 *pd = (u32 *)(Pico.est.HighCol + sx);
  25. pd[0] = pd[1] = pal ? 0x10101010 : 0;
  26. }
  27. // 8 pixels are arranged to have 1 bit in each byte of a 32 bit word. To pull
  28. // the 4 bitplanes together multiply with each bit distance (multiples of 1<<7)
  29. #define PLANAR_PIXELL(x,p) \
  30. t = (pack>>(7-p)) & 0x01010101; \
  31. t = (t*0x10204080) >> 28; \
  32. pd[x] = pal|t;
  33. static void TileNormLowM4(u16 sx, unsigned int pack, int pal)
  34. {
  35. u8 *pd = Pico.est.HighCol + sx;
  36. u32 t;
  37. PLANAR_PIXELL(0, 0)
  38. PLANAR_PIXELL(1, 1)
  39. PLANAR_PIXELL(2, 2)
  40. PLANAR_PIXELL(3, 3)
  41. PLANAR_PIXELL(4, 4)
  42. PLANAR_PIXELL(5, 5)
  43. PLANAR_PIXELL(6, 6)
  44. PLANAR_PIXELL(7, 7)
  45. }
  46. static void TileFlipLowM4(u16 sx, unsigned int pack, int pal)
  47. {
  48. u8 *pd = Pico.est.HighCol + sx;
  49. u32 t;
  50. PLANAR_PIXELL(0, 7)
  51. PLANAR_PIXELL(1, 6)
  52. PLANAR_PIXELL(2, 5)
  53. PLANAR_PIXELL(3, 4)
  54. PLANAR_PIXELL(4, 3)
  55. PLANAR_PIXELL(5, 2)
  56. PLANAR_PIXELL(6, 1)
  57. PLANAR_PIXELL(7, 0)
  58. }
  59. #define PLANAR_PIXEL(x,p) \
  60. t = (pack>>(7-p)) & 0x01010101; \
  61. if (t) { \
  62. t = (t*0x10204080) >> 28; \
  63. pd[x] = pal|t; \
  64. }
  65. static void TileNormM4(u16 sx, unsigned int pack, int pal)
  66. {
  67. u8 *pd = Pico.est.HighCol + sx;
  68. u32 t;
  69. PLANAR_PIXEL(0, 0)
  70. PLANAR_PIXEL(1, 1)
  71. PLANAR_PIXEL(2, 2)
  72. PLANAR_PIXEL(3, 3)
  73. PLANAR_PIXEL(4, 4)
  74. PLANAR_PIXEL(5, 5)
  75. PLANAR_PIXEL(6, 6)
  76. PLANAR_PIXEL(7, 7)
  77. }
  78. static void TileFlipM4(u16 sx, unsigned int pack, int pal)
  79. {
  80. u8 *pd = Pico.est.HighCol + sx;
  81. u32 t;
  82. PLANAR_PIXEL(0, 7)
  83. PLANAR_PIXEL(1, 6)
  84. PLANAR_PIXEL(2, 5)
  85. PLANAR_PIXEL(3, 4)
  86. PLANAR_PIXEL(4, 3)
  87. PLANAR_PIXEL(5, 2)
  88. PLANAR_PIXEL(6, 1)
  89. PLANAR_PIXEL(7, 0)
  90. }
  91. static void TileDoubleM4(int sx, unsigned int pack, int pal)
  92. {
  93. u8 *pd = Pico.est.HighCol + sx;
  94. u32 t;
  95. PLANAR_PIXEL(0, 0)
  96. PLANAR_PIXEL(1, 0)
  97. PLANAR_PIXEL(2, 1)
  98. PLANAR_PIXEL(3, 1)
  99. PLANAR_PIXEL(4, 2)
  100. PLANAR_PIXEL(5, 2)
  101. PLANAR_PIXEL(6, 3)
  102. PLANAR_PIXEL(7, 3)
  103. PLANAR_PIXEL(8, 4)
  104. PLANAR_PIXEL(9, 4)
  105. PLANAR_PIXEL(10, 5)
  106. PLANAR_PIXEL(11, 5)
  107. PLANAR_PIXEL(12, 6)
  108. PLANAR_PIXEL(13, 6)
  109. PLANAR_PIXEL(14, 7)
  110. PLANAR_PIXEL(15, 7)
  111. }
  112. static void DrawSpritesM4(int scanline)
  113. {
  114. struct PicoVideo *pv = &Pico.video;
  115. unsigned int sprites_addr[8];
  116. unsigned int sprites_x[8];
  117. unsigned int pack;
  118. u8 *sat;
  119. int xoff = 8; // relative to HighCol, which is (screen - 8)
  120. int sprite_base, addr_mask;
  121. int zoomed = pv->reg[1] & 0x1; // zoomed sprites, e.g. Earthworm Jim
  122. int i, s, h;
  123. if (pv->reg[0] & 8)
  124. xoff = 0;
  125. xoff += line_offset;
  126. if ((Pico.m.hardware & 0x3) == 0x3)
  127. xoff -= 48; // GG LCD, adjust to center 160 px
  128. sat = (u8 *)PicoMem.vram + ((pv->reg[5] & 0x7e) << 7);
  129. if (pv->reg[1] & 2) {
  130. addr_mask = 0xfe; h = 16;
  131. } else {
  132. addr_mask = 0xff; h = 8;
  133. }
  134. if (zoomed) h *= 2;
  135. sprite_base = (pv->reg[6] & 4) << (13-2-1);
  136. for (i = s = 0; i < 64; i++)
  137. {
  138. int y;
  139. y = (sat[MEM_LE2(i)] + 1) & 0xff;
  140. if (y == 0xd1 && !((pv->reg[0] & 6) == 6 && (pv->reg[1] & 0x18)))
  141. break;
  142. if (y + h <= scanline || scanline < y)
  143. continue; // not on this line
  144. if (s >= 8) {
  145. pv->status |= SR_SOVR;
  146. break;
  147. }
  148. if (xoff + sat[MEM_LE2(0x80 + i*2)] >= 0) {
  149. sprites_x[s] = xoff + sat[MEM_LE2(0x80 + i*2)];
  150. sprites_addr[s] = sprite_base + ((sat[MEM_LE2(0x80 + i*2 + 1)] & addr_mask) << (5-1)) +
  151. ((scanline - y) >> zoomed << (2-1));
  152. s++;
  153. }
  154. }
  155. // really half-assed but better than nothing
  156. if (s > 1)
  157. pv->status |= SR_C;
  158. // now draw all sprites backwards
  159. for (--s; s >= 0; s--) {
  160. pack = CPU_LE2(*(u32 *)(PicoMem.vram + sprites_addr[s]));
  161. if (zoomed) TileDoubleM4(sprites_x[s], pack, 0x10);
  162. else TileNormM4(sprites_x[s], pack, 0x10);
  163. }
  164. }
  165. // cells_dx, tilex_ty merged to reduce register pressure
  166. static void DrawStripLowM4(const u16 *nametab, int cells_dx, int tilex_ty)
  167. {
  168. int oldcode = -1;
  169. int addr = 0, pal = 0;
  170. // Draw tiles across screen:
  171. for (; cells_dx > 0; cells_dx += 8, tilex_ty++, cells_dx -= 0x10000)
  172. {
  173. unsigned int pack;
  174. unsigned code;
  175. code = nametab[tilex_ty& 0x1f];
  176. if (code != oldcode) {
  177. oldcode = code;
  178. // Get tile address/2:
  179. addr = (code & 0x1ff) << 4;
  180. addr += tilex_ty>> 16;
  181. if (code & 0x0400)
  182. addr ^= 0xe; // Y-flip
  183. pal = (code>>7) & 0x10;
  184. }
  185. pack = CPU_LE2(*(u32 *)(PicoMem.vram + addr)); /* Get 4 bitplanes / 8 pixels */
  186. if (pack == 0) TileBGM4(cells_dx, pal);
  187. else if (code & 0x0200) TileFlipLowM4(cells_dx, pack, pal);
  188. else TileNormLowM4(cells_dx, pack, pal);
  189. }
  190. }
  191. static void DrawStripHighM4(const u16 *nametab, int cells_dx, int tilex_ty)
  192. {
  193. int oldcode = -1, blank = -1; // The tile we know is blank
  194. int addr = 0, pal = 0;
  195. // Draw tiles across screen:
  196. for (; cells_dx > 0; cells_dx += 8, tilex_ty++, cells_dx -= 0x10000)
  197. {
  198. unsigned int pack;
  199. unsigned code;
  200. code = nametab[tilex_ty& 0x1f];
  201. if (code == blank)
  202. continue;
  203. if (!(code & 0x1000)) // priority low?
  204. continue;
  205. if (code != oldcode) {
  206. oldcode = code;
  207. // Get tile address/2:
  208. addr = (code & 0x1ff) << 4;
  209. addr += tilex_ty>> 16;
  210. if (code & 0x0400)
  211. addr ^= 0xe; // Y-flip
  212. pal = (code>>7) & 0x10;
  213. }
  214. pack = CPU_LE2(*(u32 *)(PicoMem.vram + addr)); /* Get 4 bitplanes / 8 pixels */
  215. if (pack == 0) {
  216. blank = code;
  217. continue;
  218. }
  219. if (code & 0x0200) TileFlipM4(cells_dx, pack, pal);
  220. else TileNormM4(cells_dx, pack, pal);
  221. }
  222. }
  223. static void DrawDisplayM4(int scanline)
  224. {
  225. struct PicoVideo *pv = &Pico.video;
  226. u16 *nametab, *nametab2;
  227. int line, tilex, dx, ty, cells;
  228. int cellskip = 0; // XXX
  229. int maxcells = 32;
  230. // Find the line in the name table
  231. line = pv->reg[9] + scanline; // vscroll + scanline
  232. // Find name table:
  233. nametab = PicoMem.vram;
  234. if ((pv->reg[0] & 6) == 6 && (pv->reg[1] & 0x18)) {
  235. // 224/240 line mode
  236. line &= 0xff;
  237. nametab += ((pv->reg[2] & 0x0c) << (10-1)) + (0x700 >> 1);
  238. } else {
  239. while (line >= 224) line -= 224;
  240. nametab += (pv->reg[2] & 0x0e) << (10-1);
  241. // old SMS only, masks line:7 with reg[2]:0 for address calculation
  242. //if ((pv->reg[2] & 0x01) == 0) line &= 0x7f;
  243. }
  244. nametab2 = nametab + ((scanline>>3) << (6-1));
  245. nametab = nametab + ((line>>3) << (6-1));
  246. dx = pv->reg[8]; // hscroll
  247. if (scanline < 16 && (pv->reg[0] & 0x40))
  248. dx = 0; // hscroll disabled for top 2 rows (e.g. Fantasy Zone II)
  249. tilex = ((-dx >> 3) + cellskip) & 0x1f;
  250. ty = (line & 7) << 1; // Y-Offset into tile
  251. cells = maxcells - cellskip;
  252. dx = ((dx - 1) & 7) + 1;
  253. if (dx != 8)
  254. cells++; // have hscroll, need to draw 1 cell more
  255. dx += cellskip << 3;
  256. dx += line_offset;
  257. // low priority tiles
  258. if (!(pv->debug_p & PVD_KILL_B)) {
  259. if ((Pico.m.hardware & 0x3) == 0x3) {
  260. // on GG render only the center 160 px
  261. DrawStripLowM4(nametab , dx | ((cells-12)<< 16),(tilex+6) | (ty << 16));
  262. } else if (pv->reg[0] & 0x80) {
  263. // vscroll disabled for rightmost 8 columns (e.g. Gauntlet)
  264. int dx2 = dx + (cells-8)*8, tilex2 = tilex + (cells-8), ty2 = scanline&7;
  265. DrawStripLowM4(nametab, dx | ((cells-8) << 16), tilex | (ty << 16));
  266. DrawStripLowM4(nametab2, dx2 | (8 << 16), tilex2 | (ty2 << 17));
  267. } else
  268. DrawStripLowM4(nametab , dx | ( cells << 16), tilex | (ty << 16));
  269. }
  270. // sprites
  271. if (!(pv->debug_p & PVD_KILL_S_LO))
  272. DrawSpritesM4(scanline);
  273. // high priority tiles (use virtual layer switch just for fun)
  274. if (!(pv->debug_p & PVD_KILL_A)) {
  275. if ((Pico.m.hardware & 0x3) == 0x3) {
  276. DrawStripHighM4(nametab , dx | ((cells-12)<< 16),(tilex+6) | (ty << 16));
  277. } else if (pv->reg[0] & 0x80) {
  278. int dx2 = dx + (cells-8)*8, tilex2 = tilex + (cells-8), ty2 = scanline&7;
  279. DrawStripHighM4(nametab, dx | ((cells-8) << 16), tilex | (ty << 16));
  280. DrawStripHighM4(nametab2, dx2 | (8 << 16), tilex2 | (ty2 << 17));
  281. } else
  282. DrawStripHighM4(nametab , dx | ( cells << 16), tilex | (ty << 16));
  283. }
  284. if ((pv->reg[0] & 0x20) && (Pico.m.hardware & 0x3) != 3) {
  285. // first column masked with background, caculate offset to start of line
  286. dx = (dx&~0x1f) / 4;
  287. ty = 0xe0e0e0e0; // really (pv->reg[7]&0x3f) * 0x01010101, but the looks...
  288. ((u32 *)Pico.est.HighCol)[dx+2] = ((u32 *)Pico.est.HighCol)[dx+3] = ty;
  289. }
  290. }
  291. /* Mode 2 */
  292. /*========*/
  293. /* Background */
  294. #define TMS_PIXELBG(x,p) \
  295. t = (pack>>(7-p)) & 0x01; \
  296. t = (pal >> (t << 2)) & 0x0f; \
  297. pd[x] = t;
  298. static void TileNormBgM2(u16 sx, unsigned int pack, int pal)
  299. {
  300. u8 *pd = Pico.est.HighCol + sx;
  301. unsigned int t;
  302. TMS_PIXELBG(0, 0)
  303. TMS_PIXELBG(1, 1)
  304. TMS_PIXELBG(2, 2)
  305. TMS_PIXELBG(3, 3)
  306. TMS_PIXELBG(4, 4)
  307. TMS_PIXELBG(5, 5)
  308. TMS_PIXELBG(6, 6)
  309. TMS_PIXELBG(7, 7)
  310. }
  311. /* Sprites */
  312. #define TMS_PIXELSP(x,p) \
  313. t = (pack>>(7-p)) & 0x01; \
  314. if (t) \
  315. pd[x] = pal;
  316. static void TileNormSprM2(u16 sx, unsigned int pack, int pal)
  317. {
  318. u8 *pd = Pico.est.HighCol + sx;
  319. unsigned int t;
  320. TMS_PIXELSP(0, 0)
  321. TMS_PIXELSP(1, 1)
  322. TMS_PIXELSP(2, 2)
  323. TMS_PIXELSP(3, 3)
  324. TMS_PIXELSP(4, 4)
  325. TMS_PIXELSP(5, 5)
  326. TMS_PIXELSP(6, 6)
  327. TMS_PIXELSP(7, 7)
  328. }
  329. static void TileDoubleSprM2(u16 sx, unsigned int pack, int pal)
  330. {
  331. u8 *pd = Pico.est.HighCol + sx;
  332. unsigned int t;
  333. TMS_PIXELSP(0, 0)
  334. TMS_PIXELSP(1, 0)
  335. TMS_PIXELSP(2, 1)
  336. TMS_PIXELSP(3, 1)
  337. TMS_PIXELSP(4, 2)
  338. TMS_PIXELSP(5, 2)
  339. TMS_PIXELSP(6, 3)
  340. TMS_PIXELSP(7, 3)
  341. TMS_PIXELSP(8, 4)
  342. TMS_PIXELSP(9, 4)
  343. TMS_PIXELSP(10, 5)
  344. TMS_PIXELSP(11, 5)
  345. TMS_PIXELSP(12, 6)
  346. TMS_PIXELSP(13, 6)
  347. TMS_PIXELSP(14, 7)
  348. TMS_PIXELSP(15, 7)
  349. }
  350. /* Draw sprites into a scanline, max 4 */
  351. static void DrawSpritesM2(int scanline)
  352. {
  353. struct PicoVideo *pv = &Pico.video;
  354. unsigned int sprites_addr[4];
  355. unsigned int sprites_x[4];
  356. unsigned int pack;
  357. u8 *sat;
  358. int xoff = 8; // relative to HighCol, which is (screen - 8)
  359. int sprite_base, addr_mask;
  360. int zoomed = pv->reg[1] & 0x1; // zoomed sprites
  361. int i, s, h;
  362. xoff += line_offset;
  363. sat = (u8 *)PicoMem.vramb + ((pv->reg[5] & 0x7e) << 7);
  364. if (pv->reg[1] & 2) {
  365. addr_mask = 0xfc; h = 16;
  366. } else {
  367. addr_mask = 0xff; h = 8;
  368. }
  369. if (zoomed) h *= 2;
  370. sprite_base = (pv->reg[6] & 0x7) << 11;
  371. /* find sprites on this scanline */
  372. for (i = s = 0; i < 32; i++)
  373. {
  374. int y;
  375. y = (sat[MEM_LE2(4*i)] + 1) & 0xff;
  376. if (y == 0xd1)
  377. break;
  378. if (y > 0xe0)
  379. y -= 256;
  380. if (y + h <= scanline || scanline < y)
  381. continue; // not on this line
  382. if (s >= 4) {
  383. pv->status |= SR_SOVR | i;
  384. break;
  385. }
  386. sprites_x[s] = 4*i;
  387. sprites_addr[s] = sprite_base + ((sat[MEM_LE2(4*i + 2)] & addr_mask) << 3) +
  388. ((scanline - y) >> zoomed);
  389. s++;
  390. }
  391. // really half-assed but better than nothing
  392. if (s > 1)
  393. pv->status |= SR_C;
  394. // now draw all sprites backwards
  395. for (--s; s >= 0; s--) {
  396. int x, c, w = (zoomed ? 16: 8);
  397. i = sprites_x[s];
  398. x = sat[MEM_LE2(i+1)] + xoff;
  399. if (sat[MEM_LE2(i+3)] & 0x80)
  400. x -= 32;
  401. c = sat[MEM_LE2(i+3)] & 0x0f;
  402. if (x > 0) {
  403. pack = PicoMem.vramb[MEM_LE2(sprites_addr[s])];
  404. if (zoomed) TileDoubleSprM2(x, pack, c);
  405. else TileNormSprM2(x, pack, c);
  406. }
  407. if((pv->reg[1] & 0x2) && (x+=w) > 0) {
  408. pack = PicoMem.vramb[MEM_LE2(sprites_addr[s]+0x10)];
  409. if (zoomed) TileDoubleSprM2(x, pack, c);
  410. else TileNormSprM2(x, pack, c);
  411. }
  412. }
  413. }
  414. /* Draw the background into a scanline; cells, dx, tilex, ty merged to reduce registers */
  415. static void DrawStripM2(const u8 *nametab, const u8 *coltab, const u8 *pattab, int cells_dx, int tilex_ty)
  416. {
  417. // Draw tiles across screen:
  418. for (; cells_dx > 0; cells_dx += 8, tilex_ty++, cells_dx -= 0x10000)
  419. {
  420. unsigned int pack, pal;
  421. unsigned code;
  422. code = nametab[tilex_ty& 0x1f] << 3;
  423. pal = coltab[code];
  424. pack = pattab[code];
  425. TileNormBgM2(cells_dx, pack, pal);
  426. }
  427. }
  428. /* Draw a scanline */
  429. static void DrawDisplayM2(int scanline)
  430. {
  431. struct PicoVideo *pv = &Pico.video;
  432. u8 *nametab, *coltab, *pattab;
  433. int tilex, dx, cells;
  434. int cellskip = 0; // XXX
  435. int maxcells = 32;
  436. // name, color, pattern table:
  437. nametab = PicoMem.vramb + ((pv->reg[2]<<10) & 0x3c00);
  438. coltab = PicoMem.vramb + ((pv->reg[3]<< 6) & 0x2000);
  439. pattab = PicoMem.vramb + ((pv->reg[4]<<11) & 0x2000);
  440. nametab += ((scanline>>3) << 5);
  441. coltab += ((scanline>>6) <<11) + (scanline & 0x7);
  442. pattab += ((scanline>>6) <<11) + (scanline & 0x7);
  443. tilex = cellskip & 0x1f;
  444. cells = maxcells - cellskip;
  445. dx = (cellskip << 3) + line_offset + 8;
  446. // tiles
  447. if (!(pv->debug_p & PVD_KILL_B))
  448. DrawStripM2(nametab, coltab, pattab, dx | (cells << 16), tilex | (scanline << 16));
  449. // sprites
  450. if (!(pv->debug_p & PVD_KILL_S_LO))
  451. DrawSpritesM2(scanline);
  452. }
  453. /* Common/global */
  454. /*===============*/
  455. static void FinalizeLineRGB555SMS(int line);
  456. void PicoFrameStartSMS(void)
  457. {
  458. int lines = 192, columns = 256, loffs, coffs;
  459. skip_next_line = 0;
  460. loffs = screen_offset = 24; // 192 lines is really 224 with top/bottom bars
  461. Pico.est.rendstatus = PDRAW_32_COLS;
  462. // Copy LCD enable flag for easier handling
  463. Pico.m.hardware &= ~0x2;
  464. if (PicoIn.opt & POPT_EN_GG_LCD)
  465. Pico.m.hardware |= 0x2;
  466. if ((Pico.m.hardware & 0x3) == 0x3) {
  467. // GG LCD always has 160x144 regardless of settings
  468. screen_offset = 24; // nonetheless the vdp timing has 224 lines
  469. loffs = 48;
  470. lines = 144;
  471. columns = 160;
  472. } else switch ((Pico.video.reg[0]&0x06) | (Pico.video.reg[1]&0x18)) {
  473. // SMS2 only 224/240 line modes, e.g. Micro Machines
  474. case 0x06|0x08:
  475. loffs = screen_offset = 0;
  476. lines = 240;
  477. break;
  478. case 0x06|0x10:
  479. loffs = screen_offset = 8;
  480. lines = 224;
  481. break;
  482. }
  483. if (PicoIn.opt & POPT_EN_SOFTSCALE) {
  484. coffs = 0;
  485. columns = 320;
  486. } else
  487. coffs = PicoIn.opt & POPT_DIS_32C_BORDER ? 0:(320-columns)/2;
  488. line_offset = (FinalizeLineSMS == NULL ? coffs : 0);
  489. if (FinalizeLineSMS == FinalizeLineRGB555SMS)
  490. line_offset = 0 /* done in FinalizeLine */;
  491. if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
  492. emu_video_mode_change(loffs, lines, coffs, columns);
  493. rendstatus_old = Pico.est.rendstatus;
  494. rendlines = lines;
  495. }
  496. Pico.est.HighCol = HighColBase + screen_offset * HighColIncrement;
  497. Pico.est.DrawLineDest = (char *)DrawLineDestBase + screen_offset * DrawLineDestIncrement;
  498. }
  499. void PicoLineSMS(int line)
  500. {
  501. int skip = skip_next_line;
  502. // GG LCD, render only visible part of screen
  503. if ((Pico.m.hardware & 0x3) == 0x3 && (line < 24 || line >= 24+144))
  504. goto norender;
  505. if (PicoScanBegin != NULL && skip == 0)
  506. skip = PicoScanBegin(line + screen_offset);
  507. if (skip) {
  508. skip_next_line = skip - 1;
  509. return;
  510. }
  511. // Draw screen:
  512. BackFill(Pico.video.reg[7] & 0x0f, 0, &Pico.est);
  513. if (Pico.video.reg[1] & 0x40) {
  514. if (Pico.video.reg[0] & 0x04) DrawDisplayM4(line);
  515. else DrawDisplayM2(line);
  516. }
  517. if (FinalizeLineSMS != NULL)
  518. FinalizeLineSMS(line);
  519. if (PicoScanEnd != NULL)
  520. skip_next_line = PicoScanEnd(line + screen_offset);
  521. norender:
  522. Pico.est.HighCol += HighColIncrement;
  523. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  524. }
  525. /* Fixed palette for TMS9918 modes */
  526. static u16 tmspal[32] = {
  527. 0x0000, 0x0000, 0x00a0, 0x00f0, 0x0500, 0x0f00, 0x0005, 0x0ff0,
  528. 0x000a, 0x000f, 0x0055, 0x00ff, 0x0050, 0x0f0f, 0x0555, 0x0fff,
  529. };
  530. void PicoDoHighPal555SMS(void)
  531. {
  532. u32 *spal=(void *)PicoMem.cram;
  533. u32 *dpal=(void *)Pico.est.HighPal;
  534. unsigned int t;
  535. int i;
  536. Pico.m.dirtyPal = 0;
  537. if (!(Pico.video.reg[0] & 0x4))
  538. spal = (u32 *)tmspal;
  539. /* SMS 6 bit cram data was already converted to MD/GG format by vdp write,
  540. * hence GG/SMS/TMS can all be handled the same here */
  541. for (i = 0x20/2; i > 0; i--, spal++, dpal++) {
  542. t = *spal;
  543. #if defined(USE_BGR555)
  544. t = ((t & 0x000f000f)<< 1) | ((t & 0x00f000f0)<<2) | ((t & 0x0f000f00)<<3);
  545. t |= (t >> 4) & 0x04210421;
  546. #elif defined(USE_BGR565)
  547. t = ((t & 0x000f000f)<< 1) | ((t & 0x00f000f0)<<3) | ((t & 0x0f000f00)<<4);
  548. t |= (t >> 4) & 0x08610861;
  549. #else
  550. t = ((t & 0x000f000f)<<12) | ((t & 0x00f000f0)<<3) | ((t & 0x0f000f00)>>7);
  551. t |= (t >> 4) & 0x08610861;
  552. #endif
  553. *dpal = t;
  554. }
  555. Pico.est.HighPal[0xe0] = 0;
  556. }
  557. static void FinalizeLineRGB555SMS(int line)
  558. {
  559. if (Pico.m.dirtyPal)
  560. PicoDoHighPal555SMS();
  561. // standard FinalizeLine can finish it for us,
  562. // with features like scaling and such
  563. FinalizeLine555(0, line, &Pico.est);
  564. }
  565. static void FinalizeLine8bitSMS(int line)
  566. {
  567. FinalizeLine8bit(0, line, &Pico.est);
  568. }
  569. void PicoDrawSetOutputSMS(pdso_t which)
  570. {
  571. switch (which)
  572. {
  573. case PDF_8BIT: FinalizeLineSMS = FinalizeLine8bitSMS; break;
  574. case PDF_RGB555: FinalizeLineSMS = FinalizeLineRGB555SMS; break;
  575. // there's no fast renderer yet, just treat it like PDF_8BIT
  576. default: FinalizeLineSMS = FinalizeLine8bitSMS;
  577. PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328); break;
  578. }
  579. rendstatus_old = -1;
  580. }
  581. // vim:shiftwidth=2:ts=2:expandtab