draw.c 45 KB

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