draw.c 57 KB

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