draw2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * tile renderer
  3. * (C) notaz, 2006-2008
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include "pico_int.h"
  9. #define START_ROW 0 // which row of tiles to start rendering at?
  10. #define END_ROW 28 // ..end
  11. #define VSRAM 0 // 2-cell vscroll (broken for line based hscroll)
  12. #define INTERLACE 0 // interlace mode 2
  13. #define TILE_ROWS END_ROW-START_ROW
  14. // note: this is not implemented in ARM asm
  15. #if defined(DRAW2_OVERRIDE_LINE_WIDTH)
  16. #define LINE_WIDTH DRAW2_OVERRIDE_LINE_WIDTH
  17. #else
  18. #define LINE_WIDTH 328
  19. #endif
  20. static unsigned char PicoDraw2FB_[LINE_WIDTH * (8+240+8) + 8];
  21. static u32 HighCache2A[2*41*(TILE_ROWS+1)+1+1]; // caches for high layers
  22. static u32 HighCache2B[2*41*(TILE_ROWS+1)+1+1];
  23. unsigned short *PicoCramHigh=PicoMem.cram; // pointer to CRAM buff (0x40 shorts), converted to native device color (works only with 16bit for now)
  24. void (*PicoPrepareCram)()=0; // prepares PicoCramHigh for renderer to use
  25. // stuff available in asm:
  26. #ifdef _ASM_DRAW_C
  27. void BackFillFull(void *dst, int reg7);
  28. void DrawLayerFull(int plane, u32 *hcache, int planestart, int planeend,
  29. struct PicoEState *est);
  30. void DrawTilesFromCacheF(u32 *hc, struct PicoEState *est);
  31. void DrawWindowFull(int start, int end, int prio, struct PicoEState *est);
  32. void DrawSpriteFull(unsigned int *sprite, struct PicoEState *est);
  33. #else
  34. static int TileXnormYnorm(unsigned char *pd,int addr,unsigned char pal, struct PicoEState *est)
  35. {
  36. unsigned int pack=0; unsigned int t=0, blank = 1;
  37. unsigned short *vram = est->PicoMem_vram;
  38. int i, inc=2;
  39. #if INTERLACE
  40. if (est->rendstatus & PDRAW_INTERLACE) inc = 4;
  41. #endif
  42. for(i=8; i; i--, addr+=inc, pd += est->Draw2Width) {
  43. pack=*(u32 *)(vram+addr); // Get 8 pixels
  44. if(!pack) continue;
  45. t=pack&0x0000f000; if (t) pd[0]=(unsigned char)((t>>12)|pal);
  46. t=pack&0x00000f00; if (t) pd[1]=(unsigned char)((t>> 8)|pal);
  47. t=pack&0x000000f0; if (t) pd[2]=(unsigned char)((t>> 4)|pal);
  48. t=pack&0x0000000f; if (t) pd[3]=(unsigned char)((t )|pal);
  49. t=pack&0xf0000000; if (t) pd[4]=(unsigned char)((t>>28)|pal);
  50. t=pack&0x0f000000; if (t) pd[5]=(unsigned char)((t>>24)|pal);
  51. t=pack&0x00f00000; if (t) pd[6]=(unsigned char)((t>>20)|pal);
  52. t=pack&0x000f0000; if (t) pd[7]=(unsigned char)((t>>16)|pal);
  53. blank = 0;
  54. }
  55. return blank; // Tile blank?
  56. }
  57. static int TileXflipYnorm(unsigned char *pd,int addr,unsigned char pal, struct PicoEState *est)
  58. {
  59. unsigned int pack=0; unsigned int t=0, blank = 1;
  60. unsigned short *vram = est->PicoMem_vram;
  61. int i, inc=2;
  62. #if INTERLACE
  63. if (est->rendstatus & PDRAW_INTERLACE) inc = 4;
  64. #endif
  65. for(i=8; i; i--, addr+=inc, pd += est->Draw2Width) {
  66. pack=*(u32 *)(vram+addr); // Get 8 pixels
  67. if(!pack) continue;
  68. t=pack&0x000f0000; if (t) pd[0]=(unsigned char)((t>>16)|pal);
  69. t=pack&0x00f00000; if (t) pd[1]=(unsigned char)((t>>20)|pal);
  70. t=pack&0x0f000000; if (t) pd[2]=(unsigned char)((t>>24)|pal);
  71. t=pack&0xf0000000; if (t) pd[3]=(unsigned char)((t>>28)|pal);
  72. t=pack&0x0000000f; if (t) pd[4]=(unsigned char)((t )|pal);
  73. t=pack&0x000000f0; if (t) pd[5]=(unsigned char)((t>> 4)|pal);
  74. t=pack&0x00000f00; if (t) pd[6]=(unsigned char)((t>> 8)|pal);
  75. t=pack&0x0000f000; if (t) pd[7]=(unsigned char)((t>>12)|pal);
  76. blank = 0;
  77. }
  78. return blank; // Tile blank?
  79. }
  80. static int TileXnormYflip(unsigned char *pd,int addr,unsigned char pal, struct PicoEState *est)
  81. {
  82. unsigned int pack=0; unsigned int t=0, blank = 1;
  83. unsigned short *vram = est->PicoMem_vram;
  84. int i, inc=2;
  85. #if INTERLACE
  86. if (est->rendstatus & PDRAW_INTERLACE) inc = 4, addr += 16;
  87. #endif
  88. addr+=14;
  89. for(i=8; i; i--, addr-=inc, pd += est->Draw2Width) {
  90. pack=*(u32 *)(vram+addr); // Get 8 pixels
  91. if(!pack) continue;
  92. t=pack&0x0000f000; if (t) pd[0]=(unsigned char)((t>>12)|pal);
  93. t=pack&0x00000f00; if (t) pd[1]=(unsigned char)((t>> 8)|pal);
  94. t=pack&0x000000f0; if (t) pd[2]=(unsigned char)((t>> 4)|pal);
  95. t=pack&0x0000000f; if (t) pd[3]=(unsigned char)((t )|pal);
  96. t=pack&0xf0000000; if (t) pd[4]=(unsigned char)((t>>28)|pal);
  97. t=pack&0x0f000000; if (t) pd[5]=(unsigned char)((t>>24)|pal);
  98. t=pack&0x00f00000; if (t) pd[6]=(unsigned char)((t>>20)|pal);
  99. t=pack&0x000f0000; if (t) pd[7]=(unsigned char)((t>>16)|pal);
  100. blank = 0;
  101. }
  102. return blank; // Tile blank?
  103. }
  104. static int TileXflipYflip(unsigned char *pd,int addr,unsigned char pal, struct PicoEState *est)
  105. {
  106. unsigned int pack=0; unsigned int t=0, blank = 1;
  107. unsigned short *vram = est->PicoMem_vram;
  108. int i, inc=2;
  109. #if INTERLACE
  110. if (est->rendstatus & PDRAW_INTERLACE) inc = 4, addr += 16;
  111. #endif
  112. addr+=14;
  113. for(i=8; i; i--, addr-=inc, pd += est->Draw2Width) {
  114. pack=*(u32 *)(vram+addr); // Get 8 pixels
  115. if(!pack) continue;
  116. t=pack&0x000f0000; if (t) pd[0]=(unsigned char)((t>>16)|pal);
  117. t=pack&0x00f00000; if (t) pd[1]=(unsigned char)((t>>20)|pal);
  118. t=pack&0x0f000000; if (t) pd[2]=(unsigned char)((t>>24)|pal);
  119. t=pack&0xf0000000; if (t) pd[3]=(unsigned char)((t>>28)|pal);
  120. t=pack&0x0000000f; if (t) pd[4]=(unsigned char)((t )|pal);
  121. t=pack&0x000000f0; if (t) pd[5]=(unsigned char)((t>> 4)|pal);
  122. t=pack&0x00000f00; if (t) pd[6]=(unsigned char)((t>> 8)|pal);
  123. t=pack&0x0000f000; if (t) pd[7]=(unsigned char)((t>>12)|pal);
  124. blank = 0;
  125. }
  126. return blank; // Tile blank?
  127. }
  128. // start: (tile_start<<16)|row_start, end: [same]
  129. static void DrawWindowFull(int start, int end, int prio, struct PicoEState *est)
  130. {
  131. struct PicoVideo *pvid=&est->Pico->video;
  132. int nametab, nametab_step, trow, tilex, blank=-1, code;
  133. unsigned char *scrpos = est->Draw2FB;
  134. int scrstart = est->Draw2Start;
  135. int tile_start, tile_end; // in cells
  136. // parse ranges
  137. tile_start = start>>16;
  138. tile_end = end>>16;
  139. start = start<<16>>16;
  140. end = end<<16>>16;
  141. // Find name table line:
  142. if (pvid->reg[12]&1)
  143. {
  144. nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
  145. nametab_step = 1<<6;
  146. }
  147. else
  148. {
  149. nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
  150. nametab_step = 1<<5;
  151. if (est->rendstatus & PDRAW_BORDER_32)
  152. scrpos += 32;
  153. }
  154. nametab += nametab_step*(start-scrstart);
  155. // check priority
  156. code=PicoMem.vram[nametab+tile_start];
  157. if ((code>>15) != prio) return; // hack: just assume that whole window uses same priority
  158. scrpos+=8*est->Draw2Width+8;
  159. scrpos+=8*est->Draw2Width*(start-scrstart);
  160. // do a window until we reach planestart row
  161. for(trow = start; trow < end; trow++, nametab+=nametab_step) { // current tile row
  162. for (tilex=tile_start; tilex<tile_end; tilex++)
  163. {
  164. int code,addr,zero=0;
  165. // unsigned short *pal=NULL;
  166. unsigned char pal;
  167. code=PicoMem.vram[nametab+tilex];
  168. if (code==blank) continue;
  169. // Get tile address/2:
  170. #if INTERLACE
  171. if (est->rendstatus & PDRAW_INTERLACE)
  172. addr=(code&0x3ff)<<5;
  173. else
  174. #endif
  175. addr=(code&0x7ff)<<4;
  176. // pal=PicoCramHigh+((code>>9)&0x30);
  177. pal=(unsigned char)((code>>9)&0x30);
  178. switch((code>>11)&3) {
  179. case 0: zero=TileXnormYnorm(scrpos+(tilex<<3),addr,pal,est); break;
  180. case 1: zero=TileXflipYnorm(scrpos+(tilex<<3),addr,pal,est); break;
  181. case 2: zero=TileXnormYflip(scrpos+(tilex<<3),addr,pal,est); break;
  182. case 3: zero=TileXflipYflip(scrpos+(tilex<<3),addr,pal,est); break;
  183. }
  184. if(zero) blank=code; // We know this tile is blank now
  185. }
  186. scrpos += est->Draw2Width*8;
  187. }
  188. }
  189. static void DrawLayerFull(int plane, u32 *hcache, int planestart, int planeend,
  190. struct PicoEState *est)
  191. {
  192. struct PicoVideo *pvid=&est->Pico->video;
  193. static char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps
  194. int width, height, ymask, htab;
  195. int nametab, hscroll=0, vscroll, cells;
  196. unsigned char *scrpos;
  197. int scrstart = est->Draw2Start;
  198. int blank=-1, xmask, nametab_row, trow;
  199. // parse ranges
  200. cells = (planeend>>16)-(planestart>>16);
  201. planestart = planestart<<16>>16;
  202. planeend = planeend<<16>>16;
  203. // Work out the Tiles to draw
  204. htab=pvid->reg[13]<<9; // Horizontal scroll table address
  205. // if ( pvid->reg[11]&2) htab+=Scanline<<1; // Offset by line
  206. // if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
  207. htab+=plane; // A or B
  208. if(!(pvid->reg[11]&3)) { // full screen scroll
  209. // Get horizontal scroll value
  210. hscroll=PicoMem.vram[htab&0x7fff];
  211. htab = 0; // this marks that we don't have to update scroll value
  212. }
  213. // Work out the name table size: 32 64 or 128 tiles (0-3)
  214. width=pvid->reg[16];
  215. height=(width>>4)&3; width&=3;
  216. xmask=(1<<shift[width ])-1; // X Mask in tiles
  217. ymask=(height<<5)|0x1f; // Y Mask in tiles
  218. if(width == 1) ymask&=0x3f;
  219. else if(width>1) ymask =0x1f;
  220. // Find name table:
  221. if (plane==0) nametab=(pvid->reg[2]&0x38)<< 9; // A
  222. else nametab=(pvid->reg[4]&0x07)<<12; // B
  223. scrpos = est->Draw2FB;
  224. if (est->rendstatus & PDRAW_BORDER_32)
  225. scrpos += 32;
  226. scrpos+=8*est->Draw2Width*(planestart-scrstart);
  227. if((pvid->reg[11]&4)||(PicoMem.vsram[plane]&7))
  228. planeend++; // we (may) have vertically clipped tiles due to vscroll, so we need 1 more row
  229. for(trow = planestart; trow < planeend; trow++) { // current tile row
  230. int cellc=cells,tilex,dx,vsidx=0;
  231. // Get vertical scroll value:
  232. vscroll=PicoMem.vsram[plane];//&0x1ff;
  233. #if VSRAM
  234. if (!(pvid->reg[12]&1) && (pvid->reg[11]&4)) // H32 + 2-cell mode
  235. vscroll=PicoMem.vsram[plane+0x20];//&0x1ff;
  236. #endif
  237. #if INTERLACE
  238. if (est->rendstatus & PDRAW_INTERLACE)
  239. vscroll >>= 1;
  240. #endif
  241. nametab_row = nametab + (((trow+(vscroll>>3))&ymask)<<shift[width]); // pointer to nametable entries for this row
  242. // Find the tile row in the name table
  243. //ts.line=(vscroll+Scanline)&ymask;
  244. //ts.nametab+=(ts.line>>3)<<shift[width];
  245. // update hscroll if needed
  246. if(htab) {
  247. int htaddr=htab+(trow<<4);
  248. if(trow) htaddr-=(vscroll&7)<<1;
  249. hscroll=PicoMem.vram[htaddr&0x7fff];
  250. }
  251. // Draw tiles across screen:
  252. tilex=(-hscroll)>>3;
  253. dx=((hscroll-1)&7)+1;
  254. if(dx != 8) cellc++, vsidx--; // have hscroll, do more cells
  255. for (; cellc; dx+=8,tilex++,cellc--)
  256. {
  257. int code=0,addr=0,zero=0,scroff;
  258. // unsigned short *pal=NULL;
  259. unsigned char pal;
  260. #if VSRAM
  261. if ((pvid->reg[11]&4) && !(vsidx&1)) { // 2-cell mode
  262. vscroll=PicoMem.vsram[vsidx+plane];//&0x1ff;
  263. #if INTERLACE
  264. if (est->rendstatus & PDRAW_INTERLACE)
  265. vscroll >>= 1;
  266. #endif
  267. nametab_row = nametab + (((trow+(vscroll>>3))&ymask)<<shift[width]); // pointer to nametable entries for this row
  268. }
  269. #endif
  270. vsidx++;
  271. code=PicoMem.vram[nametab_row+(tilex&xmask)];
  272. if (code==blank) continue;
  273. if (code>>15) { // high priority tile
  274. *hcache++ = code|(dx<<16)|(trow<<27); // cache it
  275. *hcache++ = 8-(vscroll&7); // push y-offset to tilecache
  276. continue;
  277. }
  278. // Get tile address/2:
  279. #if INTERLACE
  280. if (est->rendstatus & PDRAW_INTERLACE)
  281. addr=(code&0x3ff)<<5;
  282. else
  283. #endif
  284. addr=(code&0x7ff)<<4;
  285. // pal=PicoCramHigh+((code>>9)&0x30);
  286. pal=(unsigned char)((code>>9)&0x30);
  287. scroff=(8-(vscroll&7))*est->Draw2Width;
  288. switch((code>>11)&3) {
  289. case 0: zero=TileXnormYnorm(scrpos+scroff+dx,addr,pal,est); break;
  290. case 1: zero=TileXflipYnorm(scrpos+scroff+dx,addr,pal,est); break;
  291. case 2: zero=TileXnormYflip(scrpos+scroff+dx,addr,pal,est); break;
  292. case 3: zero=TileXflipYflip(scrpos+scroff+dx,addr,pal,est); break;
  293. }
  294. if(zero) blank=code; // We know this tile is blank now
  295. }
  296. scrpos += est->Draw2Width*8;
  297. }
  298. *hcache = 0; // terminate cache
  299. }
  300. static void DrawTilesFromCacheF(u32 *hc, struct PicoEState *est)
  301. {
  302. u32 code;
  303. int addr, zero = 0, vscroll;
  304. unsigned int prevy=0xFFFFFFFF;
  305. // unsigned short *pal;
  306. unsigned char pal;
  307. short blank=-1; // The tile we know is blank
  308. unsigned char *scrpos = est->Draw2FB, *pd = 0;
  309. int scrstart = est->Draw2Start;
  310. if (est->rendstatus & PDRAW_BORDER_32)
  311. scrpos += 32;
  312. while((code=*hc++)) {
  313. vscroll=(*hc++ - START_ROW*8)*est->Draw2Width;
  314. if((short)code == blank) continue;
  315. // y pos
  316. if(((unsigned)code>>27) != prevy) {
  317. prevy = (unsigned)code>>27;
  318. pd = scrpos + (prevy-scrstart)*est->Draw2Width*8;
  319. }
  320. // Get tile address/2:
  321. #if INTERLACE
  322. if (est->rendstatus & PDRAW_INTERLACE)
  323. addr=(code&0x3ff)<<5;
  324. else
  325. #endif
  326. addr=(code&0x7ff)<<4;
  327. // pal=PicoCramHigh+((code>>9)&0x30);
  328. pal=(unsigned char)((code>>9)&0x30);
  329. switch((code>>11)&3) {
  330. case 0: zero=TileXnormYnorm(pd+vscroll+((code>>16)&0x1ff),addr,pal,est); break;
  331. case 1: zero=TileXflipYnorm(pd+vscroll+((code>>16)&0x1ff),addr,pal,est); break;
  332. case 2: zero=TileXnormYflip(pd+vscroll+((code>>16)&0x1ff),addr,pal,est); break;
  333. case 3: zero=TileXflipYflip(pd+vscroll+((code>>16)&0x1ff),addr,pal,est); break;
  334. }
  335. if(zero) blank=(short)code;
  336. }
  337. }
  338. // sx and sy are coords of virtual screen with 8pix borders on top and on left
  339. static void DrawSpriteFull(unsigned int *sprite, struct PicoEState *est)
  340. {
  341. int width=0,height=0;
  342. // unsigned short *pal=NULL;
  343. unsigned char pal;
  344. int tile,code,tdeltax,tdeltay;
  345. unsigned char *scrpos;
  346. int scrstart = est->Draw2Start;
  347. int sx, sy;
  348. sy=sprite[0];
  349. height=sy>>24;
  350. #if INTERLACE
  351. if (est->rendstatus & PDRAW_INTERLACE)
  352. sy = ((sy>>1)&0x1ff)-0x78;
  353. else
  354. #endif
  355. sy=(sy&0x1ff)-0x78; // Y
  356. width=(height>>2)&3; height&=3;
  357. width++; height++; // Width and height in tiles
  358. code=sprite[1];
  359. sx=((code>>16)&0x1ff)-0x78; // X
  360. tile=code&0x7ff; // Tile number
  361. tdeltax=height; // Delta to increase tile by going right
  362. tdeltay=1; // Delta to increase tile by going down
  363. if (code&0x1000) { tile+=tdeltax-1; tdeltay=-tdeltay; } // Flip Y
  364. if (code&0x0800) { tile+=tdeltax*(width-1); tdeltax=-tdeltax; } // Flip X
  365. //delta<<=4; // Delta of address
  366. // pal=PicoCramHigh+((code>>9)&0x30); // Get palette pointer
  367. pal=(unsigned char)((code>>9)&0x30);
  368. // goto first vertically visible tile
  369. while(sy <= scrstart*8) { sy+=8; tile+=tdeltay; height--; }
  370. scrpos = est->Draw2FB;
  371. if (est->rendstatus&PDRAW_BORDER_32)
  372. scrpos += 32;
  373. scrpos+=(sy-scrstart*8)*est->Draw2Width;
  374. for (; height > 0; height--, sy+=8, tile+=tdeltay)
  375. {
  376. int w = width, x=sx, t=tile, s;
  377. if((sy-scrstart*8) >= END_ROW*8+8) return; // offscreen
  378. for (; w; w--,x+=8,t+=tdeltax)
  379. {
  380. if(x<=0) continue;
  381. if(x>=328) break; // Offscreen
  382. t&=0x7fff; // Clip tile address
  383. #if INTERLACE
  384. if (est->rendstatus & PDRAW_INTERLACE)
  385. s=5;
  386. else
  387. #endif
  388. s=4;
  389. switch((code>>11)&3) {
  390. case 0: TileXnormYnorm(scrpos+x,t<<s,pal,est); break;
  391. case 1: TileXflipYnorm(scrpos+x,t<<s,pal,est); break;
  392. case 2: TileXnormYflip(scrpos+x,t<<s,pal,est); break;
  393. case 3: TileXflipYflip(scrpos+x,t<<s,pal,est); break;
  394. }
  395. }
  396. scrpos+=8*est->Draw2Width;
  397. }
  398. }
  399. #endif
  400. static void DrawAllSpritesFull(int prio, int maxwidth, struct PicoEState *est)
  401. {
  402. struct PicoVideo *pvid=&est->Pico->video;
  403. int table=0,maskrange=0;
  404. int i,u,link=0;
  405. unsigned int *sprites[80]; // Sprites
  406. int y_min=START_ROW*8, y_max=END_ROW*8; // for a simple sprite masking
  407. int max_sprites = pvid->reg[12]&1 ? 80 : 64;
  408. if (est->rendstatus & PDRAW_30_ROWS)
  409. y_min += 8, y_max += 8;
  410. table=pvid->reg[5]&0x7f;
  411. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  412. table<<=8; // Get sprite table address/2
  413. for (i = u = 0; u < max_sprites && link < max_sprites; u++)
  414. {
  415. unsigned int *sprite=NULL;
  416. int code, code2, sx, sy, height;
  417. sprite=(u32 *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  418. // get sprite info
  419. code = sprite[0];
  420. // check if it is not hidden vertically
  421. #if INTERLACE
  422. if (est->rendstatus & PDRAW_INTERLACE)
  423. sy = ((code>>1)&0x1ff)-0x80;
  424. else
  425. #endif
  426. sy = (code&0x1ff)-0x80;
  427. height = (((code>>24)&3)+1)<<3;
  428. if(sy+height <= y_min || sy > y_max) goto nextsprite;
  429. // masking sprite?
  430. code2=sprite[1];
  431. sx = (code2>>16)&0x1ff;
  432. if(!sx) {
  433. int to = sy+height; // sy ~ from
  434. if(maskrange) {
  435. // try to merge with previous range
  436. if((maskrange>>16)+1 >= sy && (maskrange>>16) <= to && (maskrange&0xffff) < sy) sy = (maskrange&0xffff);
  437. else if((maskrange&0xffff)-1 <= to && (maskrange&0xffff) >= sy && (maskrange>>16) > to) to = (maskrange>>16);
  438. }
  439. // support only very simple masking (top and bottom of screen)
  440. if(sy <= y_min && to+1 > y_min) y_min = to+1;
  441. else if(to >= y_max && sy-1 < y_max) y_max = sy-1;
  442. else maskrange=sy|(to<<16);
  443. goto nextsprite;
  444. }
  445. // priority
  446. if(((code2>>15)&1) != prio) goto nextsprite; // wrong priority
  447. // check if sprite is not hidden horizontally
  448. sx -= 0x78; // Get X coordinate + 8
  449. if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;
  450. // sprite is good, save it's index
  451. sprites[i++]=sprite;
  452. nextsprite:
  453. // Find next sprite
  454. link=(code>>16)&0x7f;
  455. if(!link) break; // End of sprites
  456. }
  457. // Go through sprites backwards:
  458. for (i--; i >= 0; i--)
  459. {
  460. DrawSpriteFull(sprites[i], est);
  461. }
  462. }
  463. #ifndef _ASM_DRAW_C
  464. static void BackFillFull(void *dst, int reg7)
  465. {
  466. unsigned int back;
  467. // Start with a background color:
  468. back=reg7&0x3f;
  469. back|=back<<8;
  470. back|=back<<16;
  471. memset32(dst, back, Pico.est.Draw2Width*(8+(END_ROW-START_ROW)*8)/4);
  472. }
  473. #endif
  474. static void DrawDisplayFull(void)
  475. {
  476. struct PicoEState *est = &Pico.est;
  477. struct PicoVideo *pvid=&est->Pico->video;
  478. int win, edge=0, hvwin=0; // LSb->MSb: hwin&plane, vwin&plane, full
  479. int scrstart=START_ROW, scrend = END_ROW; // our render area
  480. int planestart, planeend; // plane A start/end when window shares display with plane A (in tile rows or columns)
  481. int winstart, winend; // same for window
  482. int maxw, maxcolc; // max width and col cells
  483. if(est->rendstatus & PDRAW_32_COLS) {
  484. maxw = 264; maxcolc = 32;
  485. } else {
  486. maxw = 328; maxcolc = 40;
  487. }
  488. if(est->rendstatus & PDRAW_30_ROWS) {
  489. // In 240 line mode, the top and bottom 8 lines are omitted
  490. // since this renderer always renderers 224 lines
  491. scrstart ++, scrend ++;
  492. }
  493. est->Draw2Start = scrstart;
  494. planestart = scrstart, planeend = scrend;
  495. winstart = scrstart, winend = scrend;
  496. // 32C border for centering? (for asm)
  497. est->rendstatus &= ~PDRAW_BORDER_32;
  498. if ((est->rendstatus&PDRAW_32_COLS) && !(PicoIn.opt&POPT_DIS_32C_BORDER))
  499. est->rendstatus |= PDRAW_BORDER_32;
  500. // horizontal window?
  501. if ((win=pvid->reg[0x12]))
  502. {
  503. hvwin=1; // hwindow shares display with plane A
  504. edge=win&0x1f;
  505. if(win == 0x80) {
  506. // fullscreen window
  507. hvwin=4;
  508. } else if(win < 0x80) {
  509. // window on the top
  510. if(edge <= scrstart) hvwin=0; // window not visible in our drawing region
  511. else if(edge >= scrend) hvwin=4;
  512. else planestart = winend = edge;
  513. } else if(win > 0x80) {
  514. // window at the bottom
  515. if(edge >= scrend) hvwin=0;
  516. else planeend = winstart = edge;
  517. }
  518. }
  519. // check for vertical window, but only if win is not fullscreen
  520. if (hvwin != 4)
  521. {
  522. win=pvid->reg[0x11];
  523. edge=win&0x1f;
  524. if (win&0x80) {
  525. if(!edge) hvwin=4;
  526. else if(edge < (maxcolc>>1)) {
  527. // window is on the right
  528. hvwin|=2;
  529. planeend|=edge<<17;
  530. winstart|=edge<<17;
  531. winend|=maxcolc<<16;
  532. }
  533. } else {
  534. if(edge >= (maxcolc>>1)) hvwin=4;
  535. else if(edge) {
  536. // window is on the left
  537. hvwin|=2;
  538. winend|=edge<<17;
  539. planestart|=edge<<17;
  540. planeend|=maxcolc<<16;
  541. }
  542. }
  543. }
  544. if (hvwin==1) { winend|=maxcolc<<16; planeend|=maxcolc<<16; }
  545. HighCache2A[0] = HighCache2B[0] = 0;
  546. if (!(pvid->debug_p & PVD_KILL_B))
  547. DrawLayerFull(1, HighCache2B, scrstart, (maxcolc<<16)|scrend, est);
  548. if (!(pvid->debug_p & PVD_KILL_A)) switch (hvwin)
  549. {
  550. case 4:
  551. // fullscreen window
  552. DrawWindowFull(scrstart, (maxcolc<<16)|scrend, 0, est);
  553. break;
  554. case 3:
  555. // we have plane A and both v and h windows
  556. DrawLayerFull(0, HighCache2A, planestart, planeend, est);
  557. DrawWindowFull( winstart&~0xff0000, (winend&~0xff0000)|(maxcolc<<16), 0, est); // h
  558. DrawWindowFull((winstart&~0xff)|scrstart, (winend&~0xff)|scrend, 0, est); // v
  559. break;
  560. case 2:
  561. case 1:
  562. // both window and plane A visible, window is vertical XOR horizontal
  563. DrawLayerFull(0, HighCache2A, planestart, planeend, est);
  564. DrawWindowFull(winstart, winend, 0, est);
  565. break;
  566. default:
  567. // fullscreen plane A
  568. DrawLayerFull(0, HighCache2A, scrstart, (maxcolc<<16)|scrend, est);
  569. break;
  570. }
  571. if (!(pvid->debug_p & PVD_KILL_S_LO))
  572. DrawAllSpritesFull(0, maxw, est);
  573. if (HighCache2B[0]) DrawTilesFromCacheF(HighCache2B, est);
  574. if (HighCache2A[0]) DrawTilesFromCacheF(HighCache2A, est);
  575. if (!(pvid->debug_p & PVD_KILL_A)) switch (hvwin)
  576. {
  577. case 4:
  578. // fullscreen window
  579. DrawWindowFull(scrstart, (maxcolc<<16)|scrend, 1, est);
  580. break;
  581. case 3:
  582. // we have plane A and both v and h windows
  583. DrawWindowFull( winstart&~0xff0000, (winend&~0xff0000)|(maxcolc<<16), 1, est); // h
  584. DrawWindowFull((winstart&~0xff)|scrstart, (winend&~0xff)|scrend, 1, est); // v
  585. break;
  586. case 2:
  587. case 1:
  588. // both window and plane A visible, window is vertical XOR horizontal
  589. DrawWindowFull(winstart, winend, 1, est);
  590. break;
  591. }
  592. if (!(pvid->debug_p & PVD_KILL_S_HI))
  593. DrawAllSpritesFull(1, maxw, est);
  594. }
  595. PICO_INTERNAL void PicoFrameFull()
  596. {
  597. pprof_start(draw);
  598. // prepare cram?
  599. if (PicoPrepareCram) PicoPrepareCram();
  600. // Draw screen:
  601. BackFillFull(Pico.est.Draw2FB, Pico.video.reg[7]);
  602. if (Pico.video.reg[1] & 0x40)
  603. DrawDisplayFull();
  604. // clear top and bottom 8 lines in 240 mode, since draw2 only does 224
  605. if (Pico.est.rendstatus & PDRAW_30_ROWS) {
  606. unsigned char *pd = Pico.est.Draw2FB;
  607. int i;
  608. for (i = 8; i > 0; i--, pd += Pico.est.Draw2Width)
  609. memset32((int *)pd, 0xe0e0e0e0, 328/4);
  610. pd += Pico.est.Draw2Width*(END_ROW-START_ROW)*8;
  611. for (i = 8; i > 0; i--, pd += Pico.est.Draw2Width)
  612. memset32((int *)pd, 0xe0e0e0e0, 328/4);
  613. }
  614. pprof_end(draw);
  615. }
  616. void PicoDraw2SetOutBuf(void *dest, int incr)
  617. {
  618. if (dest) {
  619. Pico.est.Draw2FB = dest;
  620. Pico.est.Draw2Width = incr;
  621. } else {
  622. Pico.est.Draw2FB = PicoDraw2FB_;
  623. Pico.est.Draw2Width = LINE_WIDTH;
  624. }
  625. }
  626. void PicoDraw2Init(void)
  627. {
  628. PicoDraw2SetOutBuf(NULL, 0);
  629. }