draw.c 46 KB

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