Draw.c 42 KB

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