draw.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. /*
  2. * line renderer
  3. * (c) Copyright Dave, 2004
  4. * (C) notaz, 2006-2010
  5. * (C) kub, 2019-2020
  6. *
  7. * This work is licensed under the terms of MAME license.
  8. * See COPYING file in the top-level directory.
  9. */
  10. /*
  11. * The renderer has 4 modes now:
  12. * - normal
  13. * - shadow/hilight (s/h)
  14. * - "sonic mode" for midline palette changes (8bit mode only)
  15. * - accurate sprites (AS) [+ s/h]
  16. *
  17. * s/h uses upper bits for both priority and shadow/hilight flags.
  18. * "sonic mode" is autodetected, shadow/hilight is enabled by emulated game.
  19. * AS is enabled by user and takes priority over "sonic mode".
  20. *
  21. * since renderer always draws line in 8bit mode, there are 2 spare bits:
  22. * b \ mode: s/h sonic
  23. * 00 normal pal index
  24. * 01 hilight pal index
  25. * 10 shadow pal index
  26. * 11 hilight|shadow=normal pal index
  27. *
  28. * sprite s/h can only be correctly done after the plane rendering s/h state is
  29. * known since the s/h result changes if there's at least one high prio plane.
  30. * sprite op rendering is deferred until this is known, and hilight is used as
  31. * mark since it can't occur before sprite ops:
  32. * x1 op marker pal index
  33. *
  34. * low prio s/h rendering:
  35. * - plane and non-op sprite pixels have shadow
  36. * - s/h sprite op pixel rendering is marked with hilight (deferred)
  37. * high prio s/h rendering:
  38. * - plane and non-op sprite pixels are normal
  39. * - all s/h sprite op pixels (either marked or high prio) are rendered
  40. *
  41. * not handled properly:
  42. * - high prio s/h sprite op overlapping low prio sprite shows sprite, not A,B,G
  43. * - in debug sprite-masked, transparent high-prio sprite px don't remove shadow
  44. */
  45. #include "pico_int.h"
  46. #define FORCE // layer forcing via debug register?
  47. int (*PicoScanBegin)(unsigned int num) = NULL;
  48. int (*PicoScanEnd) (unsigned int num) = NULL;
  49. static unsigned char DefHighCol[8+320+8];
  50. unsigned char *HighColBase = DefHighCol;
  51. int HighColIncrement;
  52. static unsigned int DefOutBuff[320*2/2];
  53. void *DrawLineDestBase = DefOutBuff;
  54. int DrawLineDestIncrement;
  55. static int HighCacheA[41*2+1]; // caches for high layers
  56. static int HighCacheB[41*2+1];
  57. static int HighPreSpr[80*2+1]; // slightly preprocessed sprites
  58. unsigned int VdpSATCache[128]; // VDP sprite cache (1st 32 sprite attr bits)
  59. // NB don't change any defines without checking their usage in ASM
  60. #define LF_PLANE (1 << 0) // must be = 1
  61. #define LF_SH (1 << 1) // must be = 2
  62. //#define LF_FORCE (1 << 2)
  63. #define LF_PLANE_A 0
  64. #define LF_PLANE_B 1
  65. #define SPRL_HAVE_HI 0x80 // have hi priority sprites
  66. #define SPRL_HAVE_LO 0x40 // *lo*
  67. #define SPRL_MAY_HAVE_OP 0x20 // may have operator sprites on the line
  68. #define SPRL_LO_ABOVE_HI 0x10 // low priority sprites may be on top of hi
  69. #define SPRL_HAVE_X 0x08 // have sprites with x != 0
  70. #define SPRL_TILE_OVFL 0x04 // tile limit exceeded on previous line
  71. #define SPRL_HAVE_MASK0 0x02 // have sprite with x == 0 in 1st slot
  72. #define SPRL_MASKED 0x01 // lo prio masking by sprite with x == 0 active
  73. unsigned char HighLnSpr[240][4+MAX_LINE_SPRITES+1]; // sprite_count, ^flags, tile_count, sprites_total, [spritep]..., last_width
  74. int rendstatus_old;
  75. int rendlines;
  76. static int skip_next_line=0;
  77. struct TileStrip
  78. {
  79. int nametab; // Position in VRAM of name table (for this tile line)
  80. int line; // Line number in pixels 0x000-0x3ff within the virtual tilemap
  81. int hscroll; // Horizontal scroll value in pixels for the line
  82. int xmask; // X-Mask (0x1f - 0x7f) for horizontal wraparound in the tilemap
  83. int *hc; // cache for high tile codes and their positions
  84. int cells; // cells (tiles) to draw (32 col mode doesn't need to update whole 320)
  85. };
  86. // stuff available in asm:
  87. #ifdef _ASM_DRAW_C
  88. void DrawWindow(int tstart, int tend, int prio, int sh,
  89. struct PicoEState *est);
  90. void DrawAllSprites(unsigned char *sprited, int prio, int sh,
  91. struct PicoEState *est);
  92. void DrawTilesFromCache(int *hc, int sh, int rlim,
  93. struct PicoEState *est);
  94. void DrawSpritesSHi(unsigned char *sprited, struct PicoEState *est);
  95. void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
  96. struct PicoEState *est);
  97. void *blockcpy(void *dst, const void *src, size_t n);
  98. void blockcpy_or(void *dst, void *src, size_t n, int pat);
  99. #else
  100. // utility
  101. void blockcpy_or(void *dst, void *src, size_t n, int pat)
  102. {
  103. unsigned char *pd = dst, *ps = src;
  104. for (; n; n--)
  105. *pd++ = (unsigned char) (*ps++ | pat);
  106. }
  107. #define blockcpy memcpy
  108. #endif
  109. #define TileNormMaker_(pix_func,ret) \
  110. { \
  111. unsigned int t; \
  112. \
  113. t = (pack&0x0000f000)>>12; pix_func(0); \
  114. t = (pack&0x00000f00)>> 8; pix_func(1); \
  115. t = (pack&0x000000f0)>> 4; pix_func(2); \
  116. t = (pack&0x0000000f) ; pix_func(3); \
  117. t = (pack&0xf0000000)>>28; pix_func(4); \
  118. t = (pack&0x0f000000)>>24; pix_func(5); \
  119. t = (pack&0x00f00000)>>20; pix_func(6); \
  120. t = (pack&0x000f0000)>>16; pix_func(7); \
  121. return ret; \
  122. }
  123. #define TileFlipMaker_(pix_func,ret) \
  124. { \
  125. unsigned int t; \
  126. \
  127. t = (pack&0x000f0000)>>16; pix_func(0); \
  128. t = (pack&0x00f00000)>>20; pix_func(1); \
  129. t = (pack&0x0f000000)>>24; pix_func(2); \
  130. t = (pack&0xf0000000)>>28; pix_func(3); \
  131. t = (pack&0x0000000f) ; pix_func(4); \
  132. t = (pack&0x000000f0)>> 4; pix_func(5); \
  133. t = (pack&0x00000f00)>> 8; pix_func(6); \
  134. t = (pack&0x0000f000)>>12; pix_func(7); \
  135. return ret; \
  136. }
  137. #define TileNormMaker(funcname, pix_func) \
  138. static void funcname(unsigned char *pd, unsigned int pack, int pal) \
  139. TileNormMaker_(pix_func,)
  140. #define TileFlipMaker(funcname, pix_func) \
  141. static void funcname(unsigned char *pd, unsigned int pack, int pal) \
  142. TileFlipMaker_(pix_func,)
  143. #define TileNormMakerAS(funcname, pix_func) \
  144. static unsigned funcname(unsigned m, unsigned char *pd, unsigned int pack, int pal) \
  145. TileNormMaker_(pix_func,m)
  146. #define TileFlipMakerAS(funcname, pix_func) \
  147. static unsigned funcname(unsigned m, unsigned char *pd, unsigned int pack, int pal) \
  148. TileFlipMaker_(pix_func,m)
  149. // draw layer or non-s/h sprite pixels (no operator colors)
  150. #define pix_just_write(x) \
  151. if (t) pd[x]=pal|t
  152. TileNormMaker(TileNorm, pix_just_write)
  153. TileFlipMaker(TileFlip, pix_just_write)
  154. #ifndef _ASM_DRAW_C
  155. // draw sprite pixels, process operator colors
  156. #define pix_sh(x) \
  157. if (t) { \
  158. if (t>=0xe) pd[x]|=(t-1)<<6; /* 80 shadow, 40 hilight */ \
  159. else pd[x]=pal|t; \
  160. }
  161. TileNormMaker(TileNormSH, pix_sh)
  162. TileFlipMaker(TileFlipSH, pix_sh)
  163. // draw sprite pixels, mark but don't process operator colors
  164. #define pix_sh_markop(x) \
  165. if (t) { \
  166. if (t>=0xe) pd[x]|=0x40; \
  167. else pd[x]=pal|t; \
  168. }
  169. TileNormMaker(TileNormSH_markop, pix_sh_markop)
  170. TileFlipMaker(TileFlipSH_markop, pix_sh_markop)
  171. #endif
  172. // draw low prio sprite operator pixels if visible (i.e. marked)
  173. #define pix_sh_onlyop(x) \
  174. if (t>=0xe && (pd[x]&0x40)) \
  175. pd[x]=(pd[x]&~0x40)|((t-1)<<6)
  176. #ifndef _ASM_DRAW_C
  177. TileNormMaker(TileNormSH_onlyop_lp, pix_sh_onlyop)
  178. TileFlipMaker(TileFlipSH_onlyop_lp, pix_sh_onlyop)
  179. #endif
  180. // AS: sprite mask bits in m shifted to bits 8-15, see DrawSpritesHiAS
  181. // draw high prio sprite pixels (AS)
  182. #define pix_as(x) \
  183. if (t && (m & (1<<(x+8)))) m &= ~(1<<(x+8)), pd[x] = pal | t
  184. TileNormMakerAS(TileNormAS, pix_as)
  185. TileFlipMakerAS(TileFlipAS, pix_as)
  186. // draw high prio sprite pixels, process operator colors (AS)
  187. // NB sprite+planes: h+s->n, h+[nh]->h, s+[nhs]->s, hence mask h before op
  188. #define pix_sh_as(x) \
  189. if (t && (m & (1<<(x+8)))) { \
  190. m &= ~(1<<(x+8)); \
  191. if (t>=0xe) pd[x]=(pd[x]&~0x40)|((t-1)<<6); \
  192. else pd[x] = pal | t; \
  193. }
  194. TileNormMakerAS(TileNormSH_AS, pix_sh_as)
  195. TileFlipMakerAS(TileFlipSH_AS, pix_sh_as)
  196. // draw only sprite operator pixels (AS)
  197. #define pix_sh_as_onlyop(x) \
  198. if (t && (m & (1<<(x+8)))) { \
  199. m &= ~(1<<(x+8)); \
  200. pix_sh_onlyop(x); \
  201. }
  202. TileNormMakerAS(TileNormSH_AS_onlyop_lp, pix_sh_as_onlyop)
  203. TileFlipMakerAS(TileFlipSH_AS_onlyop_lp, pix_sh_as_onlyop)
  204. // mark low prio sprite pixels (AS)
  205. #define pix_sh_as_onlymark(x) \
  206. if (t) m &= ~(1<<(x+8))
  207. TileNormMakerAS(TileNormAS_onlymark, pix_sh_as_onlymark)
  208. TileFlipMakerAS(TileFlipAS_onlymark, pix_sh_as_onlymark)
  209. #ifdef FORCE
  210. // NB s/h already resolved by non-forced drawing
  211. // forced both layer draw (through debug reg)
  212. #define pix_and(x) \
  213. pal |= 0xc0; /* leave s/h bits untouched in pixel "and" */ \
  214. pd[x] &= pal | t
  215. TileNormMaker(TileNorm_and, pix_and)
  216. TileFlipMaker(TileFlip_and, pix_and)
  217. // forced sprite draw (through debug reg)
  218. #define pix_sh_as_and(x) \
  219. pal |= 0xc0; /* leave s/h bits untouched in pixel "and" */ \
  220. if (m & (1<<(x+8))) { \
  221. m &= ~(1<<(x+8)); \
  222. if (t<0xe) pd[x] &= pal | t; \
  223. }
  224. TileNormMakerAS(TileNormSH_AS_and, pix_sh_as_and)
  225. TileFlipMakerAS(TileFlipSH_AS_and, pix_sh_as_and)
  226. #endif
  227. // --------------------------------------------
  228. #ifndef _ASM_DRAW_C
  229. static void DrawStrip(struct TileStrip *ts, int lflags, int cellskip)
  230. {
  231. unsigned char *pd = Pico.est.HighCol;
  232. int *hc = ts->hc;
  233. int tilex, dx, ty, cells;
  234. int oldcode = -1, blank = -1; // The tile we know is blank
  235. unsigned int pal = 0, pack = 0, sh;
  236. // Draw tiles across screen:
  237. sh = (lflags & LF_SH) << 6; // shadow
  238. tilex=((-ts->hscroll)>>3)+cellskip;
  239. ty=(ts->line&7)<<1; // Y-Offset into tile
  240. dx=((ts->hscroll-1)&7)+1;
  241. cells = ts->cells - cellskip;
  242. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  243. dx+=cellskip<<3;
  244. // int force = (lflags&LF_FORCE) << 13;
  245. for (; cells > 0; dx+=8, tilex++, cells--)
  246. {
  247. u32 code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];
  248. // code &= ~force; // forced always draw everything
  249. if (code == blank && !((code & 0x8000) && sh))
  250. continue;
  251. if (code!=oldcode) {
  252. oldcode = code;
  253. pack = 0;
  254. if (code != blank) {
  255. // Get tile address/2:
  256. u32 addr = ((code&0x7ff)<<4) + ty;
  257. if (code & 0x1000) addr ^= 0xe; // Y-flip
  258. pal = ((code>>9)&0x30) | sh; // shadow
  259. pack = *(unsigned int *)(PicoMem.vram + addr);
  260. if (!pack)
  261. blank = code;
  262. }
  263. }
  264. if (code & 0x8000) { // (un-forced) high priority tile
  265. int cval = code | (dx<<16) | (ty<<25);
  266. if (code & 0x1000) cval ^= 0xe<<25;
  267. *hc++ = cval, *hc++ = pack; // cache it
  268. } else if (code != blank) {
  269. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  270. else TileNorm(pd + dx, pack, pal);
  271. }
  272. }
  273. // terminate the cache list
  274. *hc = 0;
  275. // if oldcode wasn't changed, it means all layer is hi priority
  276. if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;
  277. }
  278. // this is messy
  279. static void DrawStripVSRam(struct TileStrip *ts, int plane_sh, int cellskip)
  280. {
  281. unsigned char *pd = Pico.est.HighCol;
  282. int *hc = ts->hc;
  283. int tilex, dx, ty = 0, addr = 0, cell = 0, nametabadd = 0;
  284. int oldcode = -1, blank = -1; // The tile we know is blank
  285. unsigned int pal = 0, scan = Pico.est.DrawScanline, sh, plane;
  286. // Draw tiles across screen:
  287. sh = (plane_sh & LF_SH) << 6; // shadow
  288. plane = (plane_sh & LF_PLANE); // plane to draw
  289. tilex=(-ts->hscroll)>>3;
  290. dx=((ts->hscroll-1)&7)+1;
  291. if (ts->hscroll & 0x0f) {
  292. int adj = ((ts->hscroll ^ dx) >> 3) & 1;
  293. cell -= adj + 1;
  294. ts->cells -= adj;
  295. PicoMem.vsram[0x3e] = PicoMem.vsram[0x3f] = plane_sh >> 16;
  296. }
  297. cell+=cellskip;
  298. tilex+=cellskip;
  299. dx+=cellskip<<3;
  300. // int force = (plane_sh&LF_FORCE) << 13;
  301. if ((cell&1)==1)
  302. {
  303. int line,vscroll;
  304. vscroll = PicoMem.vsram[plane + (cell&0x3e)];
  305. // Find the line in the name table
  306. line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
  307. nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
  308. ty=(line&7)<<1; // Y-Offset into tile
  309. }
  310. for (; cell < ts->cells; dx+=8,tilex++,cell++)
  311. {
  312. u32 code, pack;
  313. if ((cell&1)==0)
  314. {
  315. int line,vscroll;
  316. vscroll = PicoMem.vsram[plane + (cell&0x3e)];
  317. // Find the line in the name table
  318. line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
  319. nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
  320. ty=(line&7)<<1; // Y-Offset into tile
  321. }
  322. code= PicoMem.vram[ts->nametab + nametabadd + (tilex & ts->xmask)];
  323. // code &= ~force; // forced always draw everything
  324. code |= ty<<16; // add ty since that can change pixel row for every 2nd tile
  325. if (code == blank && !((code & 0x8000) && sh))
  326. continue;
  327. if (code!=oldcode) {
  328. oldcode = code;
  329. // Get tile address/2:
  330. addr = (code&0x7ff)<<4;
  331. pal = ((code>>9)&0x30) | sh; // shadow
  332. }
  333. pack = (code & 0x1000 ? ty^0xe : ty); // Y-flip
  334. pack = *(unsigned int *)(PicoMem.vram + addr+pack);
  335. if (!pack)
  336. blank = code;
  337. if (code & 0x8000) { // (un-forced) high priority tile
  338. int cval = (u16)code | (dx<<16) | (ty<<25);
  339. if (code & 0x1000) cval ^= 0xe<<25;
  340. *hc++ = cval, *hc++ = pack; // cache it
  341. } else if (code != blank) {
  342. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  343. else TileNorm(pd + dx, pack, pal);
  344. }
  345. }
  346. // terminate the cache list
  347. *hc = 0;
  348. if (oldcode == -1) Pico.est.rendstatus |= PDRAW_PLANE_HI_PRIO;
  349. }
  350. #endif
  351. #ifndef _ASM_DRAW_C
  352. static
  353. #endif
  354. void DrawStripInterlace(struct TileStrip *ts, int plane_sh)
  355. {
  356. unsigned char *pd = Pico.est.HighCol;
  357. int *hc = ts->hc;
  358. int tilex = 0, dx = 0, ty = 0, cells;
  359. int oldcode = -1, blank = -1; // The tile we know is blank
  360. unsigned int pal = 0, pack = 0, sh;
  361. // Draw tiles across screen:
  362. sh = (plane_sh & LF_SH) << 6; // shadow
  363. tilex=(-ts->hscroll)>>3;
  364. ty=(ts->line&15)<<1; // Y-Offset into tile
  365. dx=((ts->hscroll-1)&7)+1;
  366. cells = ts->cells;
  367. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  368. // int force = (plane_sh&LF_FORCE) << 13;
  369. for (; cells; dx+=8,tilex++,cells--)
  370. {
  371. u32 code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];
  372. // code &= ~force; // forced always draw everything
  373. if (code == blank && !(code & 0x8000))
  374. continue;
  375. if (code!=oldcode) {
  376. oldcode = code;
  377. pack = 0;
  378. if (code != blank) {
  379. // Get tile address/2:
  380. u32 addr = ((code&0x3ff)<<5) + ty;
  381. if (code & 0x1000) addr ^= 0x1e; // Y-flip
  382. pal = ((code>>9)&0x30) | sh; // shadow
  383. pack = *(unsigned int *)(PicoMem.vram + addr);
  384. if (!pack)
  385. blank = code;
  386. }
  387. }
  388. if (code & 0x8000) { // high priority tile
  389. if ((plane_sh&LF_SH) | (code!=blank)) {
  390. int cval = (code&0xfc00) | ((code&0x3ff)<<1) | (dx<<16) | (ty<<25);
  391. if (code & 0x1000) cval ^= 0x1e<<25;
  392. *hc++ = cval, *hc++ = pack; // cache it
  393. }
  394. continue;
  395. } else if (code != blank) {
  396. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  397. else TileNorm(pd + dx, pack, pal);
  398. }
  399. }
  400. // terminate the cache list
  401. *hc = 0;
  402. }
  403. // --------------------------------------------
  404. #ifndef _ASM_DRAW_C
  405. static void DrawLayer(int plane_sh, int *hcache, int cellskip, int maxcells,
  406. struct PicoEState *est)
  407. {
  408. struct PicoVideo *pvid=&Pico.video;
  409. const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
  410. struct TileStrip ts;
  411. int width, height, ymask;
  412. int vscroll, htab;
  413. ts.hc=hcache;
  414. ts.cells=maxcells;
  415. // Work out the TileStrip to draw
  416. // Work out the name table size: 32 64 or 128 tiles (0-3)
  417. width=pvid->reg[16];
  418. height=(width>>4)&3; width&=3;
  419. ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
  420. ymask=(height<<8)|0xff; // Y Mask in pixels
  421. switch (width) {
  422. case 1: ymask &= 0x1ff; break;
  423. case 2: ymask = 0x007; break;
  424. case 3: ymask = 0x0ff; break;
  425. }
  426. // Find name table:
  427. if (plane_sh&LF_PLANE) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
  428. else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
  429. htab=pvid->reg[13]<<9; // Horizontal scroll table address
  430. switch (pvid->reg[11]&3) {
  431. case 1: htab += (est->DrawScanline<<1) & 0x0f; break;
  432. case 2: htab += (est->DrawScanline<<1) & ~0x0f; break; // Offset by tile
  433. case 3: htab += (est->DrawScanline<<1); break; // Offset by line
  434. }
  435. htab+=plane_sh&LF_PLANE; // A or B
  436. // Get horizontal scroll value, will be masked later
  437. ts.hscroll = PicoMem.vram[htab & 0x7fff];
  438. if((pvid->reg[12]&6) == 6) {
  439. // interlace mode 2
  440. vscroll = PicoMem.vsram[plane_sh&LF_PLANE]; // Get vertical scroll value
  441. // Find the line in the name table
  442. ts.line=(vscroll+(est->DrawScanline<<1))&((ymask<<1)|1);
  443. ts.nametab+=(ts.line>>4)<<shift[width];
  444. DrawStripInterlace(&ts, plane_sh);
  445. } else if( pvid->reg[11]&4) {
  446. // shit, we have 2-cell column based vscroll
  447. // luckily this doesn't happen too often
  448. ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
  449. // vscroll value for leftmost cells in case of hscroll not on 16px boundary
  450. // XXX it's unclear what exactly the hw is doing. Continue reading where it
  451. // stopped last seems to work best (H40: 0x50 (wrap->0x00), H32 0x40).
  452. plane_sh |= PicoMem.vsram[(pvid->reg[12]&1?0x00:0x20) + (plane_sh&LF_PLANE)] << 16;
  453. DrawStripVSRam(&ts, plane_sh, cellskip);
  454. } else {
  455. vscroll = PicoMem.vsram[plane_sh&LF_PLANE]; // Get vertical scroll value
  456. // Find the line in the name table
  457. ts.line=(vscroll+est->DrawScanline)&ymask;
  458. ts.nametab+=(ts.line>>3)<<shift[width];
  459. DrawStrip(&ts, plane_sh, cellskip);
  460. }
  461. }
  462. // --------------------------------------------
  463. // tstart & tend are tile pair numbers
  464. static void DrawWindow(int tstart, int tend, int prio, int sh,
  465. struct PicoEState *est)
  466. {
  467. unsigned char *pd = Pico.est.HighCol;
  468. struct PicoVideo *pvid = &Pico.video;
  469. int tilex,ty,nametab,code=0;
  470. int blank=-1; // The tile we know is blank
  471. // Find name table line:
  472. if (pvid->reg[12]&1)
  473. {
  474. nametab=(pvid->reg[3]&0x3c)<<9; // 40-cell mode
  475. nametab+=(est->DrawScanline>>3)<<6;
  476. }
  477. else
  478. {
  479. nametab=(pvid->reg[3]&0x3e)<<9; // 32-cell mode
  480. nametab+=(est->DrawScanline>>3)<<5;
  481. }
  482. tilex=tstart<<1;
  483. if (prio && !(est->rendstatus & PDRAW_WND_DIFF_PRIO)) {
  484. // all tiles processed in low prio pass
  485. return;
  486. }
  487. tend<<=1;
  488. ty=(est->DrawScanline&7)<<1; // Y-Offset into tile
  489. // Draw tiles across screen:
  490. if (!sh)
  491. {
  492. for (; tilex < tend; tilex++)
  493. {
  494. unsigned int pack;
  495. int dx, addr;
  496. int pal;
  497. code = PicoMem.vram[nametab + tilex];
  498. if ((code>>15) != prio) {
  499. est->rendstatus |= PDRAW_WND_DIFF_PRIO;
  500. continue;
  501. }
  502. if (code==blank) continue;
  503. // Get tile address/2:
  504. addr=(code&0x7ff)<<4;
  505. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  506. pack = *(unsigned int *)(PicoMem.vram + addr);
  507. if (!pack) {
  508. blank = code;
  509. continue;
  510. }
  511. pal = ((code >> 9) & 0x30);
  512. dx = 8 + (tilex << 3);
  513. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  514. else TileNorm(pd + dx, pack, pal);
  515. }
  516. }
  517. else
  518. {
  519. for (; tilex < tend; tilex++)
  520. {
  521. unsigned int pack;
  522. int dx, addr;
  523. int pal;
  524. code = PicoMem.vram[nametab + tilex];
  525. if((code>>15) != prio) {
  526. est->rendstatus |= PDRAW_WND_DIFF_PRIO;
  527. continue;
  528. }
  529. pal=((code>>9)&0x30);
  530. if (prio) {
  531. int *zb = (int *)(est->HighCol+8+(tilex<<3));
  532. *zb++ &= 0x7f7f7f7f;
  533. *zb &= 0x7f7f7f7f;
  534. } else {
  535. pal |= 0x80;
  536. }
  537. if(code==blank) continue;
  538. // Get tile address/2:
  539. addr=(code&0x7ff)<<4;
  540. if (code&0x1000) addr+=14-ty; else addr+=ty; // Y-flip
  541. pack = *(unsigned int *)(PicoMem.vram + addr);
  542. if (!pack) {
  543. blank = code;
  544. continue;
  545. }
  546. dx = 8 + (tilex << 3);
  547. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  548. else TileNorm(pd + dx, pack, pal);
  549. }
  550. }
  551. }
  552. // --------------------------------------------
  553. static void DrawTilesFromCacheShPrep(void)
  554. {
  555. // as some layer has covered whole line with hi priority tiles,
  556. // we can process whole line and then act as if sh/hi mode was off,
  557. // but leave lo pri op sprite markers alone
  558. int c = 320/4, *zb = (int *)(Pico.est.HighCol+8);
  559. Pico.est.rendstatus |= PDRAW_SHHI_DONE;
  560. while (c--)
  561. {
  562. *zb++ &= 0x7f7f7f7f;
  563. }
  564. }
  565. static void DrawTilesFromCache(int *hc, int sh, int rlim, struct PicoEState *est)
  566. {
  567. unsigned char *pd = Pico.est.HighCol;
  568. int code, dx;
  569. unsigned int pack;
  570. int pal;
  571. // *ts->hc++ = code | (dx<<16) | (ty<<25); // cache it
  572. if (sh && (est->rendstatus & (PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO)))
  573. {
  574. if (!(est->rendstatus & PDRAW_SHHI_DONE))
  575. DrawTilesFromCacheShPrep();
  576. sh = 0;
  577. }
  578. if (!sh)
  579. {
  580. while ((code=*hc++)) {
  581. pack = *hc++;
  582. if (!pack)
  583. continue;
  584. dx = (code >> 16) & 0x1ff;
  585. pal = ((code >> 9) & 0x30);
  586. if (rlim-dx < 0)
  587. goto last_cut_tile;
  588. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  589. else TileNorm(pd + dx, pack, pal);
  590. }
  591. }
  592. else
  593. {
  594. while ((code=*hc++)) {
  595. unsigned char *zb;
  596. dx = (code >> 16) & 0x1ff;
  597. zb = est->HighCol+dx;
  598. *zb++ &= 0x7f; *zb++ &= 0x7f; *zb++ &= 0x7f; *zb++ &= 0x7f;
  599. *zb++ &= 0x7f; *zb++ &= 0x7f; *zb++ &= 0x7f; *zb++ &= 0x7f;
  600. pack = *hc++;
  601. if (!pack)
  602. continue;
  603. pal = ((code >> 9) & 0x30);
  604. if (rlim - dx < 0)
  605. goto last_cut_tile;
  606. if (code & 0x0800) TileFlip(pd + dx, pack, pal);
  607. else TileNorm(pd + dx, pack, pal);
  608. }
  609. }
  610. return;
  611. last_cut_tile:
  612. // for vertical window cutoff
  613. {
  614. unsigned int t;
  615. pd += dx;
  616. if (code&0x0800)
  617. {
  618. switch (rlim-dx+8)
  619. {
  620. case 7: t=pack&0x00000f00; if (t) pd[6]=(unsigned char)(pal|(t>> 8)); // "break" is left out intentionally
  621. case 6: t=pack&0x000000f0; if (t) pd[5]=(unsigned char)(pal|(t>> 4));
  622. case 5: t=pack&0x0000000f; if (t) pd[4]=(unsigned char)(pal|(t ));
  623. case 4: t=pack&0xf0000000; if (t) pd[3]=(unsigned char)(pal|(t>>28));
  624. case 3: t=pack&0x0f000000; if (t) pd[2]=(unsigned char)(pal|(t>>24));
  625. case 2: t=pack&0x00f00000; if (t) pd[1]=(unsigned char)(pal|(t>>20));
  626. case 1: t=pack&0x000f0000; if (t) pd[0]=(unsigned char)(pal|(t>>16));
  627. default: break;
  628. }
  629. }
  630. else
  631. {
  632. switch (rlim-dx+8)
  633. {
  634. case 7: t=pack&0x00f00000; if (t) pd[6]=(unsigned char)(pal|(t>>20));
  635. case 6: t=pack&0x0f000000; if (t) pd[5]=(unsigned char)(pal|(t>>24));
  636. case 5: t=pack&0xf0000000; if (t) pd[4]=(unsigned char)(pal|(t>>28));
  637. case 4: t=pack&0x0000000f; if (t) pd[3]=(unsigned char)(pal|(t ));
  638. case 3: t=pack&0x000000f0; if (t) pd[2]=(unsigned char)(pal|(t>> 4));
  639. case 2: t=pack&0x00000f00; if (t) pd[1]=(unsigned char)(pal|(t>> 8));
  640. case 1: t=pack&0x0000f000; if (t) pd[0]=(unsigned char)(pal|(t>>12));
  641. default: break;
  642. }
  643. }
  644. }
  645. }
  646. // --------------------------------------------
  647. // Index + 0 : hhhhvvvv ab--hhvv yyyyyyyy yyyyyyyy // a: offscreen h, b: offs. v, h: horiz. size
  648. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  649. static void DrawSprite(int *sprite, int sh, int w)
  650. {
  651. void (*fTileFunc)(unsigned char *pd, unsigned int pack, int pal);
  652. unsigned char *pd = Pico.est.HighCol;
  653. int width=0,height=0;
  654. int row=0,code=0;
  655. int pal;
  656. int tile=0,delta=0;
  657. int sx, sy;
  658. // parse the sprite data
  659. sy=sprite[0];
  660. code=sprite[1];
  661. sx=code>>16; // X
  662. width=sy>>28;
  663. height=(sy>>24)&7; // Width and height in tiles
  664. sy=(sy<<16)>>16; // Y
  665. row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
  666. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  667. tile=code + (row>>3); // Tile number increases going down
  668. delta=height; // Delta to increase tile by going right
  669. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  670. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  671. delta<<=4; // Delta of address
  672. pal=(code>>9)&0x30;
  673. pal|=sh<<7; // shadow
  674. if (sh && (code&0x6000) == 0x6000) {
  675. if(code&0x0800) fTileFunc=TileFlipSH_markop;
  676. else fTileFunc=TileNormSH_markop;
  677. } else {
  678. if(code&0x0800) fTileFunc=TileFlip;
  679. else fTileFunc=TileNorm;
  680. }
  681. if (w) width = w; // tile limit
  682. for (; width; width--,sx+=8,tile+=delta)
  683. {
  684. unsigned int pack;
  685. if(sx<=0) continue;
  686. if(sx>=328) break; // Offscreen
  687. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  688. fTileFunc(pd + sx, pack, pal);
  689. }
  690. }
  691. #endif
  692. static void DrawSpriteInterlace(unsigned int *sprite)
  693. {
  694. unsigned char *pd = Pico.est.HighCol;
  695. int width=0,height=0;
  696. int row=0,code=0;
  697. int pal;
  698. int tile=0,delta=0;
  699. int sx, sy;
  700. // parse the sprite data
  701. sy=sprite[0];
  702. height=sy>>24;
  703. sy=(sy&0x3ff)-0x100; // Y
  704. width=(height>>2)&3; height&=3;
  705. width++; height++; // Width and height in tiles
  706. row=(Pico.est.DrawScanline<<1)-sy; // Row of the sprite we are on
  707. code=sprite[1];
  708. sx=((code>>16)&0x1ff)-0x78; // X
  709. if (code&0x1000) row^=(16<<height)-1; // Flip Y
  710. tile=code&0x3ff; // Tile number
  711. tile+=row>>4; // Tile number increases going down
  712. delta=height; // Delta to increase tile by going right
  713. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  714. tile<<=5; tile+=(row&15)<<1; // Tile address
  715. delta<<=5; // Delta of address
  716. pal=((code>>9)&0x30); // Get palette pointer
  717. for (; width; width--,sx+=8,tile+=delta)
  718. {
  719. unsigned int pack;
  720. if(sx<=0) continue;
  721. if(sx>=328) break; // Offscreen
  722. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  723. if (code & 0x0800) TileFlip(pd + sx, pack, pal);
  724. else TileNorm(pd + sx, pack, pal);
  725. }
  726. }
  727. static NOINLINE void DrawAllSpritesInterlace(int pri, int sh)
  728. {
  729. struct PicoVideo *pvid=&Pico.video;
  730. int i,u,table,link=0,sline=Pico.est.DrawScanline<<1;
  731. unsigned int *sprites[80]; // Sprite index
  732. int max_sprites = Pico.video.reg[12]&1 ? 80 : 64;
  733. table=pvid->reg[5]&0x7f;
  734. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  735. table<<=8; // Get sprite table address/2
  736. for (i = u = 0; u < max_sprites && link < max_sprites; u++)
  737. {
  738. unsigned int *sprite;
  739. int code, sx, sy, height;
  740. sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  741. // get sprite info
  742. code = sprite[0];
  743. sx = sprite[1];
  744. if(((sx>>15)&1) != pri) goto nextsprite; // wrong priority sprite
  745. // check if it is on this line
  746. sy = (code&0x3ff)-0x100;
  747. height = (((code>>24)&3)+1)<<4;
  748. if((sline < sy) | (sline >= sy+height)) goto nextsprite; // no
  749. // check if sprite is not hidden offscreen
  750. sx = (sx>>16)&0x1ff;
  751. sx -= 0x78; // Get X coordinate + 8
  752. if((sx <= -8*3) | (sx >= 328)) goto nextsprite;
  753. // sprite is good, save it's pointer
  754. sprites[i++]=sprite;
  755. nextsprite:
  756. // Find next sprite
  757. link=(code>>16)&0x7f;
  758. if(!link) break; // End of sprites
  759. }
  760. // Go through sprites backwards:
  761. for (i-- ;i>=0; i--)
  762. DrawSpriteInterlace(sprites[i]);
  763. }
  764. #ifndef _ASM_DRAW_C
  765. /*
  766. * s/h drawing: lo_layers|40, lo_sprites|40 && mark_op,
  767. * hi_layers&=~40, hi_sprites
  768. *
  769. * Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
  770. * Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  771. */
  772. static void DrawSpritesSHi(unsigned char *sprited, const struct PicoEState *est)
  773. {
  774. static void (*tilefuncs[2][2][2])(unsigned char *, unsigned, int) = {
  775. { {NULL, NULL}, {TileNorm, TileFlip} },
  776. { {TileNormSH_onlyop_lp, TileFlipSH_onlyop_lp}, {TileNormSH, TileFlipSH} }
  777. }; // [sh?][hi?][flip?]
  778. void (*fTileFunc)(unsigned char *pd, unsigned int pack, int pal);
  779. unsigned char *pd = Pico.est.HighCol;
  780. unsigned char *p;
  781. int cnt, w;
  782. cnt = sprited[0] & 0x7f;
  783. if (cnt == 0) return;
  784. p = &sprited[4];
  785. if ((sprited[1] & (SPRL_TILE_OVFL|SPRL_HAVE_MASK0)) == (SPRL_TILE_OVFL|SPRL_HAVE_MASK0))
  786. return; // masking effective due to tile overflow
  787. // Go through sprites backwards:
  788. w = p[cnt]; // possibly clipped width of last sprite
  789. for (cnt--; cnt >= 0; cnt--, w = 0)
  790. {
  791. int *sprite, code, pal, tile, sx, sy;
  792. int offs, delta, width, height, row;
  793. offs = (p[cnt] & 0x7f) * 2;
  794. sprite = est->HighPreSpr + offs;
  795. code = sprite[1];
  796. pal = (code>>9)&0x30;
  797. fTileFunc = tilefuncs[pal == 0x30][!!(code & 0x8000)][!!(code & 0x800)];
  798. if (fTileFunc == NULL) continue; // non-operator low sprite, already drawn
  799. // parse remaining sprite data
  800. sy=sprite[0];
  801. sx=code>>16; // X
  802. width=sy>>28;
  803. height=(sy>>24)&7; // Width and height in tiles
  804. sy=(sy<<16)>>16; // Y
  805. row=est->DrawScanline-sy; // Row of the sprite we are on
  806. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  807. tile=code + (row>>3); // Tile number increases going down
  808. delta=height; // Delta to increase tile by going right
  809. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  810. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  811. delta<<=4; // Delta of address
  812. if (w) width = w; // tile limit
  813. for (; width; width--,sx+=8,tile+=delta)
  814. {
  815. unsigned int pack;
  816. if(sx<=0) continue;
  817. if(sx>=328) break; // Offscreen
  818. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  819. fTileFunc(pd + sx, pack, pal);
  820. }
  821. }
  822. }
  823. #endif // !_ASM_DRAW_C
  824. static void DrawSpritesHiAS(unsigned char *sprited, int sh)
  825. {
  826. static unsigned (*tilefuncs[2][2][2])(unsigned, unsigned char *, unsigned, int) = {
  827. { {TileNormAS_onlymark, TileFlipAS_onlymark}, {TileNormAS, TileFlipAS} },
  828. { {TileNormSH_AS_onlyop_lp, TileFlipSH_AS_onlyop_lp}, {TileNormSH_AS, TileFlipSH_AS} }
  829. }; // [sh?][hi?][flip?]
  830. unsigned (*fTileFunc)(unsigned m, unsigned char *pd, unsigned int pack, int pal);
  831. unsigned char *pd = Pico.est.HighCol;
  832. unsigned char mb[sizeof(DefHighCol)/8];
  833. unsigned char *p, *mp;
  834. unsigned m;
  835. int entry, cnt;
  836. cnt = sprited[0] & 0x7f;
  837. if (cnt == 0) return;
  838. memset(mb, 0xff, sizeof(mb));
  839. p = &sprited[4];
  840. if ((sprited[1] & (SPRL_TILE_OVFL|SPRL_HAVE_MASK0)) == (SPRL_TILE_OVFL|SPRL_HAVE_MASK0))
  841. return; // masking effective due to tile overflow
  842. // Go through sprites:
  843. for (entry = 0; entry < cnt; entry++)
  844. {
  845. int *sprite, code, pal, tile, sx, sy;
  846. int offs, delta, width, height, row;
  847. offs = (p[entry] & 0x7f) * 2;
  848. sprite = HighPreSpr + offs;
  849. code = sprite[1];
  850. pal = (code>>9)&0x30;
  851. fTileFunc = tilefuncs[(sh && pal == 0x30)][!!(code&0x8000)][!!(code&0x800)];
  852. // parse remaining sprite data
  853. sy=sprite[0];
  854. sx=code>>16; // X
  855. width=sy>>28;
  856. height=(sy>>24)&7; // Width and height in tiles
  857. sy=(sy<<16)>>16; // Y
  858. row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
  859. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  860. tile=code + (row>>3); // Tile number increases going down
  861. delta=height; // Delta to increase tile by going right
  862. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  863. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  864. delta<<=4; // Delta of address
  865. if (entry+1 == cnt) width = p[entry+1]; // last sprite width limited?
  866. while (sx <= 0 && width) width--, sx+=8, tile+=delta; // Offscreen
  867. mp = mb+(sx>>3);
  868. for (m = *mp; width; width--, sx+=8, tile+=delta, *mp++ = m, m >>= 8)
  869. {
  870. unsigned int pack;
  871. if(sx>=328) break; // Offscreen
  872. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  873. m |= mp[1] << 8; // next mask byte
  874. // shift mask bits to bits 8-15 for easier load/store handling
  875. m = fTileFunc(m << (8-(sx&0x7)), pd + sx, pack, pal) >> (8-(sx&0x7));
  876. }
  877. *mp = m; // write last mask byte
  878. }
  879. }
  880. #ifdef FORCE
  881. // NB lots of duplicate code, all for the sake of a small performance gain.
  882. static void DrawStripForced(struct TileStrip *ts, int cellskip)
  883. {
  884. unsigned char *pd = Pico.est.HighCol;
  885. int tilex, dx, ty, code=0, addr=0, cells;
  886. int oldcode = -1;
  887. int pal = 0;
  888. // Draw tiles across screen:
  889. tilex=((-ts->hscroll)>>3)+cellskip;
  890. ty=(ts->line&7)<<1; // Y-Offset into tile
  891. dx=((ts->hscroll-1)&7)+1;
  892. cells = ts->cells - cellskip;
  893. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  894. dx+=cellskip<<3;
  895. for (; cells > 0; dx+=8, tilex++, cells--)
  896. {
  897. unsigned int pack;
  898. code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];
  899. if (code!=oldcode) {
  900. oldcode = code;
  901. // Get tile address/2:
  902. addr = ((code&0x7ff)<<4) + ty;
  903. if (code & 0x1000) addr^=0xe; // Y-flip
  904. pal = (code>>9)&0x30;
  905. }
  906. pack = *(unsigned int *)(PicoMem.vram + addr);
  907. if (code & 0x0800) TileFlip_and(pd + dx, pack, pal);
  908. else TileNorm_and(pd + dx, pack, pal);
  909. }
  910. }
  911. static void DrawStripVSRamForced(struct TileStrip *ts, int plane_sh, int cellskip)
  912. {
  913. unsigned char *pd = Pico.est.HighCol;
  914. int tilex, dx, ty=0, code=0, addr=0, cell=0, nametabadd=0;
  915. int oldcode=-1;
  916. int pal=0, scan=Pico.est.DrawScanline, plane;
  917. // Draw tiles across screen:
  918. plane = plane_sh & LF_PLANE;
  919. tilex=(-ts->hscroll)>>3;
  920. dx=((ts->hscroll-1)&7)+1;
  921. if (ts->hscroll & 0x0f) {
  922. int adj = ((ts->hscroll ^ dx) >> 3) & 1;
  923. cell -= adj + 1;
  924. ts->cells -= adj;
  925. PicoMem.vsram[0x3e] = PicoMem.vsram[0x3f] = plane_sh >> 16;
  926. }
  927. cell+=cellskip;
  928. tilex+=cellskip;
  929. dx+=cellskip<<3;
  930. if ((cell&1)==1)
  931. {
  932. int line,vscroll;
  933. vscroll = PicoMem.vsram[plane + (cell&0x3e)];
  934. // Find the line in the name table
  935. line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
  936. nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
  937. ty=(line&7)<<1; // Y-Offset into tile
  938. }
  939. for (; cell < ts->cells; dx+=8,tilex++,cell++)
  940. {
  941. unsigned int pack;
  942. if ((cell&1)==0)
  943. {
  944. int line,vscroll;
  945. vscroll = PicoMem.vsram[plane + (cell&0x3e)];
  946. // Find the line in the name table
  947. line=(vscroll+scan)&ts->line&0xffff; // ts->line is really ymask ..
  948. nametabadd=(line>>3)<<(ts->line>>24); // .. and shift[width]
  949. ty=(line&7)<<1; // Y-Offset into tile
  950. }
  951. code=PicoMem.vram[ts->nametab+nametabadd+(tilex&ts->xmask)];
  952. if (code!=oldcode) {
  953. oldcode = code;
  954. // Get tile address/2:
  955. addr=(code&0x7ff)<<4;
  956. pal = (code>>9)&0x30; // shadow
  957. }
  958. pack = code & 0x1000 ? ty^0xe : ty; // Y-flip
  959. pack = *(unsigned int *)(PicoMem.vram + addr+pack);
  960. if (code & 0x0800) TileFlip_and(pd + dx, pack, pal);
  961. else TileNorm_and(pd + dx, pack, pal);
  962. }
  963. }
  964. void DrawStripInterlaceForced(struct TileStrip *ts)
  965. {
  966. unsigned char *pd = Pico.est.HighCol;
  967. int tilex = 0, dx = 0, ty = 0, cells;
  968. int oldcode = -1;
  969. unsigned int pal = 0, pack = 0;
  970. // Draw tiles across screen:
  971. tilex=(-ts->hscroll)>>3;
  972. ty=(ts->line&15)<<1; // Y-Offset into tile
  973. dx=((ts->hscroll-1)&7)+1;
  974. cells = ts->cells;
  975. if(dx != 8) cells++; // have hscroll, need to draw 1 cell more
  976. for (; cells; dx+=8,tilex++,cells--)
  977. {
  978. u32 code = PicoMem.vram[ts->nametab + (tilex & ts->xmask)];
  979. if (code!=oldcode) {
  980. oldcode = code;
  981. // Get tile address/2:
  982. u32 addr = ((code&0x3ff)<<5) + ty;
  983. if (code & 0x1000) addr ^= 0x1e; // Y-flip
  984. pal = (code>>9)&0x30; // shadow
  985. pack = *(unsigned int *)(PicoMem.vram + addr);
  986. }
  987. if (code & 0x0800) TileFlip_and(pd + dx, pack, pal);
  988. else TileNorm_and(pd + dx, pack, pal);
  989. }
  990. }
  991. // XXX only duplicated to avoid ARM asm hassles
  992. static void DrawLayerForced(int plane_sh, int cellskip, int maxcells,
  993. struct PicoEState *est)
  994. {
  995. struct PicoVideo *pvid=&Pico.video;
  996. const char shift[4]={5,6,5,7}; // 32,64 or 128 sized tilemaps (2 is invalid)
  997. struct TileStrip ts;
  998. int width, height, ymask;
  999. int vscroll, htab;
  1000. ts.cells=maxcells;
  1001. // Work out the TileStrip to draw
  1002. // Work out the name table size: 32 64 or 128 tiles (0-3)
  1003. width=pvid->reg[16];
  1004. height=(width>>4)&3; width&=3;
  1005. ts.xmask=(1<<shift[width])-1; // X Mask in tiles (0x1f-0x7f)
  1006. ymask=(height<<8)|0xff; // Y Mask in pixels
  1007. switch (width) {
  1008. case 1: ymask &= 0x1ff; break;
  1009. case 2: ymask = 0x007; break;
  1010. case 3: ymask = 0x0ff; break;
  1011. }
  1012. // Find name table:
  1013. if (plane_sh&1) ts.nametab=(pvid->reg[4]&0x07)<<12; // B
  1014. else ts.nametab=(pvid->reg[2]&0x38)<< 9; // A
  1015. htab=pvid->reg[13]<<9; // Horizontal scroll table address
  1016. switch (pvid->reg[11]&3) {
  1017. case 1: htab += (est->DrawScanline<<1) & 0x0f; break;
  1018. case 2: htab += (est->DrawScanline<<1) & ~0x0f; break; // Offset by tile
  1019. case 3: htab += (est->DrawScanline<<1); break; // Offset by line
  1020. }
  1021. htab+=plane_sh&1; // A or B
  1022. // Get horizontal scroll value, will be masked later
  1023. ts.hscroll = PicoMem.vram[htab & 0x7fff];
  1024. if((pvid->reg[12]&6) == 6) {
  1025. // interlace mode 2
  1026. vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value
  1027. // Find the line in the name table
  1028. ts.line=(vscroll+(est->DrawScanline<<1))&((ymask<<1)|1);
  1029. ts.nametab+=(ts.line>>4)<<shift[width];
  1030. DrawStripInterlaceForced(&ts);
  1031. } else if( pvid->reg[11]&4) {
  1032. // shit, we have 2-cell column based vscroll
  1033. // luckily this doesn't happen too often
  1034. ts.line=ymask|(shift[width]<<24); // save some stuff instead of line
  1035. // vscroll value for leftmost cells in case of hscroll not on 16px boundary
  1036. // XXX it's unclear what exactly the hw is doing. Continue reading where it
  1037. // stopped last seems to work best (H40: 0x50 (wrap->0x00), H32 0x40).
  1038. plane_sh |= PicoMem.vsram[(pvid->reg[12]&1?0x00:0x20) + (plane_sh&1)] << 16;
  1039. DrawStripVSRamForced(&ts, plane_sh, cellskip);
  1040. } else {
  1041. vscroll = PicoMem.vsram[plane_sh & 1]; // Get vertical scroll value
  1042. // Find the line in the name table
  1043. ts.line=(vscroll+est->DrawScanline)&ymask;
  1044. ts.nametab+=(ts.line>>3)<<shift[width];
  1045. DrawStripForced(&ts, cellskip);
  1046. }
  1047. }
  1048. static void DrawSpritesForced(unsigned char *sprited)
  1049. {
  1050. unsigned (*fTileFunc)(unsigned m, unsigned char *pd, unsigned int pack, int pal);
  1051. unsigned char *pd = Pico.est.HighCol;
  1052. unsigned char mb[sizeof(DefHighCol)/8];
  1053. unsigned char *p, *mp;
  1054. unsigned m;
  1055. int entry, cnt;
  1056. cnt = sprited[0] & 0x7f;
  1057. if (cnt == 0) { memset(pd, 0, sizeof(DefHighCol)); return; }
  1058. memset(mb, 0xff, sizeof(mb));
  1059. p = &sprited[4];
  1060. if ((sprited[1] & (SPRL_TILE_OVFL|SPRL_HAVE_MASK0)) == (SPRL_TILE_OVFL|SPRL_HAVE_MASK0))
  1061. return; // masking effective due to tile overflow
  1062. // Go through sprites:
  1063. for (entry = 0; entry < cnt; entry++)
  1064. {
  1065. int *sprite, code, pal, tile, sx, sy;
  1066. int offs, delta, width, height, row;
  1067. offs = (p[entry] & 0x7f) * 2;
  1068. sprite = HighPreSpr + offs;
  1069. code = sprite[1];
  1070. pal = (code>>9)&0x30;
  1071. if (code&0x800) fTileFunc = TileFlipSH_AS_and;
  1072. else fTileFunc = TileNormSH_AS_and;
  1073. // parse remaining sprite data
  1074. sy=sprite[0];
  1075. sx=code>>16; // X
  1076. width=sy>>28;
  1077. height=(sy>>24)&7; // Width and height in tiles
  1078. sy=(sy<<16)>>16; // Y
  1079. row=Pico.est.DrawScanline-sy; // Row of the sprite we are on
  1080. if (code&0x1000) row=(height<<3)-1-row; // Flip Y
  1081. tile=code + (row>>3); // Tile number increases going down
  1082. delta=height; // Delta to increase tile by going right
  1083. if (code&0x0800) { tile+=delta*(width-1); delta=-delta; } // Flip X
  1084. tile &= 0x7ff; tile<<=4; tile+=(row&7)<<1; // Tile address
  1085. delta<<=4; // Delta of address
  1086. if (entry+1 == cnt) width = p[entry+1]; // last sprite width limited?
  1087. while (sx <= 0 && width) width--, sx+=8, tile+=delta; // Offscreen
  1088. mp = mb+(sx>>3);
  1089. for (m = *mp; width; width--, sx+=8, tile+=delta, *mp++ = m, m >>= 8)
  1090. {
  1091. unsigned int pack;
  1092. if(sx>=328) break; // Offscreen
  1093. pack = *(unsigned int *)(PicoMem.vram + (tile & 0x7fff));
  1094. m |= mp[1] << 8; // next mask byte
  1095. // shift mask bits to bits 8-15 for easier load/store handling
  1096. m = fTileFunc(m << (8-(sx&0x7)), pd + sx, pack, pal) >> (8-(sx&0x7));
  1097. }
  1098. *mp = m; // write last mask byte
  1099. }
  1100. // anything not covered by a sprite is off (XXX or bg?)
  1101. for (cnt = 1; cnt < sizeof(mb)-1; cnt++)
  1102. if (mb[cnt] == 0xff) {
  1103. *(u32 *)(pd+8*cnt+0) = 0;
  1104. *(u32 *)(pd+8*cnt+4) = 0;
  1105. } else if (mb[cnt])
  1106. for (m = 0; m < 8; m++)
  1107. if (mb[cnt] & (1<<m))
  1108. pd[8*cnt+m] = 0;
  1109. }
  1110. #endif
  1111. // Index + 0 : ----hhvv -lllllll -------y yyyyyyyy
  1112. // Index + 4 : -------x xxxxxxxx pccvhnnn nnnnnnnn
  1113. // v
  1114. // Index + 0 : hhhhvvvv ----hhvv yyyyyyyy yyyyyyyy // v, h: vert./horiz. size
  1115. // Index + 4 : xxxxxxxx xxxxxxxx pccvhnnn nnnnnnnn // x: x coord + 8
  1116. static NOINLINE void PrepareSprites(int max_lines)
  1117. {
  1118. const struct PicoVideo *pvid=&Pico.video;
  1119. const struct PicoEState *est=&Pico.est;
  1120. int u,link=0,sh;
  1121. int table=0;
  1122. int *pd = HighPreSpr;
  1123. int max_sprites = 80, max_width = 328;
  1124. int max_line_sprites = 20; // 20 sprites, 40 tiles
  1125. if (!(Pico.video.reg[12]&1))
  1126. max_sprites = 64, max_line_sprites = 16, max_width = 264;
  1127. if (PicoIn.opt & POPT_DIS_SPRITE_LIM)
  1128. max_line_sprites = MAX_LINE_SPRITES;
  1129. sh = Pico.video.reg[0xC]&8; // shadow/hilight?
  1130. table=pvid->reg[5]&0x7f;
  1131. if (pvid->reg[12]&1) table&=0x7e; // Lowest bit 0 in 40-cell mode
  1132. table<<=8; // Get sprite table address/2
  1133. for (u = est->DrawScanline; u < max_lines; u++)
  1134. *((int *)&HighLnSpr[u][0]) = 0;
  1135. for (u = 0; u < max_sprites && link < max_sprites; u++)
  1136. {
  1137. unsigned int *sprite;
  1138. int code, code2, sx, sy, hv, height, width;
  1139. sprite=(unsigned int *)(PicoMem.vram+((table+(link<<2))&0x7ffc)); // Find sprite
  1140. // parse sprite info. the 1st half comes from the VDPs internal cache,
  1141. // the 2nd half is read from VRAM
  1142. code = VdpSATCache[link]; // normally but not always equal to sprite[0]
  1143. sy = (code&0x1ff)-0x80;
  1144. hv = (code>>24)&0xf;
  1145. height = (hv&3)+1;
  1146. width = (hv>>2)+1;
  1147. code2 = sprite[1];
  1148. sx = (code2>>16)&0x1ff;
  1149. sx -= 0x78; // Get X coordinate + 8
  1150. if (sy < max_lines && sy + (height<<3) >= est->DrawScanline) // sprite onscreen (y)?
  1151. {
  1152. int entry, y, w, sx_min, onscr_x, maybe_op = 0;
  1153. sx_min = 8-(width<<3);
  1154. onscr_x = sx_min < sx && sx < max_width;
  1155. if (sh && (code2 & 0x6000) == 0x6000)
  1156. maybe_op = SPRL_MAY_HAVE_OP;
  1157. entry = ((pd - HighPreSpr) / 2) | ((code2>>8)&0x80);
  1158. y = (sy >= est->DrawScanline) ? sy : est->DrawScanline;
  1159. for (; y < sy + (height<<3) && y < max_lines; y++)
  1160. {
  1161. unsigned char *p = &HighLnSpr[y][0];
  1162. int cnt = p[0];
  1163. if (p[3] >= max_line_sprites) continue; // sprite limit?
  1164. if (p[1] & SPRL_MASKED) continue; // masked?
  1165. w = width;
  1166. if (p[2] + width > max_line_sprites*2) { // tile limit?
  1167. if (y+1 < 240) HighLnSpr[y+1][1] |= SPRL_TILE_OVFL;
  1168. if (p[2] >= max_line_sprites*2) continue;
  1169. w = max_line_sprites*2 - p[2];
  1170. }
  1171. p[2] += w;
  1172. p[3] ++;
  1173. if (sx == -0x78) {
  1174. if (p[1] & (SPRL_HAVE_X|SPRL_TILE_OVFL))
  1175. p[1] |= SPRL_MASKED; // masked, no more sprites for this line
  1176. if (!(p[1] & SPRL_HAVE_X) && cnt == 0)
  1177. p[1] |= SPRL_HAVE_MASK0; // 1st sprite is masking
  1178. } else
  1179. p[1] |= SPRL_HAVE_X;
  1180. if (!onscr_x) continue; // offscreen x
  1181. p[4+cnt] = entry;
  1182. p[5+cnt] = w; // width clipped by tile limit for sprite renderer
  1183. p[0] = cnt + 1;
  1184. p[1] |= (entry & 0x80) ? SPRL_HAVE_HI : SPRL_HAVE_LO;
  1185. p[1] |= maybe_op; // there might be op sprites on this line
  1186. if (cnt > 0 && (code2 & 0x8000) && !(p[4+cnt-1]&0x80))
  1187. p[1] |= SPRL_LO_ABOVE_HI;
  1188. }
  1189. }
  1190. *pd++ = (width<<28)|(height<<24)|(hv<<16)|((unsigned short)sy);
  1191. *pd++ = (sx<<16)|((unsigned short)code2);
  1192. // Find next sprite
  1193. link=(code>>16)&0x7f;
  1194. if (!link) break; // End of sprites
  1195. }
  1196. *pd = 0;
  1197. #if 0
  1198. for (u = 0; u < max_lines; u++)
  1199. {
  1200. int y;
  1201. printf("c%03i: f %x c %2i/%2i w %2i: ", u, HighLnSpr[u][1],
  1202. HighLnSpr[u][0], HighLnSpr[u][3], HighLnSpr[u][2]);
  1203. for (y = 0; y < HighLnSpr[u][0]; y++) {
  1204. int *sp = HighPreSpr + (HighLnSpr[u][y+4]&0x7f) * 2;
  1205. printf(" %i(%x/%x)", HighLnSpr[u][y+4],sp[0],sp[1]);
  1206. }
  1207. printf("\n");
  1208. }
  1209. #endif
  1210. }
  1211. #ifndef _ASM_DRAW_C
  1212. static void DrawAllSprites(unsigned char *sprited, int prio, int sh,
  1213. struct PicoEState *est)
  1214. {
  1215. unsigned char *p;
  1216. int cnt, w;
  1217. cnt = sprited[0] & 0x7f;
  1218. if (cnt == 0) return;
  1219. p = &sprited[4];
  1220. if ((sprited[1] & (SPRL_TILE_OVFL|SPRL_HAVE_MASK0)) == (SPRL_TILE_OVFL|SPRL_HAVE_MASK0))
  1221. return; // masking effective due to tile overflow
  1222. // Go through sprites backwards:
  1223. w = p[cnt]; // possibly clipped width of last sprite
  1224. for (cnt--; cnt >= 0; cnt--, w = 0)
  1225. {
  1226. int *sp = HighPreSpr + (p[cnt]&0x7f) * 2;
  1227. if ((p[cnt] >> 7) != prio) continue;
  1228. DrawSprite(sp, sh, w);
  1229. }
  1230. }
  1231. // --------------------------------------------
  1232. void BackFill(int reg7, int sh, struct PicoEState *est)
  1233. {
  1234. unsigned int back;
  1235. // Start with a blank scanline (background colour):
  1236. back=reg7&0x3f;
  1237. back|=sh<<7; // shadow
  1238. back|=back<<8;
  1239. back|=back<<16;
  1240. memset32((int *)(est->HighCol+8), back, 320/4);
  1241. }
  1242. #endif
  1243. // --------------------------------------------
  1244. void PicoDoHighPal555_8bit(int sh, int line, struct PicoEState *est)
  1245. {
  1246. unsigned int *spal, *dpal;
  1247. unsigned int cnt = (sh ? 1 : est->SonicPalCount+1);
  1248. unsigned int t, i;
  1249. // reset dirty only if there are no outstanding changes
  1250. if (Pico.m.dirtyPal == 2)
  1251. Pico.m.dirtyPal = 0;
  1252. // In Sonic render mode palettes were backuped in SonicPal
  1253. spal = (void *)est->SonicPal;
  1254. dpal = (void *)est->HighPal;
  1255. // additional palettes stored after in-frame changes
  1256. for (i = 0; i < cnt * 0x40 / 2; i++) {
  1257. t = spal[i];
  1258. #ifdef USE_BGR555
  1259. t = ((t & 0x000e000e)<< 1) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)<<4);
  1260. #else
  1261. t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);
  1262. #endif
  1263. // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.
  1264. // otherwise intensity difference between this and s/h will be wrong
  1265. t |= (t >> 4) & 0x08610861; // 0x18e318e3
  1266. dpal[i] = dpal[0xc0/2 + i] = t;
  1267. }
  1268. // norm: xxx0, sh: 0xxx, hi: 0xxx + 7
  1269. if (sh)
  1270. {
  1271. // shadowed pixels
  1272. for (i = 0; i < 0x40 / 2; i++)
  1273. dpal[0x80/2 + i] = (dpal[i] >> 1) & 0x738e738e;
  1274. // hilighted pixels
  1275. for (i = 0; i < 0x40 / 2; i++) {
  1276. t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;
  1277. t |= (t >> 4) & 0x08610861;
  1278. dpal[0x40/2 + i] = t;
  1279. }
  1280. // shadowed pixels in color 14 always appear normal (hw bug?)
  1281. unsigned short *hpal = est->HighPal;
  1282. hpal[0x80 + 0x0e] = hpal[0x0e];
  1283. hpal[0x80 + 0x1e] = hpal[0x1e];
  1284. hpal[0x80 + 0x2e] = hpal[0x2e];
  1285. }
  1286. }
  1287. #ifndef _ASM_DRAW_C
  1288. void PicoDoHighPal555(int sh, int line, struct PicoEState *est)
  1289. {
  1290. unsigned int *spal, *dpal;
  1291. unsigned int t, i;
  1292. Pico.m.dirtyPal = 0;
  1293. spal = (void *)PicoMem.cram;
  1294. dpal = (void *)est->HighPal;
  1295. for (i = 0; i < 0x40 / 2; i++) {
  1296. t = spal[i];
  1297. #ifdef USE_BGR555
  1298. t = ((t & 0x0e000e00)<< 3) | ((t & 0x00e000e0)<<2) | ((t & 0x000e000e)<<1);
  1299. #else
  1300. t = ((t & 0x000e000e)<<12) | ((t & 0x00e000e0)<<3) | ((t & 0x0e000e00)>>7);
  1301. #endif
  1302. // treat it like it was 4-bit per channel, since in s/h mode it somewhat is that.
  1303. // otherwise intensity difference between this and s/h will be wrong
  1304. t |= (t >> 4) & 0x08610861; // 0x18e318e3
  1305. dpal[i] = dpal[0xc0/2 + i] = t;
  1306. }
  1307. // norm: xxx0, sh: 0xxx, hi: 0xxx + 7
  1308. if (sh)
  1309. {
  1310. // shadowed pixels
  1311. for (i = 0; i < 0x40 / 2; i++)
  1312. dpal[0x80/2 + i] = (dpal[i] >> 1) & 0x738e738e;
  1313. // hilighted pixels
  1314. for (i = 0; i < 0x40 / 2; i++) {
  1315. t = ((dpal[i] >> 1) & 0x738e738e) + 0x738e738e; // 0x7bef7bef;
  1316. t |= (t >> 4) & 0x08610861;
  1317. dpal[0x40/2 + i] = t;
  1318. }
  1319. // shadowed pixels in color 14 always appear normal (hw bug?)
  1320. unsigned short *hpal = est->HighPal;
  1321. hpal[0x80 + 0x0e] = hpal[0x0e];
  1322. hpal[0x80 + 0x1e] = hpal[0x1e];
  1323. hpal[0x80 + 0x2e] = hpal[0x2e];
  1324. }
  1325. }
  1326. void FinalizeLine555(int sh, int line, struct PicoEState *est)
  1327. {
  1328. unsigned short *pd=est->DrawLineDest;
  1329. unsigned char *ps=est->HighCol+8;
  1330. unsigned short *pal=est->HighPal;
  1331. int len;
  1332. PicoDrawUpdateHighPal();
  1333. if (Pico.video.reg[12]&1) {
  1334. len = 320;
  1335. } else {
  1336. if (!(PicoIn.opt&POPT_DIS_32C_BORDER)) pd+=32;
  1337. len = 256;
  1338. }
  1339. {
  1340. #if 1
  1341. int i;
  1342. for (i = len; i > 0; i-=4) {
  1343. *pd++ = pal[*ps++];
  1344. *pd++ = pal[*ps++];
  1345. *pd++ = pal[*ps++];
  1346. *pd++ = pal[*ps++];
  1347. }
  1348. #else
  1349. extern void amips_clut(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  1350. extern void amips_clut_6bit(unsigned short *dst, unsigned char *src, unsigned short *pal, int count);
  1351. if (!sh)
  1352. amips_clut_6bit(pd, ps, pal, len);
  1353. else amips_clut(pd, ps, pal, len);
  1354. #endif
  1355. }
  1356. }
  1357. #endif
  1358. static void FinalizeLine8bit(int sh, int line, struct PicoEState *est)
  1359. {
  1360. unsigned char *pd = est->DrawLineDest;
  1361. int len;
  1362. static int dirty_line;
  1363. if (Pico.m.dirtyPal == 1)
  1364. {
  1365. // a hack for mid-frame palette changes
  1366. if (!(est->rendstatus & PDRAW_SONIC_MODE) | (line - dirty_line > 4)) {
  1367. // store a maximum of 2 additional palettes in SonicPal
  1368. if (est->SonicPalCount < 2)
  1369. est->SonicPalCount ++;
  1370. dirty_line = line;
  1371. est->rendstatus |= PDRAW_SONIC_MODE;
  1372. }
  1373. blockcpy(est->SonicPal+est->SonicPalCount*0x40, PicoMem.cram, 0x40*2);
  1374. Pico.m.dirtyPal = 2;
  1375. }
  1376. if (Pico.video.reg[12]&1) {
  1377. len = 320;
  1378. } else {
  1379. if (!(PicoIn.opt & POPT_DIS_32C_BORDER))
  1380. pd += 32;
  1381. len = 256;
  1382. }
  1383. if (DrawLineDestBase == HighColBase) {
  1384. if (!sh && (est->rendstatus & PDRAW_SONIC_MODE))
  1385. blockcpy_or(pd+8, est->HighCol+8, len, est->SonicPalCount*0x40);
  1386. } else if (!sh && (est->rendstatus & PDRAW_SONIC_MODE)) {
  1387. // select active backup palette
  1388. blockcpy_or(pd, est->HighCol+8, len, est->SonicPalCount*0x40);
  1389. } else {
  1390. blockcpy(pd, est->HighCol+8, len);
  1391. }
  1392. }
  1393. static void (*FinalizeLine)(int sh, int line, struct PicoEState *est);
  1394. // --------------------------------------------
  1395. static int DrawDisplay(int sh)
  1396. {
  1397. struct PicoEState *est=&Pico.est;
  1398. unsigned char *sprited = &HighLnSpr[est->DrawScanline][0];
  1399. struct PicoVideo *pvid=&Pico.video;
  1400. int win=0, edge=0, hvwind=0, lflags;
  1401. int maxw, maxcells;
  1402. est->rendstatus &= ~(PDRAW_SHHI_DONE|PDRAW_PLANE_HI_PRIO|PDRAW_WND_DIFF_PRIO);
  1403. if (pvid->reg[12]&1) {
  1404. maxw = 328; maxcells = 40;
  1405. } else {
  1406. maxw = 264; maxcells = 32;
  1407. }
  1408. // Find out if the window is on this line:
  1409. win=pvid->reg[0x12];
  1410. edge=(win&0x1f)<<3;
  1411. if (win&0x80) { if (est->DrawScanline>=edge) hvwind=1; }
  1412. else { if (est->DrawScanline< edge) hvwind=1; }
  1413. if (!hvwind) // we might have a vertical window here
  1414. {
  1415. win=pvid->reg[0x11];
  1416. edge=win&0x1f;
  1417. if (win&0x80) {
  1418. if (!edge) hvwind=1;
  1419. else if(edge < (maxcells>>1)) hvwind=2;
  1420. } else {
  1421. if (!edge);
  1422. else if(edge < (maxcells>>1)) hvwind=2;
  1423. else hvwind=1;
  1424. }
  1425. }
  1426. /* - layer B low - */
  1427. if (!(pvid->debug_p & PVD_KILL_B)) {
  1428. lflags = LF_PLANE_B | (sh<<1);
  1429. DrawLayer(lflags, HighCacheB, 0, maxcells, est);
  1430. }
  1431. /* - layer A low - */
  1432. lflags = LF_PLANE_A | (sh<<1);
  1433. if (pvid->debug_p & PVD_KILL_A)
  1434. ;
  1435. else if (hvwind == 1)
  1436. DrawWindow(0, maxcells>>1, 0, sh, est);
  1437. else if (hvwind == 2) {
  1438. DrawLayer(lflags, HighCacheA, (win&0x80) ? 0 : edge<<1, (win&0x80) ? edge<<1 : maxcells, est);
  1439. DrawWindow( (win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 0, sh, est);
  1440. }
  1441. else
  1442. DrawLayer(lflags, HighCacheA, 0, maxcells, est);
  1443. /* - sprites low - */
  1444. if (pvid->debug_p & PVD_KILL_S_LO)
  1445. ;
  1446. else if (est->rendstatus & PDRAW_INTERLACE)
  1447. DrawAllSpritesInterlace(0, sh);
  1448. else if (sprited[1] & SPRL_HAVE_LO)
  1449. DrawAllSprites(sprited, 0, sh, est);
  1450. /* - layer B hi - */
  1451. if (!(pvid->debug_p & PVD_KILL_B) && HighCacheB[0])
  1452. DrawTilesFromCache(HighCacheB, sh, maxw, est);
  1453. /* - layer A hi - */
  1454. if (pvid->debug_p & PVD_KILL_A)
  1455. ;
  1456. else if (hvwind == 1)
  1457. DrawWindow(0, maxcells>>1, 1, sh, est);
  1458. else if (hvwind == 2) {
  1459. if (HighCacheA[0])
  1460. DrawTilesFromCache(HighCacheA, sh, (win&0x80) ? edge<<4 : maxw, est);
  1461. DrawWindow((win&0x80) ? edge : 0, (win&0x80) ? maxcells>>1 : edge, 1, sh, est);
  1462. } else
  1463. if (HighCacheA[0])
  1464. DrawTilesFromCache(HighCacheA, sh, maxw, est);
  1465. /* - sprites hi - */
  1466. if (pvid->debug_p & PVD_KILL_S_HI)
  1467. ;
  1468. else if (est->rendstatus & PDRAW_INTERLACE)
  1469. DrawAllSpritesInterlace(1, sh);
  1470. // have sprites without layer pri bit ontop of sprites with that bit
  1471. else if ((sprited[1] & SPRL_LO_ABOVE_HI) && (PicoIn.opt & POPT_ACC_SPRITES))
  1472. DrawSpritesHiAS(sprited, sh);
  1473. else if (sh && (sprited[1] & SPRL_MAY_HAVE_OP))
  1474. DrawSpritesSHi(sprited, est);
  1475. else if (sprited[1] & SPRL_HAVE_HI)
  1476. DrawAllSprites(sprited, 1, 0, est);
  1477. #ifdef FORCE
  1478. if (pvid->debug_p & PVD_FORCE_B) {
  1479. lflags = LF_PLANE_B | (sh<<1);
  1480. DrawLayerForced(lflags, 0, maxcells, est);
  1481. } else if (pvid->debug_p & PVD_FORCE_A) {
  1482. lflags = LF_PLANE_A | (sh<<1);
  1483. DrawLayerForced(lflags, 0, maxcells, est);
  1484. } else if (pvid->debug_p & PVD_FORCE_S)
  1485. DrawSpritesForced(sprited);
  1486. #endif
  1487. #if 0
  1488. {
  1489. int *c, a, b;
  1490. for (a = 0, c = HighCacheA; *c; c+=2, a++);
  1491. for (b = 0, c = HighCacheB; *c; c+=2, b++);
  1492. printf("%i:%03i: a=%i, b=%i\n", Pico.m.frame_count,
  1493. Pico.est.DrawScanline, a, b);
  1494. }
  1495. #endif
  1496. return 0;
  1497. }
  1498. // MUST be called every frame
  1499. PICO_INTERNAL void PicoFrameStart(void)
  1500. {
  1501. int offs = 8, lines = 224;
  1502. int dirty = ((Pico.est.rendstatus & PDRAW_SONIC_MODE) || Pico.m.dirtyPal);
  1503. int sprep = Pico.est.rendstatus & (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES);
  1504. int skipped = Pico.est.rendstatus & PDRAW_SKIP_FRAME;
  1505. // prepare to do this frame
  1506. Pico.est.rendstatus = 0;
  1507. if ((Pico.video.reg[12] & 6) == 6)
  1508. Pico.est.rendstatus |= PDRAW_INTERLACE; // interlace mode
  1509. if (!(Pico.video.reg[12] & 1))
  1510. Pico.est.rendstatus |= PDRAW_32_COLS;
  1511. if (Pico.video.reg[1] & 8) {
  1512. offs = 0;
  1513. lines = 240;
  1514. }
  1515. if (Pico.est.rendstatus != rendstatus_old || lines != rendlines) {
  1516. rendlines = lines;
  1517. // mode_change() might reset rendstatus_old by calling SetColorFormat
  1518. emu_video_mode_change((lines == 240) ? 0 : 8,
  1519. lines, (Pico.video.reg[12] & 1) ? 0 : 1);
  1520. rendstatus_old = Pico.est.rendstatus;
  1521. }
  1522. if (PicoIn.skipFrame) // preserve this until something is rendered at last
  1523. Pico.est.rendstatus |= PDRAW_SKIP_FRAME;
  1524. if (sprep | skipped)
  1525. Pico.est.rendstatus |= PDRAW_PARSE_SPRITES;
  1526. Pico.est.HighCol = HighColBase + offs * HighColIncrement;
  1527. Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;
  1528. Pico.est.DrawScanline = 0;
  1529. skip_next_line = 0;
  1530. if (FinalizeLine == FinalizeLine8bit) {
  1531. // make a backup of the current palette in case Sonic mode is detected later
  1532. Pico.est.SonicPalCount = 0;
  1533. Pico.m.dirtyPal = (dirty ? 2 : 0); // mark as dirty but already copied
  1534. blockcpy(Pico.est.SonicPal, PicoMem.cram, 0x40*2);
  1535. }
  1536. if (PicoIn.opt & POPT_ALT_RENDERER)
  1537. return;
  1538. }
  1539. static void DrawBlankedLine(int line, int offs, int sh, int bgc)
  1540. {
  1541. if (PicoScanBegin != NULL)
  1542. PicoScanBegin(line + offs);
  1543. BackFill(bgc, sh, &Pico.est);
  1544. if (FinalizeLine != NULL)
  1545. FinalizeLine(sh, line, &Pico.est);
  1546. if (PicoScanEnd != NULL)
  1547. PicoScanEnd(line + offs);
  1548. Pico.est.HighCol += HighColIncrement;
  1549. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  1550. }
  1551. static void PicoLine(int line, int offs, int sh, int bgc)
  1552. {
  1553. int skip = 0;
  1554. if (skip_next_line > 0) {
  1555. skip_next_line--;
  1556. return;
  1557. }
  1558. Pico.est.DrawScanline = line;
  1559. if (PicoScanBegin != NULL)
  1560. skip = PicoScanBegin(line + offs);
  1561. if (skip) {
  1562. skip_next_line = skip - 1;
  1563. return;
  1564. }
  1565. if (Pico.video.debug_p & (PVD_FORCE_A | PVD_FORCE_B | PVD_FORCE_S))
  1566. bgc = 0x3f;
  1567. // Draw screen:
  1568. BackFill(bgc, sh, &Pico.est);
  1569. if (Pico.video.reg[1]&0x40)
  1570. DrawDisplay(sh);
  1571. if (FinalizeLine != NULL)
  1572. FinalizeLine(sh, line, &Pico.est);
  1573. if (PicoScanEnd != NULL)
  1574. skip_next_line = PicoScanEnd(line + offs);
  1575. Pico.est.HighCol += HighColIncrement;
  1576. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement;
  1577. }
  1578. void PicoDrawSync(int to, int blank_last_line)
  1579. {
  1580. struct PicoEState *est = &Pico.est;
  1581. int line, offs = 0;
  1582. int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
  1583. int bgc = Pico.video.reg[7];
  1584. pprof_start(draw);
  1585. if (rendlines != 240) {
  1586. offs = 8;
  1587. if (to > 223)
  1588. to = 223;
  1589. }
  1590. if (est->DrawScanline <= to - blank_last_line && (est->rendstatus &
  1591. (PDRAW_SPRITES_MOVED|PDRAW_DIRTY_SPRITES|PDRAW_PARSE_SPRITES)))
  1592. PrepareSprites(to - blank_last_line + 1);
  1593. for (line = est->DrawScanline; line < to; line++)
  1594. PicoLine(line, offs, sh, bgc);
  1595. // last line
  1596. if (line <= to)
  1597. {
  1598. if (blank_last_line)
  1599. DrawBlankedLine(line, offs, sh, bgc);
  1600. else PicoLine(line, offs, sh, bgc);
  1601. line++;
  1602. }
  1603. est->DrawScanline = line;
  1604. pprof_end(draw);
  1605. }
  1606. // also works for fast renderer
  1607. void PicoDrawUpdateHighPal(void)
  1608. {
  1609. struct PicoEState *est = &Pico.est;
  1610. if (Pico.m.dirtyPal) {
  1611. int sh = (Pico.video.reg[0xC] & 8) >> 3; // shadow/hilight?
  1612. if ((PicoIn.opt & POPT_ALT_RENDERER) | (est->rendstatus & PDRAW_SONIC_MODE))
  1613. sh = 0; // no s/h support
  1614. if (PicoIn.AHW & PAHW_SMS)
  1615. PicoDoHighPal555M4();
  1616. else if (FinalizeLine == FinalizeLine8bit)
  1617. PicoDoHighPal555_8bit(sh, 0, est);
  1618. else
  1619. PicoDoHighPal555(sh, 0, est);
  1620. // cover for sprite priority bits if not in s/h or sonic mode
  1621. if (!sh && !(est->rendstatus & PDRAW_SONIC_MODE)) {
  1622. blockcpy(est->HighPal+0x40, est->HighPal, 0x40*2);
  1623. blockcpy(est->HighPal+0x80, est->HighPal, 0x80*2);
  1624. }
  1625. }
  1626. }
  1627. void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode)
  1628. {
  1629. PicoDrawSetInternalBuf(NULL, 0);
  1630. switch (which)
  1631. {
  1632. case PDF_8BIT:
  1633. FinalizeLine = FinalizeLine8bit;
  1634. break;
  1635. case PDF_RGB555:
  1636. if ((PicoIn.AHW & PAHW_32X) && use_32x_line_mode)
  1637. FinalizeLine = FinalizeLine32xRGB555;
  1638. else
  1639. FinalizeLine = FinalizeLine555;
  1640. break;
  1641. default:
  1642. FinalizeLine = NULL;
  1643. PicoDrawSetOutBufMD(Pico.est.Draw2FB+8, 328);
  1644. break;
  1645. }
  1646. if (PicoIn.AHW & PAHW_32X)
  1647. PicoDrawSetOutFormat32x(which, use_32x_line_mode);
  1648. PicoDrawSetOutputMode4(which);
  1649. rendstatus_old = -1;
  1650. }
  1651. void PicoDrawSetOutBufMD(void *dest, int increment)
  1652. {
  1653. if (FinalizeLine == FinalizeLine8bit && increment == 328) {
  1654. // kludge for no-copy mode
  1655. PicoDrawSetInternalBuf(dest, increment);
  1656. }
  1657. if (dest != NULL) {
  1658. DrawLineDestBase = dest;
  1659. DrawLineDestIncrement = increment;
  1660. Pico.est.DrawLineDest = (char *)DrawLineDestBase + Pico.est.DrawScanline * increment;
  1661. }
  1662. else {
  1663. DrawLineDestBase = DefOutBuff;
  1664. DrawLineDestIncrement = 0;
  1665. Pico.est.DrawLineDest = DefOutBuff;
  1666. }
  1667. }
  1668. // note: may be called on the middle of frame
  1669. void PicoDrawSetOutBuf(void *dest, int increment)
  1670. {
  1671. if (PicoIn.AHW & PAHW_32X)
  1672. PicoDrawSetOutBuf32X(dest, increment);
  1673. else
  1674. PicoDrawSetOutBufMD(dest, increment);
  1675. }
  1676. void PicoDrawSetInternalBuf(void *dest, int increment)
  1677. {
  1678. if (dest != NULL) {
  1679. HighColBase = dest;
  1680. HighColIncrement = increment;
  1681. Pico.est.HighCol = HighColBase + Pico.est.DrawScanline * increment;
  1682. }
  1683. else {
  1684. HighColBase = DefHighCol;
  1685. HighColIncrement = 0;
  1686. Pico.est.HighCol = DefHighCol;
  1687. }
  1688. }
  1689. void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num))
  1690. {
  1691. PicoScanBegin = NULL;
  1692. PicoScanEnd = NULL;
  1693. PicoScan32xBegin = NULL;
  1694. PicoScan32xEnd = NULL;
  1695. if ((PicoIn.AHW & PAHW_32X) && FinalizeLine != FinalizeLine32xRGB555) {
  1696. PicoScan32xBegin = begin;
  1697. PicoScan32xEnd = end;
  1698. }
  1699. else {
  1700. PicoScanBegin = begin;
  1701. PicoScanEnd = end;
  1702. }
  1703. }
  1704. void PicoDrawInit(void)
  1705. {
  1706. Pico.est.DrawLineDest = DefOutBuff;
  1707. Pico.est.HighCol = HighColBase;
  1708. Pico.est.HighPreSpr = HighPreSpr;
  1709. rendstatus_old = -1;
  1710. }
  1711. // vim:ts=2:sw=2:expandtab