draw.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * line renderer
  3. * (c) Copyright Dave, 2004
  4. * (C) notaz, 2006-2010
  5. *
  6. * This work is licensed under the terms of MAME license.
  7. * See COPYING file in the top-level directory.
  8. */
  9. /*
  10. * The renderer has 4 modes now:
  11. * - normal
  12. * - shadow/hilight (s/h)
  13. * - "sonic mode" for midline palette changes (8bit mode only)
  14. * - accurate sprites (AS) [+ s/h]
  15. *
  16. * AS and s/h both use upper bits for both priority and shadow/hilight flags.
  17. * "sonic mode" is autodetected, shadow/hilight is enabled by emulated game.
  18. * AS is enabled by user and takes priority over "sonic mode".
  19. *
  20. * since renderer always draws line in 8bit mode, there are 2 spare bits:
  21. * b \ mode: s/h as sonic
  22. * 00 normal - pal index
  23. * 01 shadow - pal index
  24. * 10 hilight+op spr spr pal index
  25. * 11 shadow +op spr - pal index
  26. *
  27. * not handled properly:
  28. * - hilight op on shadow tile
  29. * - AS + s/h (s/h sprite flag interferes with and cleared by AS code)
  30. */
  31. #include "pico_int.h"
  32. int (*PicoScanBegin)(unsigned int num) = NULL;
  33. int (*PicoScanEnd) (unsigned int num) = NULL;
  34. static unsigned char DefHighCol[8+320+8];
  35. static unsigned char *HighColBase = DefHighCol;
  36. static int HighColIncrement;
  37. static unsigned int DefOutBuff[320*2/2];
  38. void *DrawLineDestBase = DefOutBuff;
  39. int DrawLineDestIncrement;
  40. static int HighCacheA[41+1]; // caches for high layers
  41. static int HighCacheB[41+1];
  42. static int HighPreSpr[80*2+1]; // slightly preprocessed sprites
  43. #define LF_PLANE_1 (1 << 0)
  44. #define LF_SH (1 << 1) // must be = 2
  45. #define LF_FORCE (1 << 2)
  46. #define SPRL_HAVE_HI 0x80 // have hi priority sprites
  47. #define SPRL_HAVE_LO 0x40 // *lo*
  48. #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line
  49. #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi
  50. unsigned char HighLnSpr[240][3 + MAX_LINE_SPRITES]; // sprite_count, ^flags, tile_count, [spritep]...
  51. int rendstatus_old;
  52. int rendlines;
  53. static int skip_next_line=0;
  54. struct TileStrip
  55. {
  56. int nametab; // Position in VRAM of name table (for this tile line)
  57. int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
  58. int hscroll; // Horizontal scroll value in pixels for the line
  59. int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
  60. int *hc; // cache for high tile codes and their positions
  61. int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
  62. };
  63. // stuff available in asm:
  64. #ifdef _ASM_DRAW_C
  65. void DrawWindow(int tstart, int tend, int prio, int sh,
  66. struct PicoEState *est);
  67. void DrawAllSprites(unsigned char *sprited, int prio, int sh,
  68. struct PicoEState *est);
  69. void DrawTilesFromCache(int *hc, int sh, int rlim,
  70. struct PicoEState *est);
  71. void DrawSpritesSHi(unsigned char *sprited, struct PicoEState *est);
  72. void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
  73. struct PicoEState *est);
  74. void *blockcpy(void *dst, const void *src, size_t n);
  75. void blockcpy_or(void *dst, void *src, size_t n, int pat);
  76. #else
  77. // utility
  78. void blockcpy_or(void *dst, void *src, size_t n, int pat)
  79. {
  80. unsigned char *pd = dst, *ps = src;
  81. for (; n; n--)
  82. *pd++ = (unsigned char) (*ps++ | pat);
  83. }
  84. #define blockcpy memcpy
  85. #endif
  86. #define TileNormMaker(funcname,pix_func) \
  87. static void funcname(int sx, unsigned int pack, int pal) \
  88. { \
  89. unsigned char *pd = Pico.est.HighCol + sx; \
  90. unsigned int t; \
  91. \
  92. t = (pack&0x0000f000)>>12; pix_func(0); \
  93. t = (pack&0x00000f00)>> 8; pix_func(1); \
  94. t = (pack&0x000000f0)>> 4; pix_func(2); \
  95. t = (pack&0x0000000f) ; pix_func(3); \
  96. t = (pack&0xf0000000)>>28; pix_func(4); \
  97. t = (pack&0x0f000000)>>24; pix_func(5); \
  98. t = (pack&0x00f00000)>>20; pix_func(6); \
  99. t = (pack&0x000f0000)>>16; pix_func(7); \
  100. }
  101. #define TileFlipMaker(funcname,pix_func) \
  102. static void funcname(int sx, unsigned int pack, int pal) \
  103. { \
  104. unsigned char *pd = Pico.est.HighCol + sx; \
  105. unsigned int t; \
  106. \
  107. t = (pack&0x000f0000)>>16; pix_func(0); \
  108. t = (pack&0x00f00000)>>20; pix_func(1); \
  109. t = (pack&0x0f000000)>>24; pix_func(2); \
  110. t = (pack&0xf0000000)>>28; pix_func(3); \
  111. t = (pack&0x0000000f) ; pix_func(4); \
  112. t = (pack&0x000000f0)>> 4; pix_func(5); \
  113. t = (pack&0x00000f00)>> 8; pix_func(6); \
  114. t = (pack&0x0000f000)>>12; pix_func(7); \
  115. }
  116. #define pix_just_write(x) \
  117. if (t) pd[x]=pal|t
  118. TileNormMaker(TileNorm,pix_just_write)
  119. TileFlipMaker(TileFlip,pix_just_write)
  120. #ifndef _ASM_DRAW_C
  121. // draw a sprite pixel, process operator colors
  122. #define pix_sh(x) \
  123. if (!t); \
  124. else if (t>=0xe) pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \
  125. else pd[x]=pal|t
  126. TileNormMaker(TileNormSH, pix_sh)
  127. TileFlipMaker(TileFlipSH, pix_sh)
  128. // draw a sprite pixel, mark operator colors
  129. #define pix_sh_markop(x) \
  130. if (!t); \
  131. else if (t>=0xe) pd[x]|=0x80; \
  132. else pd[x]=pal|t
  133. TileNormMaker(TileNormSH_markop, pix_sh_markop)
  134. TileFlipMaker(TileFlipSH_markop, pix_sh_markop)
  135. // process operator pixels only, apply only on low pri tiles and other op pixels
  136. #define pix_sh_onlyop(x) \
  137. if (t>=0xe && (pd[x]&0xc0)) \
  138. pd[x]=(pd[x]&0x3f)|(t<<6); /* c0 shadow, 80 hilight */ \
  139. TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)
  140. TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)
  141. #endif
  142. // draw a sprite pixel (AS)
  143. #define pix_as(x) \
  144. if (t && !(pd[x]&0x80)) pd[x]=pal|t
  145. TileNormMaker(TileNormAS, pix_as)
  146. TileFlipMaker(TileFlipAS, pix_as)
  147. // draw a sprite pixel, skip operator colors (AS)
  148. #define pix_sh_as_noop(x) \
  149. if (t && t < 0xe && !(pd[x]&0x80)) pd[x]=pal|t
  150. TileNormMaker(TileNormAS_noop, pix_sh_as_noop)
  151. TileFlipMaker(TileFlipAS_noop, pix_sh_as_noop)
  152. // mark pixel as sprite pixel (AS)
  153. #define pix_sh_as_onlymark(x) \
  154. if (t) pd[x]|=0x80
  155. TileNormMaker(TileNormAS_onlymark, pix_sh_as_onlymark)
  156. TileFlipMaker(TileFlipAS_onlymark, pix_sh_as_onlymark)
  157. // mark pixel as sprite pixel (AS)
  158. #define pix_and(x) \
  159. pd[x] = (pd[x] & 0xc0) | (pd[x] & (pal | t))
  160. TileNormMaker(TileNorm_and, pix_and)
  161. TileFlipMaker(TileFlip_and, pix_and)
  162. // --------------------------------------------
  163. #ifndef _ASM_DRAW_C
  164. static void DrawStrip(struct TileStrip *ts, int lflags, int cellskip)
  165. {
  166. int tilex,dx,ty,code=0,addr=0,cells;
  167. int oldcode=-1,blank=-1; // The tile we know is blank
  168. int pal=0,sh;
  169. // Draw tiles across screen:
  170. sh = (lflags & LF_SH) << 5; // 0x40
  171. tilex=((-ts->hscroll)>>3)+cellskip;
  172. ty=(ts->line&7)<<1; // Y-Offset into tile
  173. dx=((ts->hscroll-1)&7)+1;
  174. cells = ts->cells - cellskip;
  175. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  176. dx+=cellskip<<3;
  177. for (; cells > 0; dx+=8, tilex++, cells--)
  178. {
  179. unsigned int pack;
  180. code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];
  181. if (code == blank)
  182. continue;
  183. if ((code >> 15) | (lflags & LF_FORCE)) { // high priority tile
  184. int cval = code | (dx<<16) | (ty<<25);
  185. if(code&0x1000) cval^=7<<26;
  186. *ts->hc++ = cval; // cache it
  187. continue;
  188. }
  189. if (code!=oldcode) {
  190. oldcode = code;
  191. // Get tile address/2:
  192. addr=(code&0x7ff)<<4;
  193. addr+=ty;
  194. if (code&0x1000) addr^=0xe; // Y-flip
  195. pal=((code>>9)&0x30)|sh;
  196. }
  197. pack = *(unsigned int *)(PicoMem.vram + addr);
  198. if (!pack) {
  199. blank = code;
  200. continue;
  201. }
  202. if (code & 0x0800) TileFlip(dx, pack, pal);
  203. else TileNorm(dx, pack, pal);
  204. }
  205. // terminate the cache list
  206. *ts->hc = 0;
  207. // if oldcode wasn't changed, it means all layer is hi priority
  208. if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;
  209. }
  210. // this is messy
  211. static void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
  212. {
  213. int tilex,dx,code=0,addr=0,cell=0;
  214. int oldcode=-1,blank=-1; // The tile we know is blank
  215. int pal=0,scan=Pico.est.DrawScanline;
  216. // Draw tiles across screen:
  217. tilex=(-ts->hscroll)>>3;
  218. dx=((ts->hscroll-1)&7)+1;
  219. if (ts->hscroll & 0x0f) {
  220. int adj = ((ts->hscroll ^ dx) >> 3) & 1;
  221. cell -= adj + 1;
  222. ts->cells -= adj;
  223. }
  224. cell+=cellskip;
  225. tilex+=cellskip;
  226. dx+=cellskip<<3;
  227. for (; cell < ts->cells; dx+=8,tilex++,cell++)
  228. {
  229. int nametabadd, ty;
  230. unsigned int pack;
  231. //if((cell&1)==0)
  232. {
  233. int line,vscroll;
  234. vscroll=PicoMem.vsram[(plane_sh&1)+(cell&~1)];
  235. // Find the line in the name table
  236. line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
  237. nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
  238. ty=(line&7)<<1; // Y-Offset into tile
  239. }
  240. code=PicoMem.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
  241. if (code==blank) continue;
  242. if (code>>15) { // high priority tile
  243. int cval = code | (dx<<16) | (ty<<25);
  244. if(code&0x1000) cval^=7<<26;
  245. *ts->hc++ = cval; // cache it
  246. continue;
  247. }
  248. if (code!=oldcode) {
  249. oldcode = code;
  250. // Get tile address/2:
  251. addr=(code&0x7ff)<<4;
  252. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  253. pal=((code>>9)&0x30)|((plane_sh<<5)&0x40);
  254. }
  255. pack = *(unsigned int *)(PicoMem.vram + addr);
  256. if (!pack) {
  257. blank = code;
  258. continue;
  259. }
  260. if (code & 0x0800) TileFlip(dx, pack, pal);
  261. else TileNorm(dx, pack, pal);
  262. }
  263. // terminate the cache list
  264. *ts->hc = 0;
  265. if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;
  266. }
  267. #endif
  268. #ifndef _ASM_DRAW_C
  269. static
  270. #endif
  271. void DrawStripInterlace(struct TileStrip *ts)
  272. {
  273. int tilex=0,dx=0,ty=0,code=0,addr=0,cells;
  274. int oldcode=-1,blank=-1; // The tile we know is blank
  275. int pal=0;
  276. // Draw tiles across screen:
  277. tilex=(-ts->hscroll)>>3;
  278. ty=(ts->line&15)<<1; // Y-Offset into tile
  279. dx=((ts->hscroll-1)&7)+1;
  280. cells = ts->cells;
  281. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  282. for (; cells; dx+=8,tilex++,cells--)
  283. {
  284. unsigned int pack;
  285. code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];
  286. if (code==blank) continue;
  287. if (code>>15) { // high priority tile
  288. int cval = (code&0xfc00) | (dx<<16) | (ty<<25);
  289. cval|=(code&0x3ff)<<1;
  290. if(code&0x1000) cval^=0xf<<26;
  291. *ts->hc++ = cval; // cache it
  292. continue;
  293. }
  294. if (code!=oldcode) {
  295. oldcode = code;
  296. // Get tile address/2:
  297. addr=(code&0x7ff)<<5;
  298. if (code&0x1000) addr+=30-ty; else addr+=ty; // Y-flip
  299. // pal=Pico.cram+((code>>9)&0x30);
  300. pal=((code>>9)&0x30);
  301. }
  302. pack = *(unsigned int *)(PicoMem.vram + addr);
  303. if (!pack) {
  304. blank = code;
  305. continue;
  306. }
  307. if (code & 0x0800) TileFlip(dx, pack, pal);
  308. else TileNorm(dx, pack, pal);
  309. }
  310. // terminate the cache list
  311. *ts->hc = 0;
  312. }
  313. // --------------------------------------------
  314. #ifndef _ASM_DRAW_C
  315. static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
  316. struct PicoEState *est)
  317. {
  318. struct PicoVideo *pvid=&Pico.video;
  319. const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
  320. struct TileStrip ts;
  321. int width, height, ymask;
  322. int vscroll, htab;
  323. ts.hc=hcache;
  324. ts.cells=maxcells;
  325. // Work out the TileStrip to draw
  326. // Work out the name table size: 32 64 or 128 tiles (0-3)
  327. width=pvid->reg[16];
  328. height=(width>>4)&3; width&=3;
  329. ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
  330. ymask=(height<<8)|0xff; // Y Mask in pixels
  331. switch (width) {
  332. case 1: ymask &= 0x1ff; break;
  333. case 2: ymask = 0x007; break;
  334. case 3: ymask = 0x0ff; break;
  335. }
  336. // Find name table:
  337. if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
  338. else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
  339. htab=pvid->reg[13]<<9; // Horizontal scroll table address
  340. if ( pvid->reg[11]&2) htab+=est->DrawScanline<<1; // Offset by line
  341. if ((pvid->reg[11]&1)==0) htab&=~0xf; // Offset by tile
  342. htab+=plane_sh&1; // A or B
  343. // Get horizontal scroll value, will be masked later
  344. ts.hscroll = PicoMem.vram[htab & 0x7fff];
  345. if((pvid->reg[12]&6) == 6) {
  346. // interlace mode 2
  347. vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value
  348. // Find the line in the name table
  349. ts.line=(vscroll+(est->DrawScanline<<1))&((ymask<<1)|1);
  350. ts.nametab+=(ts.line>>4)<<shift[width];
  351. DrawStripInterlace(&ts);
  352. } else if( pvid->reg[11]&4) {
  353. // shit, we have 2-cell column based vscroll
  354. // luckily this doesn't happen too often
  355. ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
  356. DrawStripVSRam(&ts, plane_sh, cellskip);
  357. } else {
  358. vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value
  359. // Find the line in the name table
  360. ts.line=(vscroll+est->DrawScanline)&ymask;
  361. ts.nametab+=(ts.line>>3)<<shift[width];
  362. DrawStrip(&ts, plane_sh, cellskip);
  363. }
  364. }
  365. // --------------------------------------------
  366. // tstart & tend are tile pair numbers
  367. static void DrawWindow(int tstart, int tend, int prio, int sh,
  368. struct PicoEState *est)
  369. {
  370. struct PicoVideo *pvid=&Pico.video;
  371. int tilex,ty,nametab,code=0;
  372. int blank=-1; // The tile we know is blank
  373. // Find name table line:
  374. if (pvid->reg[12]&1)
  375. {
  376. nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
  377. nametab+=(est->DrawScanline>>3)<<6;
  378. }
  379. else
  380. {
  381. nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
  382. nametab+=(est->DrawScanline>>3)<<5;
  383. }
  384. tilex=tstart<<1;
  385. if (!(est->rendstatus & PDRAW_WND_DIFF_PRIO)) {
  386. // check the first tile code
  387. code = PicoMem.vram[nametab + tilex];
  388. // if the whole window uses same priority (what is often the case), we may be able to skip this field
  389. if ((code>>15) != prio) return;
  390. }
  391. tend<<=1;
  392. ty=(est->DrawScanline&7)<<1; // Y-Offset into tile
  393. // Draw tiles across screen:
  394. if (!sh)
  395. {
  396. for (; tilex < tend; tilex++)
  397. {
  398. unsigned int pack;
  399. int dx, addr;
  400. int pal;
  401. code = PicoMem.vram[nametab + tilex];
  402. if (code==blank) continue;
  403. if ((code>>15) != prio) {
  404. est->rendstatus |= PDRAW_WND_DIFF_PRIO;
  405. continue;
  406. }
  407. // Get tile address/2:
  408. addr=(code&0x7ff)<<4;
  409. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  410. pack = *(unsigned int *)(PicoMem.vram + addr);
  411. if (!pack) {
  412. blank = code;
  413. continue;
  414. }
  415. pal = ((code >> 9) & 0x30);
  416. dx = 8 + (tilex << 3);
  417. if (code & 0x0800) TileFlip(dx, pack, pal);
  418. else TileNorm(dx, pack, pal);
  419. }
  420. }
  421. else
  422. {
  423. for (; tilex < tend; tilex++)
  424. {
  425. unsigned int pack;
  426. int dx, addr;
  427. int pal;
  428. code = PicoMem.vram[nametab + tilex];
  429. if(code==blank) continue;
  430. if((code>>15) != prio) {
  431. est->rendstatus |= PDRAW_WND_DIFF_PRIO;
  432. continue;
  433. }
  434. pal=((code>>9)&0x30);
  435. if (prio) {
  436. int *zb = (int *)(est->HighCol+8+(tilex<<3));
  437. *zb++ &= 0xbfbfbfbf;
  438. *zb &= 0xbfbfbfbf;
  439. } else {
  440. pal |= 0x40;
  441. }
  442. // Get tile address/2:
  443. addr=(code&0x7ff)<<4;
  444. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  445. pack = *(unsigned int *)(PicoMem.vram + addr);
  446. if (!pack) {
  447. blank = code;
  448. continue;
  449. }
  450. dx = 8 + (tilex << 3);
  451. if (code & 0x0800) TileFlip(dx, pack, pal);
  452. else TileNorm(dx, pack, pal);
  453. }
  454. }
  455. }
  456. // --------------------------------------------
  457. static void DrawTilesFromCacheShPrep(void)
  458. {
  459. // as some layer has covered whole line with hi priority tiles,
  460. // we can process whole line and then act as if sh/hi mode was off,
  461. // but leave lo pri op sprite markers alone
  462. int c = 320/4, *zb = (int *)(Pico.est.HighCol+8);
  463. Pico.est.rendstatus |= PDRAW_SHHI_DONE;
  464. while (c--)
  465. {
  466. *zb++ &= 0xbfbfbfbf;
  467. }
  468. }
  469. static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est)
  470. {
  471. int code, addr, dx;
  472. unsigned int pack;
  473. int pal;
  474. // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
  475. if (sh && (est->rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
  476. {
  477. if (!(est->rendstatus & PDRAW_SHHI_DONE))
  478. DrawTilesFromCacheShPrep();
  479. sh = 0;
  480. }
  481. if (!sh)
  482. {
  483. short blank=-1; // The tile we know is blank
  484. while ((code=*hc++)) {
  485. if (!(code & 0x8000) || (short)code == blank)
  486. continue;
  487. // Get tile address/2:
  488. addr = (code & 0x7ff) << 4;
  489. addr += code >> 25; // y offset into tile
  490. pack = *(unsigned int *)(PicoMem.vram + addr);
  491. if (!pack) {
  492. blank = (short)code;
  493. continue;
  494. }
  495. dx = (code >> 16) & 0x1ff;
  496. pal = ((code >> 9) & 0x30);
  497. if (rlim-dx < 0)
  498. goto last_cut_tile;
  499. if (code & 0x0800) TileFlip(dx, pack, pal);
  500. else TileNorm(dx, pack, pal);
  501. }
  502. }
  503. else
  504. {
  505. while ((code=*hc++)) {
  506. unsigned char *zb;
  507. // Get tile address/2:
  508. addr=(code&0x7ff)<<4;
  509. addr+=(unsigned int)code>>25; // y offset into tile
  510. dx=(code>>16)&0x1ff;
  511. zb = est->HighCol+dx;
  512. *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;
  513. *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf; *zb++ &= 0xbf;
  514. pack = *(unsigned int *)(PicoMem.vram + addr);
  515. if (!pack)
  516. continue;
  517. pal = ((code >> 9) & 0x30);
  518. if (rlim - dx < 0)
  519. goto last_cut_tile;
  520. if (code & 0x0800) TileFlip(dx, pack, pal);
  521. else TileNorm(dx, pack, pal);
  522. }
  523. }
  524. return;
  525. last_cut_tile:
  526. // for vertical window cutoff
  527. {
  528. unsigned char *pd = est->HighCol + dx;
  529. unsigned int t;
  530. if (code&0x0800)
  531. {
  532. switch (rlim-dx+8)
  533. {
  534. case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
  535. case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
  536. case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
  537. case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
  538. case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
  539. case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
  540. case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
  541. default: break;
  542. }
  543. }
  544. else
  545. {
  546. switch (rlim-dx+8)
  547. {
  548. case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
  549. case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
  550. case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
  551. case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
  552. case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
  553. case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
  554. case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
  555. default: break;
  556. }
  557. }
  558. }
  559. }
  560. // --------------------------------------------
  561. // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
  562. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  563. static void DrawSprite(int *sprite, int sh)
  564. {
  565. int width=0,height=0;
  566. int row=0,code=0;
  567. int pal;
  568. int tile=0,delta=0;
  569. int sx, sy;
  570. void (*fTileFunc)(int sx, unsigned int pack, int pal);
  571. // parse the sprite data
  572. sy=sprite[0];
  573. code=sprite[1];
  574. sx=code>>16; // X
  575. width=sy>>28;
  576. height=(sy>>24)&7; // Width and height in tiles
  577. sy=(sy<<16)>>16; // Y
  578. row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
  579. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  580. tile=code + (row>>3); // Tile number increases going down
  581. delta=height; // Delta to increase tile by going right
  582. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  583. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  584. delta<<=4; // Delta of address
  585. pal=(code>>9)&0x30;
  586. pal|=sh<<6;
  587. if (sh && (code&0x6000) == 0x6000) {
  588. if(code&0x0800) fTileFunc=TileFlipSH_markop;
  589. else fTileFunc=TileNormSH_markop;
  590. } else {
  591. if(code&0x0800) fTileFunc=TileFlip;
  592. else fTileFunc=TileNorm;
  593. }
  594. for (; width; width--,sx+=8,tile+=delta)
  595. {
  596. unsigned int pack;
  597. if(sx<=0) continue;
  598. if(sx>=328) break; // Offscreen
  599. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  600. fTileFunc(sx, pack, pal);
  601. }
  602. }
  603. #endif
  604. static NOINLINE void DrawTilesFromCacheForced(const int *hc)
  605. {
  606. int code, addr, dx;
  607. unsigned int pack;
  608. int pal;
  609. // *ts->hc++ = code | (dx<<16) | (ty<<25);
  610. while ((code = *hc++)) {
  611. // Get tile address/2:
  612. addr = (code & 0x7ff) << 4;
  613. addr += (code >> 25) & 0x0e; // y offset into tile
  614. dx = (code >> 16) & 0x1ff;
  615. pal = ((code >> 9) & 0x30);
  616. pack = *(unsigned int *)(PicoMem.vram + addr);
  617. if (code & 0x0800) TileFlip_and(dx, pack, pal);
  618. else TileNorm_and(dx, pack, pal);
  619. }
  620. }
  621. static void DrawSpriteInterlace(unsigned int *sprite)
  622. {
  623. int width=0,height=0;
  624. int row=0,code=0;
  625. int pal;
  626. int tile=0,delta=0;
  627. int sx, sy;
  628. // parse the sprite data
  629. sy=sprite[0];
  630. height=sy>>24;
  631. sy=(sy&0x3ff)-0x100; // Y
  632. width=(height>>2)&3; height&=3;
  633. width++; height++; // Width and height in tiles
  634. row=(Pico.est.DrawScanline<<1)-sy; // Row of the sprite we are on
  635. code=sprite[1];
  636. sx=((code>>16)&0x1ff)-0x78; // X
  637. if (code&0x1000) row^=(16<<height)-1; // Flip Y
  638. tile=code&0x3ff; // Tile number
  639. tile+=row>>4; // Tile number increases going down
  640. delta=height; // Delta to increase tile by going right
  641. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  642. tile<<=5; tile+=(row&15)<<1; // Tile address
  643. delta<<=5; // Delta of address
  644. pal=((code>>9)&0x30); // Get palette pointer
  645. for (; width; width--,sx+=8,tile+=delta)
  646. {
  647. unsigned int pack;
  648. if(sx<=0) continue;
  649. if(sx>=328) break; // Offscreen
  650. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  651. if (code & 0x0800) TileFlip(sx, pack, pal);
  652. else TileNorm(sx, pack, pal);
  653. }
  654. }
  655. static NOINLINE void DrawAllSpritesInterlace(int pri, int sh)
  656. {
  657. struct PicoVideo *pvid=&Pico.video;
  658. int i,u,table,link=0,sline=Pico.est.DrawScanline<<1;
  659. unsigned int *sprites[80]; // Sprite index
  660. table=pvid->reg[5]&0x7f;
  661. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  662. table<<=8; // Get sprite table address/2
  663. for (i=u=0; u < 80 && i < 21; u++)
  664. {
  665. unsigned int *sprite;
  666. int code, sx, sy, height;
  667. sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  668. // get sprite info
  669. code = sprite[0];
  670. sx = sprite[1];
  671. if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
  672. // check if it is on this line
  673. sy = (code&0x3ff)-0x100;
  674. height = (((code>>24)&3)+1)<<4;
  675. if(sline < sy || sline >= sy+height) goto nextsprite; // no
  676. // check if sprite is not hidden offscreen
  677. sx = (sx>>16)&0x1ff;
  678. sx -= 0x78; // Get X coordinate + 8
  679. if(sx <= -8*3 || sx >= 328) goto nextsprite;
  680. // sprite is good, save it's pointer
  681. sprites[i++]=sprite;
  682. nextsprite:
  683. // Find next sprite
  684. link=(code>>16)&0x7f;
  685. if(!link) break; // End of sprites
  686. }
  687. // Go through sprites backwards:
  688. for (i-- ;i>=0; i--)
  689. DrawSpriteInterlace(sprites[i]);
  690. }
  691. #ifndef _ASM_DRAW_C
  692. /*
  693. * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,
  694. * hi_layers&=~40, hi_sprites
  695. *
  696. * Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
  697. * Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  698. */
  699. static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)
  700. {
  701. void (*fTileFunc)(int sx, unsigned int pack, int pal);
  702. unsigned char *p;
  703. int cnt;
  704. cnt = sprited[0] & 0x7f;
  705. if (cnt == 0) return;
  706. p = &sprited[3];
  707. // Go through sprites backwards:
  708. for (cnt--; cnt >= 0; cnt--)
  709. {
  710. int *sprite, code, pal, tile, sx, sy;
  711. int offs, delta, width, height, row;
  712. offs = (p[cnt] & 0x7f) * 2;
  713. sprite = est->HighPreSpr + offs;
  714. code = sprite[1];
  715. pal = (code>>9)&0x30;
  716. if (pal == 0x30)
  717. {
  718. if (code & 0x8000) // hi priority
  719. {
  720. if (code&0x800) fTileFunc=TileFlipSH;
  721. else fTileFunc=TileNormSH;
  722. } else {
  723. if (code&0x800) fTileFunc=TileFlipSH_onlyop_lp;
  724. else fTileFunc=TileNormSH_onlyop_lp;
  725. }
  726. } else {
  727. if (!(code & 0x8000)) continue; // non-operator low sprite, already drawn
  728. if (code&0x800) fTileFunc=TileFlip;
  729. else fTileFunc=TileNorm;
  730. }
  731. // parse remaining sprite data
  732. sy=sprite[0];
  733. sx=code>>16; // X
  734. width=sy>>28;
  735. height=(sy>>24)&7; // Width and height in tiles
  736. sy=(sy<<16)>>16; // Y
  737. row=est->DrawScanline-sy; // Row of the sprite we are on
  738. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  739. tile=code + (row>>3); // Tile number increases going down
  740. delta=height; // Delta to increase tile by going right
  741. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  742. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  743. delta<<=4; // Delta of address
  744. for (; width; width--,sx+=8,tile+=delta)
  745. {
  746. unsigned int pack;
  747. if(sx<=0) continue;
  748. if(sx>=328) break; // Offscreen
  749. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  750. fTileFunc(sx, pack, pal);
  751. }
  752. }
  753. }
  754. #endif // !_ASM_DRAW_C
  755. static void DrawSpritesHiAS(unsigned char *sprited, int sh)
  756. {
  757. void (*fTileFunc)(int sx, unsigned int pack, int pal);
  758. unsigned char *p;
  759. int entry, cnt, sh_cnt = 0;
  760. cnt = sprited[0] & 0x7f;
  761. if (cnt == 0) return;
  762. Pico.est.rendstatus |= PDRAW_SPR_LO_ON_HI;
  763. p = &sprited[3];
  764. // Go through sprites:
  765. for (entry = 0; entry < cnt; entry++)
  766. {
  767. int *sprite, code, pal, tile, sx, sy;
  768. int offs, delta, width, height, row;
  769. offs = (p[entry] & 0x7f) * 2;
  770. sprite = HighPreSpr + offs;
  771. code = sprite[1];
  772. pal = (code>>9)&0x30;
  773. if (code & 0x8000) // hi priority
  774. {
  775. if (sh && pal == 0x30)
  776. {
  777. if (code&0x800) fTileFunc=TileFlipAS_noop;
  778. else fTileFunc=TileNormAS_noop;
  779. } else {
  780. if (code&0x800) fTileFunc=TileFlipAS;
  781. else fTileFunc=TileNormAS;
  782. }
  783. } else {
  784. if (code&0x800) fTileFunc=TileFlipAS_onlymark;
  785. else fTileFunc=TileNormAS_onlymark;
  786. }
  787. if (sh && pal == 0x30)
  788. p[sh_cnt++] = offs / 2; // re-save for sh/hi pass
  789. // parse remaining sprite data
  790. sy=sprite[0];
  791. sx=code>>16; // X
  792. width=sy>>28;
  793. height=(sy>>24)&7; // Width and height in tiles
  794. sy=(sy<<16)>>16; // Y
  795. row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
  796. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  797. tile=code + (row>>3); // Tile number increases going down
  798. delta=height; // Delta to increase tile by going right
  799. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  800. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  801. delta<<=4; // Delta of address
  802. pal |= 0x80;
  803. for (; width; width--,sx+=8,tile+=delta)
  804. {
  805. unsigned int pack;
  806. if(sx<=0) continue;
  807. if(sx>=328) break; // Offscreen
  808. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  809. fTileFunc(sx, pack, pal);
  810. }
  811. }
  812. if (!sh || !(sprited[1]&SPRL_MAY_HAVE_OP)) return;
  813. /* nasty 1: remove 'sprite' flags */
  814. {
  815. int c = 320/4/4, *zb = (int *)(Pico.est.HighCol+8);
  816. while (c--)
  817. {
  818. *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;
  819. *zb++ &= 0x7f7f7f7f; *zb++ &= 0x7f7f7f7f;
  820. }
  821. }
  822. /* nasty 2: sh operator pass */
  823. sprited[0] = sh_cnt;
  824. DrawSpritesSHi(sprited, &Pico.est);
  825. }
  826. // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
  827. // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
  828. // v
  829. // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
  830. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  831. static NOINLINE void PrepareSprites(int full)
  832. {
  833. const struct PicoVideo *pvid=&Pico.video;
  834. const struct PicoEState *est=&Pico.est;
  835. int u,link=0,sh;
  836. int table=0;
  837. int *pd = HighPreSpr;
  838. int max_lines = 224, max_sprites = 80, max_width = 328;
  839. int max_line_sprites = 20; // 20 sprites, 40 tiles
  840. if (!(Pico.video.reg[12]&1))
  841. max_sprites = 64, max_line_sprites = 16, max_width = 264;
  842. if (PicoOpt & POPT_DIS_SPRITE_LIM)
  843. max_line_sprites = MAX_LINE_SPRITES;
  844. if (pvid->reg[1]&8) max_lines = 240;
  845. sh = Pico.video.reg[0xC]&8; // shadow/hilight?
  846. table=pvid->reg[5]&0x7f;
  847. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  848. table<<=8; // Get sprite table address/2
  849. if (!full)
  850. {
  851. int pack;
  852. // updates: tilecode, sx
  853. for (u=0; u < max_sprites && (pack = *pd); u++, pd+=2)
  854. {
  855. unsigned int *sprite;
  856. int code2, sx, sy, height;
  857. sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  858. // parse sprite info
  859. code2 = sprite[1];
  860. sx = (code2>>16)&0x1ff;
  861. sx -= 0x78; // Get X coordinate + 8
  862. sy = (pack << 16) >> 16;
  863. height = (pack >> 24) & 0xf;
  864. if (sy < max_lines &&
  865. sy + (height<<3) > est->DrawScanline && // sprite onscreen (y)?
  866. (sx > -24 || sx < max_width)) // onscreen x
  867. {
  868. int y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;
  869. int entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
  870. for (; y < sy + (height<<3) && y < max_lines; y++)
  871. {
  872. int i, cnt;
  873. cnt = HighLnSpr[y][0] & 0x7f;
  874. if (cnt >= max_line_sprites) continue; // sprite limit?
  875. for (i = 0; i < cnt; i++)
  876. if (((HighLnSpr[y][3+i] ^ entry) & 0x7f) == 0) goto found;
  877. // this sprite was previously missing
  878. HighLnSpr[y][3+cnt] = entry;
  879. HighLnSpr[y][0] = cnt + 1;
  880. found:;
  881. if (entry & 0x80)
  882. HighLnSpr[y][1] |= SPRL_HAVE_HI;
  883. else HighLnSpr[y][1] |= SPRL_HAVE_LO;
  884. }
  885. }
  886. code2 &= ~0xfe000000;
  887. code2 -= 0x00780000; // Get X coordinate + 8 in upper 16 bits
  888. pd[1] = code2;
  889. // Find next sprite
  890. link=(sprite[0]>>16)&0x7f;
  891. if (!link) break; // End of sprites
  892. }
  893. }
  894. else
  895. {
  896. for (u = 0; u < max_lines; u++)
  897. *((int *)&HighLnSpr[u][0]) = 0;
  898. for (u = 0; u < max_sprites; u++)
  899. {
  900. unsigned int *sprite;
  901. int code, code2, sx, sy, hv, height, width;
  902. sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  903. // parse sprite info
  904. code = sprite[0];
  905. sy = (code&0x1ff)-0x80;
  906. hv = (code>>24)&0xf;
  907. height = (hv&3)+1;
  908. width = (hv>>2)+1;
  909. code2 = sprite[1];
  910. sx = (code2>>16)&0x1ff;
  911. sx -= 0x78; // Get X coordinate + 8
  912. if (sy < max_lines && sy + (height<<3) > est->DrawScanline) // sprite onscreen (y)?
  913. {
  914. int entry, y, sx_min, onscr_x, maybe_op = 0;
  915. sx_min = 8-(width<<3);
  916. onscr_x = sx_min < sx && sx < max_width;
  917. if (sh && (code2 & 0x6000) == 0x6000)
  918. maybe_op = SPRL_MAY_HAVE_OP;
  919. entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
  920. y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;
  921. for (; y < sy + (height<<3) && y < max_lines; y++)
  922. {
  923. unsigned char *p = &HighLnSpr[y][0];
  924. int cnt = p[0];
  925. if (cnt >= max_line_sprites) continue; // sprite limit?
  926. if (p[2] >= max_line_sprites*2) { // tile limit?
  927. p[0] |= 0x80;
  928. continue;
  929. }
  930. p[2] += width;
  931. if (sx == -0x78) {
  932. if (cnt > 0)
  933. p[0] |= 0x80; // masked, no more sprites for this line
  934. continue;
  935. }
  936. // must keep the first sprite even if it's offscreen, for masking
  937. if (cnt > 0 && !onscr_x) continue; // offscreen x
  938. p[3+cnt] = entry;
  939. p[0] = cnt + 1;
  940. p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;
  941. p[1] |= maybe_op; // there might be op sprites on this line
  942. if (cnt > 0 && (code2 & 0x8000) && !(p[3+cnt-1]&0x80))
  943. p[1] |= SPRL_LO_ABOVE_HI;
  944. }
  945. }
  946. *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);
  947. *pd++ = (sx<<16)|((unsigned short)code2);
  948. // Find next sprite
  949. link=(code>>16)&0x7f;
  950. if (!link) break; // End of sprites
  951. }
  952. *pd = 0;
  953. #if 0
  954. for (u = 0; u < max_lines; u++)
  955. {
  956. int y;
  957. printf("c%03i: %2i, %2i: ", u, HighLnSpr[u][0] & 0x7f, HighLnSpr[u][2]);
  958. for (y = 0; y < HighLnSpr[u][0] & 0x7f; y++)
  959. printf(" %i", HighLnSpr[u][y+3]);
  960. printf("\n");
  961. }
  962. #endif
  963. }
  964. }
  965. #ifndef _ASM_DRAW_C
  966. static void DrawAllSprites(unsigned char *sprited, int prio, int sh,
  967. struct PicoEState *est)
  968. {
  969. unsigned char *p;
  970. int cnt;
  971. cnt = sprited[0] & 0x7f;
  972. if (cnt == 0) return;
  973. p = &sprited[3];
  974. // Go through sprites backwards:
  975. for (cnt--; cnt >= 0; cnt--)
  976. {
  977. int offs;
  978. if ((p[cnt] >> 7) != prio) continue;
  979. offs = (p[cnt]&0x7f) * 2;
  980. DrawSprite(HighPreSpr + offs, sh);
  981. }
  982. }
  983. // --------------------------------------------
  984. void BackFill(int reg7, int sh, struct PicoEState *est)
  985. {
  986. unsigned int back;
  987. // Start with a blank scanline (background colour):
  988. back=reg7&0x3f;
  989. back|=sh<<6;
  990. back|=back<<8;
  991. back|=back<<16;
  992. memset32((int *)(est->HighCol+8), back, 320/4);
  993. }
  994. #endif
  995. // --------------------------------------------
  996. #ifndef _ASM_DRAW_C
  997. void PicoDoHighPal555(int sh, int line, struct PicoEState *est)
  998. {
  999. unsigned int *spal, *dpal;
  1000. unsigned int t, i;
  1001. Pico.m.dirtyPal = 0;
  1002. spal = (void *)PicoMem.cram;
  1003. dpal = (void *)est->HighPal;
  1004. for (i = 0; i < 0x40 / 2; i++) {
  1005. t = spal[i];
  1006. #ifdef USE_BGR555
  1007. t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);
  1008. #else
  1009. t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);
  1010. #endif
  1011. // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.
  1012. // otherwise intensity difference between this and s/h will be wrong
  1013. t |= (t >> 4) & 0x08610861; // 0x18e318e3
  1014. dpal[i] = t;
  1015. }
  1016. // norm: xxx0, sh: 0xxx, hi: 0xxx + 7
  1017. if (sh)
  1018. {
  1019. // shadowed pixels
  1020. for (i = 0; i < 0x40 / 2; i++)
  1021. dpal[0x40/2 | i] = dpal[0xc0/2 | i] = (dpal[i] >> 1) & 0x738e738e;
  1022. // hilighted pixels
  1023. for (i = 0; i < 0x40 / 2; i++) {
  1024. t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;
  1025. t |= (t >> 4) & 0x08610861;
  1026. dpal[0x80/2 | i] = t;
  1027. }
  1028. }
  1029. }
  1030. void FinalizeLine555(int sh, int line, struct PicoEState *est)
  1031. {
  1032. unsigned short *pd=est->DrawLineDest;
  1033. unsigned char *ps=est->HighCol+8;
  1034. unsigned short *pal=est->HighPal;
  1035. int len;
  1036. if (Pico.m.dirtyPal)
  1037. PicoDoHighPal555(sh, line, est);
  1038. if (Pico.video.reg[12]&1) {
  1039. len = 320;
  1040. } else {
  1041. if (!(PicoOpt&POPT_DIS_32C_BORDER)) pd+=32;
  1042. len = 256;
  1043. }
  1044. {
  1045. #ifndef PSP
  1046. int i, mask=0xff;
  1047. if (!sh && (est->rendstatus & PDRAW_SPR_LO_ON_HI))
  1048. mask=0x3f; // accurate sprites, upper bits are priority stuff
  1049. for (i = 0; i < len; i++)
  1050. pd[i] = pal[ps[i] & mask];
  1051. #else
  1052. extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  1053. extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  1054. if (!sh && (est->rendstatus & PDRAW_SPR_LO_ON_HI))
  1055. amips_clut_6bit(pd, ps, pal, len);
  1056. else amips_clut(pd, ps, pal, len);
  1057. #endif
  1058. }
  1059. }
  1060. #endif
  1061. static void FinalizeLine8bit(int sh, int line, struct PicoEState *est)
  1062. {
  1063. unsigned char *pd = est->DrawLineDest;
  1064. int len, rs = est->rendstatus;
  1065. static int dirty_count;
  1066. if (!sh && Pico.m.dirtyPal == 1)
  1067. {
  1068. // a hack for mid-frame palette changes
  1069. if (!(rs & PDRAW_SONIC_MODE))
  1070. dirty_count = 1;
  1071. else dirty_count++;
  1072. rs |= PDRAW_SONIC_MODE;
  1073. est->rendstatus = rs;
  1074. if (dirty_count == 3) {
  1075. blockcpy(est->HighPal, PicoMem.cram, 0x40*2);
  1076. } else if (dirty_count == 11) {
  1077. blockcpy(est->HighPal+0x40, PicoMem.cram, 0x40*2);
  1078. }
  1079. }
  1080. if (Pico.video.reg[12]&1) {
  1081. len = 320;
  1082. } else {
  1083. if (!(PicoOpt & POPT_DIS_32C_BORDER))
  1084. pd += 32;
  1085. len = 256;
  1086. }
  1087. if (!sh && (rs & PDRAW_SONIC_MODE)) {
  1088. if (dirty_count >= 11) {
  1089. blockcpy_or(pd, est->HighCol+8, len, 0x80);
  1090. } else {
  1091. blockcpy_or(pd, est->HighCol+8, len, 0x40);
  1092. }
  1093. } else {
  1094. blockcpy(pd, est->HighCol+8, len);
  1095. }
  1096. }
  1097. static void (*FinalizeLine)(int sh, int line, struct PicoEState *est);
  1098. // --------------------------------------------
  1099. static int DrawDisplay(int sh)
  1100. {
  1101. struct PicoEState *est=&Pico.est;
  1102. unsigned char *sprited = &HighLnSpr[est->DrawScanline][0];
  1103. struct PicoVideo *pvid=&Pico.video;
  1104. int win=0, edge=0, hvwind=0, lflags;
  1105. int maxw, maxcells;
  1106. if (est->rendstatus & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES)) {
  1107. // elprintf(EL_STATUS, "PrepareSprites(%i)", (est->rendstatus>>4)&1);
  1108. PrepareSprites(est->rendstatus & PDRAW_DIRTY_SPRITES);
  1109. est->rendstatus &= ~(PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
  1110. }
  1111. est->rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO);
  1112. if (pvid->reg[12]&1) {
  1113. maxw = 328; maxcells = 40;
  1114. } else {
  1115. maxw = 264; maxcells = 32;
  1116. }
  1117. // Find out if the window is on this line:
  1118. win=pvid->reg[0x12];
  1119. edge=(win&0x1f)<<3;
  1120. if (win&0x80) { if (est->DrawScanline>=edge) hvwind=1; }
  1121. else { if (est->DrawScanline< edge) hvwind=1; }
  1122. if (!hvwind) // we might have a vertical window here
  1123. {
  1124. win=pvid->reg[0x11];
  1125. edge=win&0x1f;
  1126. if (win&0x80) {
  1127. if (!edge) hvwind=1;
  1128. else if(edge < (maxcells>>1)) hvwind=2;
  1129. } else {
  1130. if (!edge);
  1131. else if(edge < (maxcells>>1)) hvwind=2;
  1132. else hvwind=1;
  1133. }
  1134. }
  1135. /* - layer B low - */
  1136. if (!(pvid->debug_p & PVD_KILL_B)) {
  1137. lflags = LF_PLANE_1 | (sh << 1);
  1138. if (pvid->debug_p & PVD_FORCE_B)
  1139. lflags |= LF_FORCE;
  1140. DrawLayer(lflags, HighCacheB, 0, maxcells, est);
  1141. }
  1142. /* - layer A low - */
  1143. lflags = 0 | (sh << 1);
  1144. if (pvid->debug_p & PVD_FORCE_A)
  1145. lflags |= LF_FORCE;
  1146. if (pvid->debug_p & PVD_KILL_A)
  1147. ;
  1148. else if (hvwind == 1)
  1149. DrawWindow(0, maxcells>>1, 0, sh, est);
  1150. else if (hvwind == 2) {
  1151. DrawLayer(lflags, HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells, est);
  1152. DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh, est);
  1153. }
  1154. else
  1155. DrawLayer(lflags, HighCacheA, 0, maxcells, est);
  1156. /* - sprites low - */
  1157. if (pvid->debug_p & PVD_KILL_S_LO)
  1158. ;
  1159. else if (est->rendstatus & PDRAW_INTERLACE)
  1160. DrawAllSpritesInterlace(0, sh);
  1161. else if (sprited[1] & SPRL_HAVE_LO)
  1162. DrawAllSprites(sprited, 0, sh, est);
  1163. /* - layer B hi - */
  1164. if (!(pvid->debug_p & PVD_KILL_B) && HighCacheB[0])
  1165. DrawTilesFromCache(HighCacheB, sh, maxw, est);
  1166. /* - layer A hi - */
  1167. if (pvid->debug_p & PVD_KILL_A)
  1168. ;
  1169. else if (hvwind == 1)
  1170. DrawWindow(0, maxcells>>1, 1, sh, est);
  1171. else if (hvwind == 2) {
  1172. if (HighCacheA[0])
  1173. DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw, est);
  1174. DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh, est);
  1175. } else
  1176. if (HighCacheA[0])
  1177. DrawTilesFromCache(HighCacheA, sh, maxw, est);
  1178. /* - sprites hi - */
  1179. if (pvid->debug_p & PVD_KILL_S_HI)
  1180. ;
  1181. else if (est->rendstatus & PDRAW_INTERLACE)
  1182. DrawAllSpritesInterlace(1, sh);
  1183. // have sprites without layer pri bit ontop of sprites with that bit
  1184. else if ((sprited[1] & 0xd0) == 0xd0 && (PicoOpt & POPT_ACC_SPRITES))
  1185. DrawSpritesHiAS(sprited, sh);
  1186. else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))
  1187. DrawSpritesSHi(sprited, est);
  1188. else if (sprited[1] & SPRL_HAVE_HI)
  1189. DrawAllSprites(sprited, 1, 0, est);
  1190. if (pvid->debug_p & PVD_FORCE_B)
  1191. DrawTilesFromCacheForced(HighCacheB);
  1192. else if (pvid->debug_p & PVD_FORCE_A)
  1193. DrawTilesFromCacheForced(HighCacheA);
  1194. #if 0
  1195. {
  1196. int *c, a, b;
  1197. for (a = 0, c = HighCacheA; *c; c++, a++);
  1198. for (b = 0, c = HighCacheB; *c; c++, b++);
  1199. printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count,
  1200. Pico.est.DrawScanline, a, b);
  1201. }
  1202. #endif
  1203. return 0;
  1204. }
  1205. // MUST be called every frame
  1206. PICO_INTERNAL void PicoFrameStart(void)
  1207. {
  1208. int offs = 8, lines = 224;
  1209. // prepare to do this frame
  1210. Pico.est.rendstatus = 0;
  1211. if ((Pico.video.reg[12] & 6) == 6)
  1212. Pico.est.rendstatus |= PDRAW_INTERLACE; // interlace mode
  1213. if (!(Pico.video.reg[12] & 1))
  1214. Pico.est.rendstatus |= PDRAW_32_COLS;
  1215. if (Pico.video.reg[1] & 8) {
  1216. offs = 0;
  1217. lines = 240;
  1218. }
  1219. if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
  1220. rendlines = lines;
  1221. // mode_change() might reset rendstatus_old by calling SetColorFormat
  1222. emu_video_mode_change((lines == 240) ? 0 : 8,
  1223. lines, (Pico.video.reg[12] & 1) ? 0 : 1);
  1224. rendstatus_old = Pico.est.rendstatus;
  1225. }
  1226. Pico.est.HighCol = HighColBase + offs * HighColIncrement;
  1227. Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;
  1228. Pico.est.DrawScanline = 0;
  1229. skip_next_line = 0;
  1230. if (PicoOpt & POPT_ALT_RENDERER)
  1231. return;
  1232. if (Pico.m.dirtyPal)
  1233. Pico.m.dirtyPal = 2; // reset dirty if needed
  1234. PrepareSprites(1);
  1235. }
  1236. static void DrawBlankedLine(int line, int offs, int sh, int bgc)
  1237. {
  1238. if (PicoScanBegin != NULL)
  1239. PicoScanBegin(line + offs);
  1240. BackFill(bgc, sh, &Pico.est);
  1241. if (FinalizeLine != NULL)
  1242. FinalizeLine(sh, line, &Pico.est);
  1243. if (PicoScanEnd != NULL)
  1244. PicoScanEnd(line + offs);
  1245. Pico.est.HighCol += HighColIncrement;
  1246. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  1247. }
  1248. static void PicoLine(int line, int offs, int sh, int bgc)
  1249. {
  1250. int skip = 0;
  1251. if (skip_next_line > 0) {
  1252. skip_next_line--;
  1253. return;
  1254. }
  1255. Pico.est.DrawScanline = line;
  1256. if (PicoScanBegin != NULL)
  1257. skip = PicoScanBegin(line + offs);
  1258. if (skip) {
  1259. skip_next_line = skip - 1;
  1260. return;
  1261. }
  1262. if (Pico.video.debug_p & (PVD_FORCE_A | PVD_FORCE_B))
  1263. bgc = 0x3f;
  1264. // Draw screen:
  1265. BackFill(bgc, sh, &Pico.est);
  1266. if (Pico.video.reg[1]&0x40)
  1267. DrawDisplay(sh);
  1268. if (FinalizeLine != NULL)
  1269. FinalizeLine(sh, line, &Pico.est);
  1270. if (PicoScanEnd != NULL)
  1271. skip_next_line = PicoScanEnd(line + offs);
  1272. Pico.est.HighCol += HighColIncrement;
  1273. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  1274. }
  1275. void PicoDrawSync(int to, int blank_last_line)
  1276. {
  1277. int line, offs = 0;
  1278. int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
  1279. int bgc = Pico.video.reg[7];
  1280. pprof_start(draw);
  1281. if (rendlines != 240) {
  1282. offs = 8;
  1283. if (to > 223)
  1284. to = 223;
  1285. }
  1286. for (line = Pico.est.DrawScanline; line < to; line++)
  1287. PicoLine(line, offs, sh, bgc);
  1288. // last line
  1289. if (line <= to)
  1290. {
  1291. if (blank_last_line)
  1292. DrawBlankedLine(line, offs, sh, bgc);
  1293. else PicoLine(line, offs, sh, bgc);
  1294. line++;
  1295. }
  1296. Pico.est.DrawScanline = line;
  1297. pprof_end(draw);
  1298. }
  1299. // also works for fast renderer
  1300. void PicoDrawUpdateHighPal(void)
  1301. {
  1302. struct PicoEState *est = &Pico.est;
  1303. int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
  1304. if (PicoOpt & POPT_ALT_RENDERER)
  1305. sh = 0; // no s/h support
  1306. PicoDoHighPal555(sh, 0, &Pico.est);
  1307. if (est->rendstatus & PDRAW_SONIC_MODE) {
  1308. // FIXME?
  1309. memcpy(est->HighPal + 0x40, est->HighPal, 0x40*2);
  1310. memcpy(est->HighPal + 0x80, est->HighPal, 0x40*2);
  1311. }
  1312. }
  1313. void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)
  1314. {
  1315. switch (which)
  1316. {
  1317. case PDF_8BIT:
  1318. FinalizeLine = FinalizeLine8bit;
  1319. break;
  1320. case PDF_RGB555:
  1321. if ((PicoAHW & PAHW_32X) && use_32x_line_mode)
  1322. FinalizeLine = FinalizeLine32xRGB555;
  1323. else
  1324. FinalizeLine = FinalizeLine555;
  1325. break;
  1326. default:
  1327. FinalizeLine = NULL;
  1328. break;
  1329. }
  1330. PicoDrawSetOutFormat32x(which, use_32x_line_mode);
  1331. PicoDrawSetOutputMode4(which);
  1332. rendstatus_old = -1;
  1333. }
  1334. // note: may be called on the middle of frame
  1335. void PicoDrawSetOutBuf(void *dest, int increment)
  1336. {
  1337. DrawLineDestBase = dest;
  1338. DrawLineDestIncrement = increment;
  1339. Pico.est.DrawLineDest = DrawLineDestBase + Pico.est.DrawScanline * increment;
  1340. }
  1341. void PicoDrawSetInternalBuf(void *dest, int increment)
  1342. {
  1343. if (dest != NULL) {
  1344. HighColBase = dest;
  1345. HighColIncrement = increment;
  1346. Pico.est.HighCol = HighColBase + Pico.est.DrawScanline * increment;
  1347. }
  1348. else {
  1349. HighColBase = DefHighCol;
  1350. HighColIncrement = 0;
  1351. }
  1352. }
  1353. void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))
  1354. {
  1355. PicoScanBegin = NULL;
  1356. PicoScanEnd = NULL;
  1357. PicoScan32xBegin = NULL;
  1358. PicoScan32xEnd = NULL;
  1359. if ((PicoAHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {
  1360. PicoScan32xBegin = begin;
  1361. PicoScan32xEnd = end;
  1362. }
  1363. else {
  1364. PicoScanBegin = begin;
  1365. PicoScanEnd = end;
  1366. }
  1367. }
  1368. void PicoDrawInit(void)
  1369. {
  1370. Pico.est.DrawLineDest = DefOutBuff;
  1371. Pico.est.HighCol = HighColBase;
  1372. Pico.est.HighPreSpr = HighPreSpr;
  1373. rendstatus_old = -1;
  1374. }
  1375. // vim:ts=2:sw=2:expandtab