draw.c 62 KB

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