Draw.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. // This is part of Pico Library
  2. // (c) Copyright 2004 Dave, All rights reserved.
  3. // (c) Copyright 2006 notaz, All rights reserved.
  4. // Free for non-commercial use.
  5. // For commercial use, separate licencing terms must be obtained.
  6. #include "PicoInt.h"
  7. int (*PicoScan)(unsigned int num, void *data)=NULL;
  8. #if OVERRIDE_HIGHCOL
  9. static unsigned char DefHighCol[8+320+8];
  10. unsigned char *HighCol=DefHighCol;
  11. #else
  12. unsigned char HighCol[8+320+8];
  13. #endif
  14. unsigned short DefOutBuff[320*2];
  15. void *DrawLineDest=DefOutBuff; // pointer to dest buffer where to draw this line to
  16. static int HighCacheA[41+1]; // caches for high layers
  17. static int HighCacheB[41+1];
  18. static int HighCacheS[80+1]; // and sprites
  19. static int HighPreSpr[80*2+1]; // slightly preprocessed sprites
  20. char HighSprZ[320+8+8]; // Z-buffer for accurate sprites and shadow/hilight mode
  21. // (if bit 7 == 0, sh caused by tile; if bit 6 == 0 pixel must be shadowed, else hilighted, if bit5 == 1)
  22. // lsb->msb: moved sprites, not all window tiles use same priority, accurate sprites (copied from PicoOpt), interlace
  23. // dirty sprites, sonic mode, have layer with all hi prio tiles (mk3), layer sh/hi already processed
  24. int rendstatus;
  25. int Scanline=0; // Scanline
  26. static int SpriteBlocks;
  27. //unsigned short ppt[] = { 0x0f11, 0x0ff1, 0x01f1, 0x011f, 0x01ff, 0x0f1f, 0x0f0e, 0x0e7c };
  28. struct TileStrip
  29. {
  30. int nametab; // Position in VRAM of name table (for this tile line)
  31. int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
  32. int hscroll; // Horizontal scroll value in pixels for the line
  33. int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
  34. int *hc; // cache for high tile codes and their positions
  35. int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
  36. };
  37. // stuff available in asm:
  38. #ifdef _ASM_DRAW_C
  39. void DrawWindow(int tstart, int tend, int prio, int sh);
  40. void BackFill(int reg7, int sh);
  41. void DrawSprite(int *sprite, int **hc, int sh);
  42. void DrawTilesFromCache(int *hc, int sh, int rlim);
  43. void DrawSpritesFromCache(int *hc, int sh);
  44. void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells);
  45. void FinalizeLineBGR444(int sh);
  46. void FinalizeLineRGB555(int sh);
  47. void blockcpy_or(void *dst, void *src, size_t n, int pat);
  48. #else
  49. // utility
  50. void blockcpy_or(void *dst, void *src, size_t n, int pat)
  51. {
  52. unsigned char *pd = dst, *ps = src;
  53. for (; n; n--)
  54. *pd++ = (unsigned char) (*ps++ | pat);
  55. }
  56. #endif
  57. #ifdef _ASM_DRAW_C_MIPS
  58. int TileNorm(int sx,int addr,int pal);
  59. int TileFlip(int sx,int addr,int pal);
  60. #else
  61. static int TileNorm(int sx,int addr,int pal)
  62. {
  63. unsigned char *pd = HighCol+sx;
  64. unsigned int pack=0; unsigned int t=0;
  65. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  66. if (pack)
  67. {
  68. t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
  69. t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
  70. t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
  71. t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
  72. t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
  73. t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
  74. t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
  75. t=pack&0x000f0000; if (t) pd[7]=(unsigned char)(pal|(t>>16));
  76. return 0;
  77. }
  78. return 1; // Tile blank
  79. }
  80. static int TileFlip(int sx,int addr,int pal)
  81. {
  82. unsigned char *pd = HighCol+sx;
  83. unsigned int pack=0; unsigned int t=0;
  84. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  85. if (pack)
  86. {
  87. t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
  88. t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
  89. t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
  90. t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
  91. t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
  92. t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
  93. t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8));
  94. t=pack&0x0000f000; if (t) pd[7]=(unsigned char)(pal|(t>>12));
  95. return 0;
  96. }
  97. return 1; // Tile blank
  98. }
  99. #endif
  100. // tile renderers for hacky operator sprite support
  101. #define sh_pix(x) \
  102. if(!t); \
  103. else if(t==0xe) pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ \
  104. else if(t==0xf) pd[x]=(unsigned char)((pd[x]&0x3f)|0xc0); /* shadow */ \
  105. else pd[x]=(unsigned char)(pal|t)
  106. #ifndef _ASM_DRAW_C
  107. static int TileNormSH(int sx,int addr,int pal)
  108. {
  109. unsigned int pack=0; unsigned int t=0;
  110. unsigned char *pd = HighCol+sx;
  111. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  112. if (pack)
  113. {
  114. t=(pack&0x0000f000)>>12; sh_pix(0);
  115. t=(pack&0x00000f00)>> 8; sh_pix(1);
  116. t=(pack&0x000000f0)>> 4; sh_pix(2);
  117. t=(pack&0x0000000f) ; sh_pix(3);
  118. t=(pack&0xf0000000)>>28; sh_pix(4);
  119. t=(pack&0x0f000000)>>24; sh_pix(5);
  120. t=(pack&0x00f00000)>>20; sh_pix(6);
  121. t=(pack&0x000f0000)>>16; sh_pix(7);
  122. return 0;
  123. }
  124. return 1; // Tile blank
  125. }
  126. static int TileFlipSH(int sx,int addr,int pal)
  127. {
  128. unsigned int pack=0; unsigned int t=0;
  129. unsigned char *pd = HighCol+sx;
  130. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  131. if (pack)
  132. {
  133. t=(pack&0x000f0000)>>16; sh_pix(0);
  134. t=(pack&0x00f00000)>>20; sh_pix(1);
  135. t=(pack&0x0f000000)>>24; sh_pix(2);
  136. t=(pack&0xf0000000)>>28; sh_pix(3);
  137. t=(pack&0x0000000f) ; sh_pix(4);
  138. t=(pack&0x000000f0)>> 4; sh_pix(5);
  139. t=(pack&0x00000f00)>> 8; sh_pix(6);
  140. t=(pack&0x0000f000)>>12; sh_pix(7);
  141. return 0;
  142. }
  143. return 1; // Tile blank
  144. }
  145. #endif
  146. static int TileNormZ(int sx,int addr,int pal,int zval)
  147. {
  148. unsigned int pack=0; unsigned int t=0;
  149. unsigned char *pd = HighCol+sx;
  150. char *zb = HighSprZ+sx;
  151. int collision = 0, zb_s;
  152. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  153. if (pack)
  154. {
  155. t=pack&0x0000f000; if(t) { zb_s=zb[0]; if(zb_s) collision=1; if(zval>zb_s) { pd[0]=(unsigned char)(pal|(t>>12)); zb[0]=(char)zval; } }
  156. t=pack&0x00000f00; if(t) { zb_s=zb[1]; if(zb_s) collision=1; if(zval>zb_s) { pd[1]=(unsigned char)(pal|(t>> 8)); zb[1]=(char)zval; } }
  157. t=pack&0x000000f0; if(t) { zb_s=zb[2]; if(zb_s) collision=1; if(zval>zb_s) { pd[2]=(unsigned char)(pal|(t>> 4)); zb[2]=(char)zval; } }
  158. t=pack&0x0000000f; if(t) { zb_s=zb[3]; if(zb_s) collision=1; if(zval>zb_s) { pd[3]=(unsigned char)(pal|(t )); zb[3]=(char)zval; } }
  159. t=pack&0xf0000000; if(t) { zb_s=zb[4]; if(zb_s) collision=1; if(zval>zb_s) { pd[4]=(unsigned char)(pal|(t>>28)); zb[4]=(char)zval; } }
  160. t=pack&0x0f000000; if(t) { zb_s=zb[5]; if(zb_s) collision=1; if(zval>zb_s) { pd[5]=(unsigned char)(pal|(t>>24)); zb[5]=(char)zval; } }
  161. t=pack&0x00f00000; if(t) { zb_s=zb[6]; if(zb_s) collision=1; if(zval>zb_s) { pd[6]=(unsigned char)(pal|(t>>20)); zb[6]=(char)zval; } }
  162. t=pack&0x000f0000; if(t) { zb_s=zb[7]; if(zb_s) collision=1; if(zval>zb_s) { pd[7]=(unsigned char)(pal|(t>>16)); zb[7]=(char)zval; } }
  163. if(collision) Pico.video.status|=0x20;
  164. return 0;
  165. }
  166. return 1; // Tile blank
  167. }
  168. static int TileFlipZ(int sx,int addr,int pal,int zval)
  169. {
  170. unsigned int pack=0; unsigned int t=0;
  171. unsigned char *pd = HighCol+sx;
  172. char *zb = HighSprZ+sx;
  173. int collision = 0, zb_s;
  174. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  175. if (pack)
  176. {
  177. t=pack&0x000f0000; if(t) { zb_s=zb[0]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[0]=(unsigned char)(pal|(t>>16)); zb[0]=(char)zval; } }
  178. t=pack&0x00f00000; if(t) { zb_s=zb[1]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[1]=(unsigned char)(pal|(t>>20)); zb[1]=(char)zval; } }
  179. t=pack&0x0f000000; if(t) { zb_s=zb[2]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[2]=(unsigned char)(pal|(t>>24)); zb[2]=(char)zval; } }
  180. t=pack&0xf0000000; if(t) { zb_s=zb[3]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[3]=(unsigned char)(pal|(t>>28)); zb[3]=(char)zval; } }
  181. t=pack&0x0000000f; if(t) { zb_s=zb[4]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[4]=(unsigned char)(pal|(t )); zb[4]=(char)zval; } }
  182. t=pack&0x000000f0; if(t) { zb_s=zb[5]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[5]=(unsigned char)(pal|(t>> 4)); zb[5]=(char)zval; } }
  183. t=pack&0x00000f00; if(t) { zb_s=zb[6]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[6]=(unsigned char)(pal|(t>> 8)); zb[6]=(char)zval; } }
  184. t=pack&0x0000f000; if(t) { zb_s=zb[7]&0x1f; if(zb_s) collision=1; if(zval>zb_s) { pd[7]=(unsigned char)(pal|(t>>12)); zb[7]=(char)zval; } }
  185. if(collision) Pico.video.status|=0x20;
  186. return 0;
  187. }
  188. return 1; // Tile blank
  189. }
  190. #define sh_pixZ(x) \
  191. if(t) { \
  192. if(zb[x]) collision=1; \
  193. if(zval>zb[x]) { \
  194. if (t==0xe) { pd[x]=(unsigned char)((pd[x]&0x3f)|0x80); /* hilight */ } \
  195. else if(t==0xf) { pd[x]=(unsigned char)((pd[x]&0x3f)|0xc0); /* shadow */ } \
  196. else { zb[x]=(char)zval; pd[x]=(unsigned char)(pal|t); } \
  197. } \
  198. }
  199. static int TileNormZSH(int sx,int addr,int pal,int zval)
  200. {
  201. unsigned int pack=0; unsigned int t=0;
  202. unsigned char *pd = HighCol+sx;
  203. char *zb = HighSprZ+sx;
  204. int collision = 0;
  205. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  206. if (pack)
  207. {
  208. t=(pack&0x0000f000)>>12; sh_pixZ(0);
  209. t=(pack&0x00000f00)>> 8; sh_pixZ(1);
  210. t=(pack&0x000000f0)>> 4; sh_pixZ(2);
  211. t=(pack&0x0000000f) ; sh_pixZ(3);
  212. t=(pack&0xf0000000)>>28; sh_pixZ(4);
  213. t=(pack&0x0f000000)>>24; sh_pixZ(5);
  214. t=(pack&0x00f00000)>>20; sh_pixZ(6);
  215. t=(pack&0x000f0000)>>16; sh_pixZ(7);
  216. if(collision) Pico.video.status|=0x20;
  217. return 0;
  218. }
  219. return 1; // Tile blank
  220. }
  221. static int TileFlipZSH(int sx,int addr,int pal,int zval)
  222. {
  223. unsigned int pack=0; unsigned int t=0;
  224. unsigned char *pd = HighCol+sx;
  225. char *zb = HighSprZ+sx;
  226. int collision = 0;
  227. pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  228. if (pack)
  229. {
  230. t=(pack&0x000f0000)>>16; sh_pixZ(0);
  231. t=(pack&0x00f00000)>>20; sh_pixZ(1);
  232. t=(pack&0x0f000000)>>24; sh_pixZ(2);
  233. t=(pack&0xf0000000)>>28; sh_pixZ(3);
  234. t=(pack&0x0000000f) ; sh_pixZ(4);
  235. t=(pack&0x000000f0)>> 4; sh_pixZ(5);
  236. t=(pack&0x00000f00)>> 8; sh_pixZ(6);
  237. t=(pack&0x0000f000)>>12; sh_pixZ(7);
  238. if(collision) Pico.video.status|=0x20;
  239. return 0;
  240. }
  241. return 1; // Tile blank
  242. }
  243. // --------------------------------------------
  244. #ifndef _ASM_DRAW_C
  245. static void DrawStrip(struct TileStrip *ts, int plane_sh, int cellskip)
  246. {
  247. int tilex,dx,ty,code=0,addr=0,cells;
  248. int oldcode=-1,blank=-1; // The tile we know is blank
  249. int pal=0,sh;
  250. // Draw tiles across screen:
  251. sh=(plane_sh<<5)&0x40;
  252. tilex=((-ts->hscroll)>>3)+cellskip;
  253. ty=(ts->line&7)<<1; // Y-Offset into tile
  254. dx=((ts->hscroll-1)&7)+1;
  255. cells = ts->cells - cellskip;
  256. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  257. dx+=cellskip<<3;
  258. for (; cells > 0; dx+=8,tilex++,cells--)
  259. {
  260. int zero=0;
  261. code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
  262. if (code==blank) continue;
  263. if (code>>15) { // high priority tile
  264. int cval = code | (dx<<16) | (ty<<25);
  265. if(code&0x1000) cval^=7<<26;
  266. *ts->hc++ = cval; // cache it
  267. continue;
  268. }
  269. if (code!=oldcode) {
  270. oldcode = code;
  271. // Get tile address/2:
  272. addr=(code&0x7ff)<<4;
  273. addr+=ty;
  274. if (code&0x1000) addr^=0xe; // Y-flip
  275. pal=((code>>9)&0x30)|sh;
  276. }
  277. if (code&0x0800) zero=TileFlip(dx,addr,pal);
  278. else zero=TileNorm(dx,addr,pal);
  279. if (zero) blank=code; // We know this tile is blank now
  280. }
  281. // terminate the cache list
  282. *ts->hc = 0;
  283. // if oldcode wasn't changed, it means all layer is hi priority
  284. if (oldcode == -1) rendstatus|=0x40;
  285. }
  286. // this is messy
  287. void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
  288. {
  289. int tilex,dx,code=0,addr=0,cell=0;
  290. int oldcode=-1,blank=-1; // The tile we know is blank
  291. int pal=0,scan=Scanline;
  292. // Draw tiles across screen:
  293. tilex=(-ts->hscroll)>>3;
  294. dx=((ts->hscroll-1)&7)+1;
  295. if(dx != 8) cell--; // have hscroll, start with negative cell
  296. cell+=cellskip;
  297. tilex+=cellskip;
  298. dx+=cellskip<<3;
  299. for (; cell < ts->cells; dx+=8,tilex++,cell++)
  300. {
  301. int zero=0,nametabadd,ty;
  302. //if((cell&1)==0)
  303. {
  304. int line,vscroll;
  305. vscroll=Pico.vsram[(plane_sh&1)+(cell&~1)];
  306. // Find the line in the name table
  307. line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
  308. nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
  309. ty=(line&7)<<1; // Y-Offset into tile
  310. }
  311. code=Pico.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
  312. if (code==blank) continue;
  313. if (code>>15) { // high priority tile
  314. int cval = code | (dx<<16) | (ty<<25);
  315. if(code&0x1000) cval^=7<<26;
  316. *ts->hc++ = cval; // cache it
  317. continue;
  318. }
  319. if (code!=oldcode) {
  320. oldcode = code;
  321. // Get tile address/2:
  322. addr=(code&0x7ff)<<4;
  323. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  324. pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
  325. }
  326. if (code&0x0800) zero=TileFlip(dx,addr,pal);
  327. else zero=TileNorm(dx,addr,pal);
  328. if (zero) blank=code; // We know this tile is blank now
  329. }
  330. // terminate the cache list
  331. *ts->hc = 0;
  332. if (oldcode == -1) rendstatus|=0x40;
  333. }
  334. #endif
  335. #ifndef _ASM_DRAW_C
  336. static
  337. #endif
  338. void DrawStripInterlace(struct TileStrip *ts)
  339. {
  340. int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
  341. int oldcode=-1,blank=-1; // The tile we know is blank
  342. int pal=0;
  343. // Draw tiles across screen:
  344. tilex=(-ts->hscroll)>>3;
  345. ty=(ts->line&15)<<1; // Y-Offset into tile
  346. dx=((ts->hscroll-1)&7)+1;
  347. cells = ts->cells;
  348. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  349. for (; cells; dx+=8,tilex++,cells--)
  350. {
  351. int zero=0;
  352. code=Pico.vram[ts->nametab+(tilex&ts->xmask)];
  353. if (code==blank) continue;
  354. if (code>>15) { // high priority tile
  355. int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
  356. cval|=(code&0x3ff)<<1;
  357. if(code&0x1000) cval^=0xf<<26;
  358. *ts->hc++ = cval; // cache it
  359. continue;
  360. }
  361. if (code!=oldcode) {
  362. oldcode = code;
  363. // Get tile address/2:
  364. addr=(code&0x7ff)<<5;
  365. if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
  366. // pal=Pico.cram+((code>>9)&0x30);
  367. pal=((code>>9)&0x30);
  368. }
  369. if (code&0x0800) zero=TileFlip(dx,addr,pal);
  370. else zero=TileNorm(dx,addr,pal);
  371. if (zero) blank=code; // We know this tile is blank now
  372. }
  373. // terminate the cache list
  374. *ts->hc = 0;
  375. }
  376. // --------------------------------------------
  377. #ifndef _ASM_DRAW_C
  378. static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells)
  379. {
  380. struct PicoVideo *pvid=&Pico.video;
  381. const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
  382. struct TileStrip ts;
  383. int width, height, ymask;
  384. int vscroll, htab;
  385. ts.hc=hcache;
  386. ts.cells=maxcells;
  387. // Work out the TileStrip to draw
  388. // Work out the name table size: 32 64 or 128 tiles (0-3)
  389. width=pvid->reg[16];
  390. height=(width>>4)&3; width&=3;
  391. ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
  392. ymask=(height<<8)|0xff; // Y Mask in pixels
  393. if(width == 1) ymask&=0x1ff;
  394. else if(width>1) ymask =0x0ff;
  395. // Find name table:
  396. if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
  397. else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
  398. htab=pvid->reg[13]<<9; // Horizontal scroll table address
  399. if ( pvid->reg[11]&2) htab+=Scanline<<1; // Offset by line
  400. if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
  401. htab+=plane_sh&1; // A or B
  402. // Get horizontal scroll value, will be masked later
  403. ts.hscroll=Pico.vram[htab&0x7fff];
  404. if((pvid->reg[12]&6) == 6) {
  405. // interlace mode 2
  406. vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
  407. // Find the line in the name table
  408. ts.line=(vscroll+(Scanline<<1))&((ymask<<1)|1);
  409. ts.nametab+=(ts.line>>4)<<shift[width];
  410. DrawStripInterlace(&ts);
  411. } else if( pvid->reg[11]&4) {
  412. // shit, we have 2-cell column based vscroll
  413. // luckily this doesn't happen too often
  414. ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
  415. DrawStripVSRam(&ts, plane_sh, cellskip);
  416. } else {
  417. vscroll=Pico.vsram[plane_sh&1]; // Get vertical scroll value
  418. // Find the line in the name table
  419. ts.line=(vscroll+Scanline)&ymask;
  420. ts.nametab+=(ts.line>>3)<<shift[width];
  421. DrawStrip(&ts, plane_sh, cellskip);
  422. }
  423. }
  424. // --------------------------------------------
  425. // tstart & tend are tile pair numbers
  426. static void DrawWindow(int tstart, int tend, int prio, int sh) // int *hcache
  427. {
  428. struct PicoVideo *pvid=&Pico.video;
  429. int tilex=0,ty=0,nametab,code=0;
  430. int blank=-1; // The tile we know is blank
  431. // Find name table line:
  432. if (pvid->reg[12]&1)
  433. {
  434. nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
  435. nametab+=(Scanline>>3)<<6;
  436. }
  437. else
  438. {
  439. nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
  440. nametab+=(Scanline>>3)<<5;
  441. }
  442. tilex=tstart<<1;
  443. tend<<=1;
  444. ty=(Scanline&7)<<1; // Y-Offset into tile
  445. if(!(rendstatus&2)) {
  446. // check the first tile code
  447. code=Pico.vram[nametab+tilex];
  448. // if the whole window uses same priority (what is often the case), we may be able to skip this field
  449. if((code>>15) != prio) return;
  450. }
  451. // Draw tiles across screen:
  452. for (; tilex < tend; tilex++)
  453. {
  454. int addr=0,zero=0;
  455. int pal;
  456. code=Pico.vram[nametab+tilex];
  457. if(code==blank) continue;
  458. if((code>>15) != prio) {
  459. rendstatus|=2;
  460. continue;
  461. }
  462. pal=((code>>9)&0x30);
  463. if(sh) {
  464. int tmp, *zb = (int *)(HighCol+8+(tilex<<3));
  465. if(prio) {
  466. tmp = *zb;
  467. if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
  468. if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
  469. *zb++=tmp; tmp = *zb;
  470. if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
  471. if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
  472. *zb++=tmp;
  473. } else {
  474. pal |= 0x40;
  475. }
  476. }
  477. // Get tile address/2:
  478. addr=(code&0x7ff)<<4;
  479. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  480. if (code&0x0800) zero=TileFlip(8+(tilex<<3),addr,pal);
  481. else zero=TileNorm(8+(tilex<<3),addr,pal);
  482. if (zero) blank=code; // We know this tile is blank now
  483. }
  484. // terminate the cache list
  485. //*hcache = 0;
  486. }
  487. // --------------------------------------------
  488. static void DrawTilesFromCache(int *hc, int sh, int rlim)
  489. {
  490. int code, addr, dx;
  491. int pal;
  492. // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
  493. if (sh && (rendstatus&0xc0))
  494. {
  495. if (!(rendstatus&0x80))
  496. {
  497. // as some layer has covered whole line with hi priority tiles,
  498. // we can process whole line and then act as if sh/hi mode was off.
  499. rendstatus|=0x80;
  500. int c = 320/4, *zb = (int *)(HighCol+8);
  501. while (c--)
  502. {
  503. int tmp = *zb;
  504. if (!(tmp & 0x80808080)) *zb=tmp&0x3f3f3f3f;
  505. else {
  506. if(!(tmp&0x00000080)) tmp&=~0x000000c0; if(!(tmp&0x00008000)) tmp&=~0x0000c000;
  507. if(!(tmp&0x00800000)) tmp&=~0x00c00000; if(!(tmp&0x80000000)) tmp&=~0xc0000000;
  508. *zb=tmp;
  509. }
  510. zb++;
  511. }
  512. }
  513. sh = 0;
  514. }
  515. if (sh)
  516. {
  517. while ((code=*hc++)) {
  518. unsigned char *zb;
  519. // Get tile address/2:
  520. addr=(code&0x7ff)<<4;
  521. addr+=(unsigned int)code>>25; // y offset into tile
  522. dx=(code>>16)&0x1ff;
  523. zb = HighCol+dx;
  524. if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
  525. if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
  526. if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
  527. if(!(*zb&0x80)) *zb&=0x3f; zb++; if(!(*zb&0x80)) *zb&=0x3f; zb++;
  528. pal=((code>>9)&0x30);
  529. if (rlim-dx < 0) goto last_cut_tile;
  530. if (code&0x0800) TileFlip(dx,addr,pal);
  531. else TileNorm(dx,addr,pal);
  532. }
  533. }
  534. else
  535. {
  536. short blank=-1; // The tile we know is blank
  537. while ((code=*hc++)) {
  538. int zero;
  539. if((short)code == blank) continue;
  540. // Get tile address/2:
  541. addr=(code&0x7ff)<<4;
  542. addr+=(unsigned int)code>>25; // y offset into tile
  543. dx=(code>>16)&0x1ff;
  544. pal=((code>>9)&0x30);
  545. if (rlim-dx < 0) goto last_cut_tile;
  546. if (code&0x0800) zero=TileFlip(dx,addr,pal);
  547. else zero=TileNorm(dx,addr,pal);
  548. if (zero) blank=(short)code;
  549. }
  550. }
  551. return;
  552. last_cut_tile:
  553. {
  554. unsigned int t, pack=*(unsigned int *)(Pico.vram+addr); // Get 8 pixels
  555. unsigned char *pd = HighCol+dx;
  556. if (!pack) return;
  557. if (code&0x0800)
  558. {
  559. switch (rlim-dx+8)
  560. {
  561. case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
  562. case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
  563. case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
  564. case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
  565. case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
  566. case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
  567. case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
  568. default: break;
  569. }
  570. }
  571. else
  572. {
  573. switch (rlim-dx+8)
  574. {
  575. case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
  576. case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
  577. case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
  578. case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
  579. case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
  580. case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
  581. case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
  582. default: break;
  583. }
  584. }
  585. }
  586. }
  587. // --------------------------------------------
  588. // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
  589. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  590. static void DrawSprite(int *sprite, int **hc, int sh)
  591. {
  592. int width=0,height=0;
  593. int row=0,code=0;
  594. int pal;
  595. int tile=0,delta=0;
  596. int sx, sy;
  597. int (*fTileFunc)(int sx,int addr,int pal);
  598. // parse the sprite data
  599. sy=sprite[0];
  600. code=sprite[1];
  601. sx=code>>16; // X
  602. width=sy>>28;
  603. height=(sy>>24)&7; // Width and height in tiles
  604. sy=(sy<<16)>>16; // Y
  605. row=Scanline-sy; // Row of the sprite we are on
  606. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  607. tile=code&0x7ff; // Tile number
  608. tile+=row>>3; // Tile number increases going down
  609. delta=height; // Delta to increase tile by going right
  610. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  611. tile<<=4; tile+=(row&7)<<1; // Tile address
  612. if(code&0x8000) { // high priority - cache it
  613. *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>16)&0xf);
  614. } else {
  615. delta<<=4; // Delta of address
  616. pal=((code>>9)&0x30)|(sh<<6);
  617. if(sh && (code&0x6000) == 0x6000) {
  618. if(code&0x0800) fTileFunc=TileFlipSH;
  619. else fTileFunc=TileNormSH;
  620. } else {
  621. if(code&0x0800) fTileFunc=TileFlip;
  622. else fTileFunc=TileNorm;
  623. }
  624. for (; width; width--,sx+=8,tile+=delta)
  625. {
  626. if(sx<=0) continue;
  627. if(sx>=328) break; // Offscreen
  628. tile&=0x7fff; // Clip tile address
  629. fTileFunc(sx,tile,pal);
  630. }
  631. }
  632. }
  633. #endif
  634. // Index + 0 : hhhhvvvv s---hhvv yyyyyyyy yyyyyyyy // s: skip flag, h: horiz. size
  635. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  636. static void DrawSpriteZ(int pack, int pack2, int shpri, int sprio)
  637. {
  638. int width=0,height=0;
  639. int row=0;
  640. int pal;
  641. int tile=0,delta=0;
  642. int sx, sy;
  643. int (*fTileFunc)(int sx,int addr,int pal,int zval);
  644. // parse the sprite data
  645. sx=pack2>>16; // X
  646. sy=(pack <<16)>>16; // Y
  647. width=pack>>28;
  648. height=(pack>>24)&7; // Width and height in tiles
  649. row=Scanline-sy; // Row of the sprite we are on
  650. if (pack2&0x1000) row=(height<<3)-1-row; // Flip Y
  651. tile=pack2&0x7ff; // Tile number
  652. tile+=row>>3; // Tile number increases going down
  653. delta=height; // Delta to increase tile by going right
  654. if (pack2&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  655. tile<<=4; tile+=(row&7)<<1; // Tile address
  656. delta<<=4; // Delta of address
  657. pal=((pack2>>9)&0x30);
  658. if((shpri&1)&&!(shpri&2)) pal|=0x40;
  659. shpri&=1;
  660. if((pack2&0x6000) != 0x6000) shpri = 0;
  661. shpri |= (pack2&0x0800)>>10;
  662. switch(shpri) {
  663. default:
  664. case 0: fTileFunc=TileNormZ; break;
  665. case 1: fTileFunc=TileNormZSH; break;
  666. case 2: fTileFunc=TileFlipZ; break;
  667. case 3: fTileFunc=TileFlipZSH; break;
  668. }
  669. for (; width; width--,sx+=8,tile+=delta)
  670. {
  671. if(sx<=0) continue;
  672. if(sx>=328) break; // Offscreen
  673. tile&=0x7fff; // Clip tile address
  674. fTileFunc(sx,tile,pal,sprio);
  675. }
  676. }
  677. static void DrawSpriteInterlace(unsigned int *sprite)
  678. {
  679. int width=0,height=0;
  680. int row=0,code=0;
  681. int pal;
  682. int tile=0,delta=0;
  683. int sx, sy;
  684. // parse the sprite data
  685. sy=sprite[0];
  686. height=sy>>24;
  687. sy=(sy&0x3ff)-0x100; // Y
  688. width=(height>>2)&3; height&=3;
  689. width++; height++; // Width and height in tiles
  690. row=(Scanline<<1)-sy; // Row of the sprite we are on
  691. code=sprite[1];
  692. sx=((code>>16)&0x1ff)-0x78; // X
  693. if (code&0x1000) row^=(16<<height)-1; // Flip Y
  694. tile=code&0x3ff; // Tile number
  695. tile+=row>>4; // Tile number increases going down
  696. delta=height; // Delta to increase tile by going right
  697. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  698. tile<<=5; tile+=(row&15)<<1; // Tile address
  699. delta<<=5; // Delta of address
  700. pal=((code>>9)&0x30); // Get palette pointer
  701. for (; width; width--,sx+=8,tile+=delta)
  702. {
  703. if(sx<=0) continue;
  704. if(sx>=328) break; // Offscreen
  705. tile&=0x7fff; // Clip tile address
  706. if (code&0x0800) TileFlip(sx,tile,pal);
  707. else TileNorm(sx,tile,pal);
  708. }
  709. }
  710. static void DrawAllSpritesInterlace(int pri, int maxwidth)
  711. {
  712. struct PicoVideo *pvid=&Pico.video;
  713. int i,u,table,link=0,sline=Scanline<<1;
  714. unsigned int *sprites[80]; // Sprite index
  715. table=pvid->reg[5]&0x7f;
  716. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  717. table<<=8; // Get sprite table address/2
  718. for (i=u=0; u < 80 && i < 21; u++)
  719. {
  720. unsigned int *sprite;
  721. int code, sx, sy, height;
  722. sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  723. // get sprite info
  724. code = sprite[0];
  725. sx = sprite[1];
  726. if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
  727. // check if it is on this line
  728. sy = (code&0x3ff)-0x100;
  729. height = (((code>>24)&3)+1)<<4;
  730. if(sline < sy || sline >= sy+height) goto nextsprite; // no
  731. // check if sprite is not hidden offscreen
  732. sx = (sx>>16)&0x1ff;
  733. sx -= 0x78; // Get X coordinate + 8
  734. if(sx <= -8*3 || sx >= maxwidth) goto nextsprite;
  735. // sprite is good, save it's pointer
  736. sprites[i++]=sprite;
  737. nextsprite:
  738. // Find next sprite
  739. link=(code>>16)&0x7f;
  740. if(!link) break; // End of sprites
  741. }
  742. // Go through sprites backwards:
  743. for (i-- ;i>=0; i--)
  744. DrawSpriteInterlace(sprites[i]);
  745. }
  746. #ifndef _ASM_DRAW_C
  747. static void DrawSpritesFromCache(int *hc, int sh)
  748. {
  749. int code, tile, sx, delta, width;
  750. int pal;
  751. int (*fTileFunc)(int sx,int addr,int pal);
  752. // *(*hc)++ = (tile<<16)|((code&0x0800)<<5)|((sx<<6)&0x0000ffc0)|((code>>9)&0x30)|((sprite[0]>>24)&0xf);
  753. while((code=*hc++)) {
  754. pal=(code&0x30);
  755. delta=code&0xf;
  756. width=delta>>2; delta&=3;
  757. width++; delta++; // Width and height in tiles
  758. if (code&0x10000) delta=-delta; // Flip X
  759. delta<<=4;
  760. tile=((unsigned int)code>>17)<<1;
  761. sx=(code<<16)>>22; // sx can be negative (start offscreen), so sign extend
  762. if(sh && pal == 0x30) { //
  763. if(code&0x10000) fTileFunc=TileFlipSH;
  764. else fTileFunc=TileNormSH;
  765. } else {
  766. if(code&0x10000) fTileFunc=TileFlip;
  767. else fTileFunc=TileNorm;
  768. }
  769. for (; width; width--,sx+=8,tile+=delta)
  770. {
  771. if(sx<=0) continue;
  772. if(sx>=328) break; // Offscreen
  773. tile&=0x7fff; // Clip tile address
  774. fTileFunc(sx,tile,pal);
  775. }
  776. }
  777. }
  778. #endif
  779. // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
  780. // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
  781. // v
  782. // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
  783. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  784. static void PrepareSprites(int full)
  785. {
  786. struct PicoVideo *pvid=&Pico.video;
  787. int u=0,link=0,sblocks=0;
  788. int table=0;
  789. int *pd = HighPreSpr;
  790. table=pvid->reg[5]&0x7f;
  791. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  792. table<<=8; // Get sprite table address/2
  793. if (!full)
  794. {
  795. int pack;
  796. // updates: tilecode, sx
  797. for (u=0; u < 80 && (pack = *pd); u++, pd+=2)
  798. {
  799. unsigned int *sprite;
  800. int code, code2, sx, sy, skip=0;
  801. sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  802. // parse sprite info
  803. code = sprite[0];
  804. code2 = sprite[1];
  805. code2 &= ~0xfe000000;
  806. code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
  807. sx = code2>>16;
  808. if((sx <= 8-((pack>>28)<<3) && sx >= -0x76) || sx >= 328) skip=1<<23;
  809. else if ((sy = (pack<<16)>>16) < 240 && sy > -32) {
  810. int sbl = (2<<(pack>>28))-1;
  811. sblocks |= sbl<<(sy>>3);
  812. }
  813. *pd = (pack&~(1<<23))|skip;
  814. *(pd+1) = code2;
  815. // Find next sprite
  816. link=(code>>16)&0x7f;
  817. if(!link) break; // End of sprites
  818. }
  819. SpriteBlocks |= sblocks;
  820. }
  821. else
  822. {
  823. for (; u < 80; u++)
  824. {
  825. unsigned int *sprite;
  826. int code, code2, sx, sy, hv, height, width, skip=0, sx_min;
  827. sprite=(unsigned int *)(Pico.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  828. // parse sprite info
  829. code = sprite[0];
  830. sy = (code&0x1ff)-0x80;
  831. hv = (code>>24)&0xf;
  832. height = (hv&3)+1;
  833. if(sy > 240 || sy + (height<<3) <= 0) skip|=1<<22;
  834. width = (hv>>2)+1;
  835. code2 = sprite[1];
  836. sx = (code2>>16)&0x1ff;
  837. sx -= 0x78; // Get X coordinate + 8
  838. sx_min = 8-(width<<3);
  839. if((sx <= sx_min && sx >= -0x76) || sx >= 328) skip|=1<<23;
  840. else if (sx > sx_min && !skip) {
  841. int sbl = (2<<height)-1;
  842. int shi = sy>>3;
  843. if(shi < 0) shi=0; // negative sy
  844. sblocks |= sbl<<shi;
  845. }
  846. *pd++ = (width<<28)|(height<<24)|skip|(hv<<16)|((unsigned short)sy);
  847. *pd++ = (sx<<16)|((unsigned short)code2);
  848. // Find next sprite
  849. link=(code>>16)&0x7f;
  850. if(!link) break; // End of sprites
  851. }
  852. SpriteBlocks = sblocks;
  853. *pd = 0; // terminate
  854. }
  855. }
  856. static void DrawAllSprites(int *hcache, int maxwidth, int prio, int sh)
  857. {
  858. int i,u,n;
  859. int sx1seen=0; // sprite with x coord 1 or 0 seen
  860. int ntiles = 0; // tile counter for sprite limit emulation
  861. int *sprites[40]; // Sprites to draw in fast mode
  862. int *ps, pack, rs = rendstatus, scan=Scanline;
  863. if(rs&8) {
  864. DrawAllSpritesInterlace(prio, maxwidth);
  865. return;
  866. }
  867. if(rs&0x11) {
  868. //dprintf("PrepareSprites(%i) [%i]", (rs>>4)&1, scan);
  869. PrepareSprites(rs&0x10);
  870. rendstatus=rs&~0x11;
  871. }
  872. if (!(SpriteBlocks & (1<<(scan>>3)))) return;
  873. if(((rs&4)||sh)&&prio==0)
  874. memset(HighSprZ, 0, 328);
  875. if(!(rs&4)&&prio) {
  876. if(hcache[0]) DrawSpritesFromCache(hcache, sh);
  877. return;
  878. }
  879. ps = HighPreSpr;
  880. // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
  881. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  882. for(i=u=n=0; (pack = *ps) && n < 20; ps+=2, u++)
  883. {
  884. int sx, sy, row, pack2;
  885. if(pack & 0x00400000) continue;
  886. // get sprite info
  887. pack2 = *(ps+1);
  888. sx = pack2>>16;
  889. sy = (pack <<16)>>16;
  890. row = scan-sy;
  891. //dprintf("x: %i y: %i %ix%i", sx, sy, (pack>>28)<<3, (pack>>21)&0x38);
  892. if(sx == -0x77) sx1seen|=1; // for masking mode 2
  893. // check if it is on this line
  894. if(row < 0 || row >= ((pack>>21)&0x38)) continue; // no
  895. n++; // number of sprites on this line (both visible and hidden, max is 20) [broken]
  896. // sprite limit
  897. ntiles += pack>>28;
  898. if(ntiles > 40) break;
  899. if(pack & 0x00800000) continue;
  900. // masking sprite?
  901. if(sx == -0x78) {
  902. if(!(sx1seen&1) || sx1seen==3) {
  903. break; // this sprite is not drawn and remaining sprites are masked
  904. }
  905. if((sx1seen>>8) == 0) sx1seen=(i+1)<<8;
  906. continue;
  907. }
  908. else if(sx == -0x77) {
  909. // masking mode2 (Outrun, Galaxy Force II, Shadow of the beast)
  910. if(sx1seen>>8) { i=(sx1seen>>8)-1; break; } // seen both 0 and 1
  911. sx1seen |= 2;
  912. continue;
  913. }
  914. // accurate sprites
  915. //dprintf("P:%i",((sx>>15)&1));
  916. if(rs&4) {
  917. // might need to skip this sprite
  918. if((pack2&0x8000) ^ (prio<<15)) continue;
  919. DrawSpriteZ(pack,pack2,sh|(prio<<1),(char)(0x1f-n));
  920. continue;
  921. }
  922. // sprite is good, save it's pointer
  923. sprites[i++]=ps;
  924. }
  925. // Go through sprites backwards:
  926. if(!(rs&4)) {
  927. for (i--; i>=0; i--)
  928. DrawSprite(sprites[i],&hcache,sh);
  929. // terminate cache list
  930. *hcache = 0;
  931. }
  932. }
  933. // --------------------------------------------
  934. #ifndef _ASM_DRAW_C
  935. static void BackFill(int reg7, int sh)
  936. {
  937. unsigned int back=0;
  938. unsigned int *pd=NULL,*end=NULL;
  939. // Start with a blank scanline (background colour):
  940. back=reg7&0x3f;
  941. back|=sh<<6;
  942. back|=back<<8;
  943. back|=back<<16;
  944. pd= (unsigned int *)(HighCol+8);
  945. end=(unsigned int *)(HighCol+8+320);
  946. do { pd[0]=pd[1]=pd[2]=pd[3]=back; pd+=4; } while (pd<end);
  947. }
  948. #endif
  949. // --------------------------------------------
  950. unsigned short HighPal[0x100];
  951. #ifndef _ASM_DRAW_C
  952. static void FinalizeLineBGR444(int sh)
  953. {
  954. unsigned short *pd=DrawLineDest;
  955. unsigned char *ps=HighCol+8;
  956. unsigned short *pal=Pico.cram;
  957. int len, i, t;
  958. if (Pico.video.reg[12]&1) {
  959. len = 320;
  960. } else {
  961. if(!(PicoOpt&0x100)) pd+=32;
  962. len = 256;
  963. }
  964. if(sh) {
  965. pal=HighPal;
  966. if(Pico.m.dirtyPal) {
  967. blockcpy(pal, Pico.cram, 0x40*2);
  968. // shadowed pixels
  969. for(i = 0x3f; i >= 0; i--)
  970. pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x0777);
  971. // hilighted pixels
  972. for(i = 0x3f; i >= 0; i--) {
  973. t=pal[i]&0xeee;t+=0x444;if(t&0x10)t|=0xe;if(t&0x100)t|=0xe0;if(t&0x1000)t|=0xe00;t&=0xeee;
  974. pal[0x80|i]=(unsigned short)t;
  975. }
  976. Pico.m.dirtyPal = 0;
  977. }
  978. }
  979. for(i = 0; i < len; i++)
  980. pd[i] = pal[ps[i]];
  981. }
  982. static void FinalizeLineRGB555(int sh)
  983. {
  984. unsigned short *pd=DrawLineDest;
  985. unsigned char *ps=HighCol+8;
  986. unsigned short *pal=HighPal;
  987. int len, i, t, dirtyPal = Pico.m.dirtyPal;
  988. if (dirtyPal)
  989. {
  990. unsigned int *spal=(void *)Pico.cram;
  991. unsigned int *dpal=(void *)HighPal;
  992. for (i = 0x3f/2; i >= 0; i--)
  993. #ifdef USE_BGR555
  994. dpal[i] = ((spal[i]&0x000f000f)<< 1)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)<<4);
  995. #else
  996. dpal[i] = ((spal[i]&0x000f000f)<<12)|((spal[i]&0x00f000f0)<<3)|((spal[i]&0x0f000f00)>>7);
  997. #endif
  998. Pico.m.dirtyPal = 0;
  999. }
  1000. if (sh)
  1001. {
  1002. if (dirtyPal) {
  1003. // shadowed pixels
  1004. for (i = 0x3f; i >= 0; i--)
  1005. pal[0x40|i] = pal[0xc0|i] = (unsigned short)((pal[i]>>1)&0x738e);
  1006. // hilighted pixels
  1007. for (i = 0x3f; i >= 0; i--) {
  1008. t=pal[i]&0xe71c;t+=0x4208;if(t&0x20)t|=0x1c;if(t&0x800)t|=0x700;if(t&0x10000)t|=0xe000;t&=0xe71c;
  1009. pal[0x80|i]=(unsigned short)t;
  1010. }
  1011. }
  1012. }
  1013. if (Pico.video.reg[12]&1) {
  1014. len = 320;
  1015. } else {
  1016. if (!(PicoOpt&0x100)) pd+=32;
  1017. len = 256;
  1018. }
  1019. #ifndef PSP
  1020. for (i = 0; i < len; i++)
  1021. pd[i] = pal[ps[i]];
  1022. #else
  1023. {
  1024. extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  1025. amips_clut(pd, ps, pal, len);
  1026. }
  1027. #endif
  1028. }
  1029. #endif
  1030. static void FinalizeLine8bit(int sh)
  1031. {
  1032. unsigned char *pd=DrawLineDest;
  1033. int len, rs = rendstatus;
  1034. static int dirty_count;
  1035. if (!sh && Pico.m.dirtyPal == 1 && Scanline < 222) {
  1036. // a hack for mid-frame palette changes
  1037. if (!(rs & 0x20))
  1038. dirty_count = 1;
  1039. else dirty_count++;
  1040. rs |= 0x20;
  1041. rendstatus = rs;
  1042. if (dirty_count == 3) {
  1043. blockcpy(HighPal, Pico.cram, 0x40*2);
  1044. } else if (dirty_count == 11) {
  1045. blockcpy(HighPal+0x40, Pico.cram, 0x40*2);
  1046. }
  1047. }
  1048. if (Pico.video.reg[12]&1) {
  1049. len = 320;
  1050. } else {
  1051. if(!(PicoOpt&0x100)) pd+=32;
  1052. len = 256;
  1053. }
  1054. if (!sh && rs & 0x20) {
  1055. if (dirty_count >= 11) {
  1056. blockcpy_or(pd, HighCol+8, len, 0x80);
  1057. } else {
  1058. blockcpy_or(pd, HighCol+8, len, 0x40);
  1059. }
  1060. } else {
  1061. blockcpy(pd, HighCol+8, len);
  1062. }
  1063. }
  1064. static void (*FinalizeLine)(int sh) = FinalizeLineBGR444;
  1065. // --------------------------------------------
  1066. static int DrawDisplay(int sh)
  1067. {
  1068. struct PicoVideo *pvid=&Pico.video;
  1069. int win=0,edge=0,hvwind=0;
  1070. int maxw, maxcells;
  1071. rendstatus&=~0xc0;
  1072. if(pvid->reg[12]&1) {
  1073. maxw = 328; maxcells = 40;
  1074. } else {
  1075. maxw = 264; maxcells = 32;
  1076. }
  1077. // Find out if the window is on this line:
  1078. win=pvid->reg[0x12];
  1079. edge=(win&0x1f)<<3;
  1080. if (win&0x80) { if (Scanline>=edge) hvwind=1; }
  1081. else { if (Scanline< edge) hvwind=1; }
  1082. if (!hvwind) { // we might have a vertical window here
  1083. win=pvid->reg[0x11];
  1084. edge=win&0x1f;
  1085. if (win&0x80) {
  1086. if (!edge) hvwind=1;
  1087. else if(edge < (maxcells>>1)) hvwind=2;
  1088. } else {
  1089. if (!edge);
  1090. else if(edge < (maxcells>>1)) hvwind=2;
  1091. else hvwind=1;
  1092. }
  1093. }
  1094. DrawLayer(1|(sh<<1), HighCacheB, 0, maxcells);
  1095. if (hvwind == 1)
  1096. DrawWindow(0, maxcells>>1, 0, sh);
  1097. else if (hvwind == 2) {
  1098. // ahh, we have vertical window
  1099. DrawLayer(0|(sh<<1), HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells);
  1100. DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh);
  1101. } else
  1102. DrawLayer(0|(sh<<1), HighCacheA, 0, maxcells);
  1103. DrawAllSprites(HighCacheS, maxw, 0, sh);
  1104. if (HighCacheB[0]) DrawTilesFromCache(HighCacheB, sh, 328);
  1105. if (hvwind == 1)
  1106. DrawWindow(0, maxcells>>1, 1, sh);
  1107. else if (hvwind == 2) {
  1108. if(HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : 328);
  1109. DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh);
  1110. } else
  1111. if (HighCacheA[0]) DrawTilesFromCache(HighCacheA, sh, 328);
  1112. DrawAllSprites(HighCacheS, maxw, 1, sh);
  1113. #if 0
  1114. {
  1115. int *c, a, b;
  1116. for (a = 0, c = HighCacheA; *c; c++, a++);
  1117. for (b = 0, c = HighCacheB; *c; c++, b++);
  1118. printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count, Scanline, a, b);
  1119. }
  1120. #endif
  1121. return 0;
  1122. }
  1123. static int Skip=0;
  1124. PICO_INTERNAL void PicoFrameStart(void)
  1125. {
  1126. // prepare to do this frame
  1127. rendstatus = (PicoOpt&0x80)>>5; // accurate sprites
  1128. if(rendstatus)
  1129. Pico.video.status &= ~0x0020;
  1130. else Pico.video.status |= 0x0020; // sprite collision
  1131. if((Pico.video.reg[12]&6) == 6) rendstatus |= 8; // interlace mode
  1132. if(Pico.m.dirtyPal) Pico.m.dirtyPal = 2; // reset dirty if needed
  1133. PrepareSprites(1);
  1134. Skip=0;
  1135. }
  1136. PICO_INTERNAL int PicoLine(int scan)
  1137. {
  1138. int sh;
  1139. if (Skip>0) { Skip--; return 0; } // Skip rendering lines
  1140. Scanline=scan;
  1141. sh=(Pico.video.reg[0xC]&8)>>3; // shadow/hilight?
  1142. // Draw screen:
  1143. BackFill(Pico.video.reg[7], sh);
  1144. if (Pico.video.reg[1]&0x40)
  1145. DrawDisplay(sh);
  1146. if (FinalizeLine != NULL)
  1147. FinalizeLine(sh);
  1148. Skip=PicoScan(Scanline,DrawLineDest);
  1149. return 0;
  1150. }
  1151. void PicoDrawSetColorFormat(int which)
  1152. {
  1153. switch (which)
  1154. {
  1155. case 2: FinalizeLine = FinalizeLine8bit; break;
  1156. case 1: FinalizeLine = FinalizeLineRGB555; break;
  1157. case 0: FinalizeLine = FinalizeLineBGR444; break;
  1158. default:FinalizeLine = NULL; break;
  1159. }
  1160. #if OVERRIDE_HIGHCOL
  1161. if (which) HighCol=DefHighCol;
  1162. #endif
  1163. }