mode4.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 & 0x1000) // priority high?
  177. continue;
  178. if (code != oldcode) {
  179. oldcode = code;
  180. // Get tile address/2:
  181. addr = (code & 0x1ff) << 4;
  182. addr += tilex_ty>> 16;
  183. if (code & 0x0400)
  184. addr ^= 0xe; // Y-flip
  185. pal = (code>>7) & 0x10;
  186. }
  187. pack = CPU_LE2(*(u32 *)(PicoMem.vram + addr)); /* Get 4 bitplanes / 8 pixels */
  188. if (pack == 0) TileBGM4(cells_dx, pal);
  189. else if (code & 0x0200) TileFlipLowM4(cells_dx, pack, pal);
  190. else TileNormLowM4(cells_dx, pack, pal);
  191. }
  192. }
  193. static void DrawStripHighM4(const u16 *nametab, int cells_dx, int tilex_ty)
  194. {
  195. int oldcode = -1, blank = -1; // The tile we know is blank
  196. int addr = 0, pal = 0;
  197. // Draw tiles across screen:
  198. for (; cells_dx > 0; cells_dx += 8, tilex_ty++, cells_dx -= 0x10000)
  199. {
  200. unsigned int pack;
  201. unsigned code;
  202. code = nametab[tilex_ty& 0x1f];
  203. if (code == blank)
  204. continue;
  205. if (!(code & 0x1000)) // priority low?
  206. continue;
  207. if (code != oldcode) {
  208. oldcode = code;
  209. // Get tile address/2:
  210. addr = (code & 0x1ff) << 4;
  211. addr += tilex_ty>> 16;
  212. if (code & 0x0400)
  213. addr ^= 0xe; // Y-flip
  214. pal = (code>>7) & 0x10;
  215. }
  216. pack = CPU_LE2(*(u32 *)(PicoMem.vram + addr)); /* Get 4 bitplanes / 8 pixels */
  217. if (pack == 0) {
  218. blank = code;
  219. continue;
  220. }
  221. if (code & 0x0200) TileFlipM4(cells_dx, pack, pal);
  222. else TileNormM4(cells_dx, pack, pal);
  223. }
  224. }
  225. static void DrawDisplayM4(int scanline)
  226. {
  227. struct PicoVideo *pv = &Pico.video;
  228. u16 *nametab, *nametab2;
  229. int line, tilex, dx, ty, cells;
  230. int cellskip = 0; // XXX
  231. int maxcells = 32;
  232. // Find the line in the name table
  233. line = pv->reg[9] + scanline; // vscroll + scanline
  234. // Find name table:
  235. nametab = PicoMem.vram;
  236. if ((pv->reg[0] & 6) == 6 && (pv->reg[1] & 0x18)) {
  237. // 224/240 line mode
  238. line &= 0xff;
  239. nametab += ((pv->reg[2] & 0x0c) << (10-1)) + (0x700 >> 1);
  240. } else {
  241. while (line >= 224) line -= 224;
  242. nametab += (pv->reg[2] & 0x0e) << (10-1);
  243. // old SMS only, masks line:7 with reg[2]:0 for address calculation
  244. //if ((pv->reg[2] & 0x01) == 0) line &= 0x7f;
  245. }
  246. nametab2 = nametab + ((scanline>>3) << (6-1));
  247. nametab = nametab + ((line>>3) << (6-1));
  248. dx = pv->reg[8]; // hscroll
  249. if (scanline < 16 && (pv->reg[0] & 0x40))
  250. dx = 0; // hscroll disabled for top 2 rows (e.g. Fantasy Zone II)
  251. tilex = ((-dx >> 3) + cellskip) & 0x1f;
  252. ty = (line & 7) << 1; // Y-Offset into tile
  253. cells = maxcells - cellskip;
  254. dx = ((dx - 1) & 7) + 1;
  255. if (dx != 8)
  256. cells++; // have hscroll, need to draw 1 cell more
  257. dx += cellskip << 3;
  258. dx += line_offset;
  259. // low priority tiles
  260. if (!(pv->debug_p & PVD_KILL_B)) {
  261. if ((Pico.m.hardware & 0x3) == 0x3) {
  262. // on GG render only the center 160 px
  263. DrawStripLowM4(nametab , dx | ((cells-12)<< 16),(tilex+6) | (ty << 16));
  264. } else if (pv->reg[0] & 0x80) {
  265. // vscroll disabled for rightmost 8 columns (e.g. Gauntlet)
  266. int dx2 = dx + (cells-8)*8, tilex2 = tilex + (cells-8), ty2 = scanline&7;
  267. DrawStripLowM4(nametab, dx | ((cells-8) << 16), tilex | (ty << 16));
  268. DrawStripLowM4(nametab2, dx2 | (8 << 16), tilex2 | (ty2 << 17));
  269. } else
  270. DrawStripLowM4(nametab , dx | ( cells << 16), tilex | (ty << 16));
  271. }
  272. // sprites
  273. if (!(pv->debug_p & PVD_KILL_S_LO))
  274. DrawSpritesM4(scanline);
  275. // high priority tiles (use virtual layer switch just for fun)
  276. if (!(pv->debug_p & PVD_KILL_A)) {
  277. if ((Pico.m.hardware & 0x3) == 0x3) {
  278. DrawStripHighM4(nametab , dx | ((cells-12)<< 16),(tilex+6) | (ty << 16));
  279. } else if (pv->reg[0] & 0x80) {
  280. int dx2 = dx + (cells-8)*8, tilex2 = tilex + (cells-8), ty2 = scanline&7;
  281. DrawStripHighM4(nametab, dx | ((cells-8) << 16), tilex | (ty << 16));
  282. DrawStripHighM4(nametab2, dx2 | (8 << 16), tilex2 | (ty2 << 17));
  283. } else
  284. DrawStripHighM4(nametab , dx | ( cells << 16), tilex | (ty << 16));
  285. }
  286. if (pv->reg[0] & 0x20) {
  287. // first column masked with background, caculate offset to start of line
  288. dx = (dx&~0x1f) / 4;
  289. ty = 0xe0e0e0e0; // really (pv->reg[7]&0x3f) * 0x01010101, but the looks...
  290. ((u32 *)Pico.est.HighCol)[dx+2] = ((u32 *)Pico.est.HighCol)[dx+3] = ty;
  291. }
  292. }
  293. /* Mode 2 */
  294. /*========*/
  295. /* Background */
  296. #define TMS_PIXELBG(x,p) \
  297. t = (pack>>(7-p)) & 0x01; \
  298. t = (pal >> (t << 2)) & 0x0f; \
  299. pd[x] = t;
  300. static void TileNormBgM2(u16 sx, unsigned int pack, int pal)
  301. {
  302. u8 *pd = Pico.est.HighCol + sx;
  303. unsigned int t;
  304. TMS_PIXELBG(0, 0)
  305. TMS_PIXELBG(1, 1)
  306. TMS_PIXELBG(2, 2)
  307. TMS_PIXELBG(3, 3)
  308. TMS_PIXELBG(4, 4)
  309. TMS_PIXELBG(5, 5)
  310. TMS_PIXELBG(6, 6)
  311. TMS_PIXELBG(7, 7)
  312. }
  313. /* Sprites */
  314. #define TMS_PIXELSP(x,p) \
  315. t = (pack>>(7-p)) & 0x01; \
  316. if (t) \
  317. pd[x] = pal;
  318. static void TileNormSprM2(u16 sx, unsigned int pack, int pal)
  319. {
  320. u8 *pd = Pico.est.HighCol + sx;
  321. unsigned int t;
  322. TMS_PIXELSP(0, 0)
  323. TMS_PIXELSP(1, 1)
  324. TMS_PIXELSP(2, 2)
  325. TMS_PIXELSP(3, 3)
  326. TMS_PIXELSP(4, 4)
  327. TMS_PIXELSP(5, 5)
  328. TMS_PIXELSP(6, 6)
  329. TMS_PIXELSP(7, 7)
  330. }
  331. static void TileDoubleSprM2(u16 sx, unsigned int pack, int pal)
  332. {
  333. u8 *pd = Pico.est.HighCol + sx;
  334. unsigned int t;
  335. TMS_PIXELSP(0, 0)
  336. TMS_PIXELSP(1, 0)
  337. TMS_PIXELSP(2, 1)
  338. TMS_PIXELSP(3, 1)
  339. TMS_PIXELSP(4, 2)
  340. TMS_PIXELSP(5, 2)
  341. TMS_PIXELSP(6, 3)
  342. TMS_PIXELSP(7, 3)
  343. TMS_PIXELSP(8, 4)
  344. TMS_PIXELSP(9, 4)
  345. TMS_PIXELSP(10, 5)
  346. TMS_PIXELSP(11, 5)
  347. TMS_PIXELSP(12, 6)
  348. TMS_PIXELSP(13, 6)
  349. TMS_PIXELSP(14, 7)
  350. TMS_PIXELSP(15, 7)
  351. }
  352. /* Draw sprites into a scanline, max 4 */
  353. static void DrawSpritesM2(int scanline)
  354. {
  355. struct PicoVideo *pv = &Pico.video;
  356. unsigned int sprites_addr[4];
  357. unsigned int sprites_x[4];
  358. unsigned int pack;
  359. u8 *sat;
  360. int xoff = 8; // relative to HighCol, which is (screen - 8)
  361. int sprite_base, addr_mask;
  362. int zoomed = pv->reg[1] & 0x1; // zoomed sprites
  363. int i, s, h;
  364. xoff += line_offset;
  365. sat = (u8 *)PicoMem.vramb + ((pv->reg[5] & 0x7e) << 7);
  366. if (pv->reg[1] & 2) {
  367. addr_mask = 0xfc; h = 16;
  368. } else {
  369. addr_mask = 0xff; h = 8;
  370. }
  371. if (zoomed) h *= 2;
  372. sprite_base = (pv->reg[6] & 0x7) << 11;
  373. /* find sprites on this scanline */
  374. for (i = s = 0; i < 32; i++)
  375. {
  376. int y;
  377. y = (sat[MEM_LE2(4*i)] + 1) & 0xff;
  378. if (y == 0xd1)
  379. break;
  380. if (y > 0xe0)
  381. y -= 256;
  382. if (y + h <= scanline || scanline < y)
  383. continue; // not on this line
  384. if (s >= 4) {
  385. pv->status |= SR_SOVR | i;
  386. break;
  387. }
  388. sprites_x[s] = 4*i;
  389. sprites_addr[s] = sprite_base + ((sat[MEM_LE2(4*i + 2)] & addr_mask) << 3) +
  390. ((scanline - y) >> zoomed);
  391. s++;
  392. }
  393. // really half-assed but better than nothing
  394. if (s > 1)
  395. pv->status |= SR_C;
  396. // now draw all sprites backwards
  397. for (--s; s >= 0; s--) {
  398. int x, c, w = (zoomed ? 16: 8);
  399. i = sprites_x[s];
  400. x = sat[MEM_LE2(i+1)] + xoff;
  401. if (sat[MEM_LE2(i+3)] & 0x80)
  402. x -= 32;
  403. c = sat[MEM_LE2(i+3)] & 0x0f;
  404. if (x > 0) {
  405. pack = PicoMem.vramb[MEM_LE2(sprites_addr[s])];
  406. if (zoomed) TileDoubleSprM2(x, pack, c);
  407. else TileNormSprM2(x, pack, c);
  408. }
  409. if((pv->reg[1] & 0x2) && (x+=w) > 0) {
  410. pack = PicoMem.vramb[MEM_LE2(sprites_addr[s]+0x10)];
  411. if (zoomed) TileDoubleSprM2(x, pack, c);
  412. else TileNormSprM2(x, pack, c);
  413. }
  414. }
  415. }
  416. /* Draw the background into a scanline; cells, dx, tilex, ty merged to reduce registers */
  417. static void DrawStripM2(const u8 *nametab, const u8 *coltab, const u8 *pattab, int cells_dx, int tilex_ty)
  418. {
  419. // Draw tiles across screen:
  420. for (; cells_dx > 0; cells_dx += 8, tilex_ty++, cells_dx -= 0x10000)
  421. {
  422. unsigned int pack, pal;
  423. unsigned code;
  424. code = nametab[tilex_ty& 0x1f] << 3;
  425. pal = coltab[code];
  426. pack = pattab[code];
  427. TileNormBgM2(cells_dx, pack, pal);
  428. }
  429. }
  430. /* Draw a scanline */
  431. static void DrawDisplayM2(int scanline)
  432. {
  433. struct PicoVideo *pv = &Pico.video;
  434. u8 *nametab, *coltab, *pattab;
  435. int tilex, dx, cells;
  436. int cellskip = 0; // XXX
  437. int maxcells = 32;
  438. // name, color, pattern table:
  439. nametab = PicoMem.vramb + ((pv->reg[2]<<10) & 0x3c00);
  440. coltab = PicoMem.vramb + ((pv->reg[3]<< 6) & 0x2000);
  441. pattab = PicoMem.vramb + ((pv->reg[4]<<11) & 0x2000);
  442. nametab += ((scanline>>3) << 5);
  443. coltab += ((scanline>>6) <<11) + (scanline & 0x7);
  444. pattab += ((scanline>>6) <<11) + (scanline & 0x7);
  445. tilex = cellskip & 0x1f;
  446. cells = maxcells - cellskip;
  447. dx = (cellskip << 3) + line_offset + 8;
  448. // tiles
  449. if (!(pv->debug_p & PVD_KILL_B))
  450. DrawStripM2(nametab, coltab, pattab, dx | (cells << 16), tilex | (scanline << 16));
  451. // sprites
  452. if (!(pv->debug_p & PVD_KILL_S_LO))
  453. DrawSpritesM2(scanline);
  454. }
  455. /* Common/global */
  456. /*===============*/
  457. static void FinalizeLineRGB555SMS(int line);
  458. void PicoFrameStartSMS(void)
  459. {
  460. int lines = 192, columns = 256, loffs, coffs;
  461. skip_next_line = 0;
  462. loffs = screen_offset = 24; // 192 lines is really 224 with top/bottom bars
  463. Pico.est.rendstatus = PDRAW_32_COLS;
  464. // Copy LCD enable flag for easier handling
  465. Pico.m.hardware &= ~0x2;
  466. if (PicoIn.opt & POPT_EN_GG_LCD)
  467. Pico.m.hardware |= 0x2;
  468. if ((Pico.m.hardware & 0x3) == 0x3) {
  469. // GG LCD always has 160x144 regardless of settings
  470. screen_offset = 24; // nonetheless the vdp timing has 224 lines
  471. loffs = 48;
  472. lines = 144;
  473. columns = 160;
  474. } else switch ((Pico.video.reg[0]&0x06) | (Pico.video.reg[1]&0x18)) {
  475. // SMS2 only 224/240 line modes, e.g. Micro Machines
  476. case 0x06|0x08:
  477. loffs = screen_offset = 0;
  478. lines = 240;
  479. break;
  480. case 0x06|0x10:
  481. loffs = screen_offset = 8;
  482. lines = 224;
  483. break;
  484. }
  485. if (PicoIn.opt & POPT_EN_SOFTSCALE) {
  486. coffs = 0;
  487. columns = 320;
  488. } else
  489. coffs = PicoIn.opt & POPT_DIS_32C_BORDER ? 0:(320-columns)/2;
  490. line_offset = (PicoIn.opt & POPT_ALT_RENDERER ? coffs : 0);
  491. if (FinalizeLineSMS == FinalizeLineRGB555SMS)
  492. line_offset = 0 /* done in FinalizeLine */;
  493. if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
  494. emu_video_mode_change(loffs, lines, coffs, columns);
  495. rendstatus_old = Pico.est.rendstatus;
  496. rendlines = lines;
  497. }
  498. Pico.est.HighCol = HighColBase + screen_offset * HighColIncrement;
  499. Pico.est.DrawLineDest = (char *)DrawLineDestBase + screen_offset * DrawLineDestIncrement;
  500. }
  501. void PicoLineSMS(int line)
  502. {
  503. int skip = skip_next_line;
  504. // GG LCD, render only visible part of screen
  505. if ((Pico.m.hardware & 0x3) == 0x3 && (line < 24 || line >= 24+144))
  506. goto norender;
  507. if (PicoScanBegin != NULL && skip == 0)
  508. skip = PicoScanBegin(line + screen_offset);
  509. if (skip) {
  510. skip_next_line = skip - 1;
  511. return;
  512. }
  513. // Draw screen:
  514. BackFill(Pico.video.reg[7] & 0x0f, 0, &Pico.est);
  515. if (Pico.video.reg[1] & 0x40) {
  516. if (Pico.video.reg[0] & 0x04) DrawDisplayM4(line);
  517. else DrawDisplayM2(line);
  518. }
  519. if (FinalizeLineSMS != NULL)
  520. FinalizeLineSMS(line);
  521. if (PicoScanEnd != NULL)
  522. skip_next_line = PicoScanEnd(line + screen_offset);
  523. norender:
  524. Pico.est.HighCol += HighColIncrement;
  525. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  526. }
  527. /* Fixed palette for TMS9918 modes */
  528. static u16 tmspal[32] = {
  529. 0x0000, 0x0000, 0x00a0, 0x00f0, 0x0500, 0x0f00, 0x0005, 0x0ff0,
  530. 0x000a, 0x000f, 0x0055, 0x00ff, 0x0050, 0x0f0f, 0x0555, 0x0fff,
  531. };
  532. void PicoDoHighPal555SMS(void)
  533. {
  534. u32 *spal=(void *)PicoMem.cram;
  535. u32 *dpal=(void *)Pico.est.HighPal;
  536. unsigned int t;
  537. int i;
  538. Pico.m.dirtyPal = 0;
  539. if (!(Pico.video.reg[0] & 0x4))
  540. spal = (u32 *)tmspal;
  541. /* SMS 6 bit cram data was already converted to MD/GG format by vdp write,
  542. * hence GG/SMS/TMS can all be handled the same here */
  543. for (i = 0x20/2; i > 0; i--, spal++, dpal++) {
  544. t = *spal;
  545. #if defined(USE_BGR555)
  546. t = ((t & 0x000f000f)<< 1) | ((t & 0x00f000f0)<<2) | ((t & 0x0f000f00)<<3);
  547. t |= (t >> 4) & 0x04210421;
  548. #elif defined(USE_BGR565)
  549. t = ((t & 0x000f000f)<< 1) | ((t & 0x00f000f0)<<3) | ((t & 0x0f000f00)<<4);
  550. t |= (t >> 4) & 0x08610861;
  551. #else
  552. t = ((t & 0x000f000f)<<12) | ((t & 0x00f000f0)<<3) | ((t & 0x0f000f00)>>7);
  553. t |= (t >> 4) & 0x08610861;
  554. #endif
  555. *dpal = t;
  556. }
  557. Pico.est.HighPal[0xe0] = 0;
  558. }
  559. static void FinalizeLineRGB555SMS(int line)
  560. {
  561. if (Pico.m.dirtyPal)
  562. PicoDoHighPal555SMS();
  563. // standard FinalizeLine can finish it for us,
  564. // with features like scaling and such
  565. FinalizeLine555(0, line, &Pico.est);
  566. }
  567. static void FinalizeLine8bitSMS(int line)
  568. {
  569. FinalizeLine8bit(0, line, &Pico.est);
  570. }
  571. void PicoDrawSetOutputSMS(pdso_t which)
  572. {
  573. switch (which)
  574. {
  575. case PDF_8BIT: FinalizeLineSMS = FinalizeLine8bitSMS; break;
  576. case PDF_RGB555: FinalizeLineSMS = FinalizeLineRGB555SMS; break;
  577. default: FinalizeLineSMS = NULL;
  578. PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328); break;
  579. }
  580. rendstatus_old = -1;
  581. }
  582. // vim:shiftwidth=2:ts=2:expandtab