draw.c 64 KB

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