mode4.c 17 KB

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