draw.c 64 KB

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