bugfixes
[picodrive.git] / pico / mode4.c
CommitLineData
87b0845f 1/*
2 * TODO:
3 * - TMS9918 modes?
4 * - gg mode?
5 * - column scroll (reg 0 bit7)
6 * - 224/240 line modes
7 * - doubled sprites
8 */
200772b7 9#include "pico_int.h"
10
11static void (*FinalizeLineM4)(void);
12static int skip_next_line;
87b0845f 13static int screen_offset;
200772b7 14
15#define PLANAR_PIXEL(x,p) \
16 t = pack & (0x80808080 >> p); \
17 if (t) { \
18 t = ((t >> (7-p)) | (t >> (14-p)) | (t >> (21-p)) | (t >> (28-p))) & 0x0f; \
19 pd[x] = pal|t; \
20 }
21
22static int TileNormM4(int sx, int addr, int pal)
23{
24 unsigned char *pd = HighCol + sx;
25 unsigned int pack, t;
26
27 pack = *(unsigned int *)(Pico.vram + addr); /* Get 4 bitplanes / 8 pixels */
28 if (pack)
29 {
30 PLANAR_PIXEL(0, 0)
31 PLANAR_PIXEL(1, 1)
32 PLANAR_PIXEL(2, 2)
33 PLANAR_PIXEL(3, 3)
34 PLANAR_PIXEL(4, 4)
35 PLANAR_PIXEL(5, 5)
36 PLANAR_PIXEL(6, 6)
37 PLANAR_PIXEL(7, 7)
38 return 0;
39 }
40
41 return 1; /* Tile blank */
42}
43
44static int TileFlipM4(int sx,int addr,int pal)
45{
46 unsigned char *pd = HighCol + sx;
47 unsigned int pack, t;
48
49 pack = *(unsigned int *)(Pico.vram + addr); /* Get 4 bitplanes / 8 pixels */
50 if (pack)
51 {
52 PLANAR_PIXEL(0, 7)
53 PLANAR_PIXEL(1, 6)
54 PLANAR_PIXEL(2, 5)
55 PLANAR_PIXEL(3, 4)
56 PLANAR_PIXEL(4, 3)
57 PLANAR_PIXEL(5, 2)
58 PLANAR_PIXEL(6, 1)
59 PLANAR_PIXEL(7, 0)
60 return 0;
61 }
62
63 return 1; /* Tile blank */
64}
65
87b0845f 66static void draw_sprites(int scanline)
200772b7 67{
87b0845f 68 struct PicoVideo *pv = &Pico.video;
69 unsigned int sprites_addr[8];
70 unsigned int sprites_x[8];
71 unsigned char *sat;
72 int xoff = 8; // relative to HighCol, which is (screen - 8)
73 int sprite_base, addr_mask;
74 int i, s, h;
75
76 if (pv->reg[0] & 8)
77 xoff = 0;
78
79 sat = (unsigned char *)Pico.vram + ((pv->reg[5] & 0x7e) << 7);
80 if (pv->reg[1] & 2) {
81 addr_mask = 0xfe; h = 16;
82 } else {
83 addr_mask = 0xff; h = 8;
84 }
85 sprite_base = (pv->reg[6] & 4) << (13-2-1);
86
87 for (i = s = 0; i < 64 && s < 8; i++)
88 {
89 int y;
90 y = sat[i] + 1;
91 if (y == 0xd1)
92 break;
93 if (y + h <= scanline || scanline < y)
94 continue; // not on this line
95
96 sprites_x[s] = xoff + sat[0x80 + i*2];
97 sprites_addr[s] = sprite_base + ((sat[0x80 + i*2 + 1] & addr_mask) << (5-1)) +
98 ((scanline - y) << (2-1));
99 s++;
100 }
101
102 // now draw all sprites backwards
103 for (--s; s >= 0; s--)
104 TileNormM4(sprites_x[s], sprites_addr[s], 0x10);
105}
106
107// tilex_ty_prio merged to reduce register pressure
108static void draw_strip(const unsigned short *nametab, int dx, int cells, int tilex_ty_prio)
200772b7 109{
87b0845f 110 int oldcode = -1, blank = -1; // The tile we know is blank
111 int addr = 0, pal = 0;
200772b7 112
113 // Draw tiles across screen:
87b0845f 114 for (; cells > 0; dx += 8, tilex_ty_prio++, cells--)
200772b7 115 {
87b0845f 116 int code, zero;
200772b7 117
87b0845f 118 code = nametab[tilex_ty_prio & 0x1f];
119 if (code == blank)
120 continue;
121 if ((code ^ tilex_ty_prio) & 0x1000) // priority differs?
122 continue;
200772b7 123
87b0845f 124 if (code != oldcode) {
200772b7 125 oldcode = code;
126 // Get tile address/2:
87b0845f 127 addr = (code & 0x1ff) << 4;
128 addr += tilex_ty_prio >> 16;
129 if (code & 0x0400)
130 addr ^= 0xe; // Y-flip
200772b7 131
87b0845f 132 pal = (code>>7) & 0x10;
200772b7 133 }
134
87b0845f 135 if (code&0x0200) zero = TileFlipM4(dx, addr, pal);
136 else zero = TileNormM4(dx, addr, pal);
200772b7 137
87b0845f 138 if (zero)
139 blank = code; // We know this tile is blank now
200772b7 140 }
141}
142
87b0845f 143static void DrawDisplayM4(int scanline)
200772b7 144{
87b0845f 145 struct PicoVideo *pv = &Pico.video;
146 unsigned short *nametab;
147 int line, tilex, dx, ty, cells;
148 int cellskip = 0; // XXX
149 int maxcells = 32;
200772b7 150
151 // Find the line in the name table
87b0845f 152 line = pv->reg[9] + scanline; // vscroll + scanline
153 if (line >= 224)
154 line -= 224;
200772b7 155
87b0845f 156 // Find name table:
157 nametab = Pico.vram;
158 nametab += (pv->reg[2] & 0x0e) << (10-1);
159 nametab += (line>>3) << (6-1);
160
161 dx = pv->reg[8]; // hscroll
162 if (scanline < 16 && (pv->reg[0] & 0x40))
163 dx = 0; // hscroll disabled for top 2 rows
164
165 tilex = ((-dx >> 3) + cellskip) & 0x1f;
166 ty = (line & 7) << 1; // Y-Offset into tile
167 cells = maxcells - cellskip;
168
169 dx = ((dx - 1) & 7) + 1;
170 if (dx != 8)
171 cells++; // have hscroll, need to draw 1 cell more
172 dx += cellskip << 3;
173
174 // low priority tiles
175 if (PicoDrawMask & PDRAW_LAYERB_ON)
176 draw_strip(nametab, dx, cells, tilex | 0x0000 | (ty << 16));
177
178 // sprites
179 if (PicoDrawMask & PDRAW_SPRITES_LOW_ON)
180 draw_sprites(scanline);
181
182 // high priority tiles (use virtual layer switch just for fun)
183 if (PicoDrawMask & PDRAW_LAYERA_ON)
184 draw_strip(nametab, dx, cells, tilex | 0x1000 | (ty << 16));
185
186 if (pv->reg[0] & 0x20)
187 // first column masked
188 ((int *)HighCol)[2] = ((int *)HighCol)[3] = 0xe0e0e0e0;
200772b7 189}
190
191void PicoFrameStartMode4(void)
192{
200772b7 193 skip_next_line = 0;
87b0845f 194 screen_offset = 24;
195 rendstatus = PDRAW_192LINES;
196 if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18)) {
197 rendstatus &= ~PDRAW_192LINES;
198 if (Pico.video.reg[1] & 0x08) {
199 screen_offset = 0;
200 rendstatus |= PDRAW_240LINES;
201 }
202 else // it's 224 lines
203 screen_offset = 8;
204 }
200772b7 205}
206
207void PicoLineMode4(int line)
208{
209 if (skip_next_line > 0) {
210 skip_next_line--;
211 return;
212 }
213
200772b7 214 if (PicoScanBegin != NULL)
87b0845f 215 skip_next_line = PicoScanBegin(line + screen_offset);
200772b7 216
217 // Draw screen:
87b0845f 218 BackFill(Pico.video.reg[7] & 0x0f, 0);
200772b7 219 if (Pico.video.reg[1] & 0x40)
87b0845f 220 DrawDisplayM4(line);
200772b7 221
222 if (FinalizeLineM4 != NULL)
223 FinalizeLineM4();
224
225 if (PicoScanEnd != NULL)
87b0845f 226 skip_next_line = PicoScanEnd(line + screen_offset);
200772b7 227}
228
229void PicoDoHighPal555M4(void)
230{
231 unsigned int *spal=(void *)Pico.cram;
232 unsigned int *dpal=(void *)HighPal;
233 unsigned int t;
234 int i;
235
236 Pico.m.dirtyPal = 0;
237
238 /* cram is always stored as shorts, even though real hardware probably uses bytes */
239 for (i = 0x20/2; i > 0; i--, spal++, dpal++) {
240 t = *spal;
241#ifdef USE_BGR555
242 t = ((t & 0x00030003)<< 3) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)<<10);
243#else
244 t = ((t & 0x00030003)<<14) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)>>1);
245#endif
246 t |= t >> 2;
247 t |= (t >> 4) & 0x08610861;
248 *dpal = t;
249 }
250}
251
252static void FinalizeLineRGB555M4(void)
253{
254 unsigned short *pd=DrawLineDest;
255 unsigned char *ps=HighCol+8;
256 unsigned short *pal=HighPal;
257 int i;
258
259 if (Pico.m.dirtyPal)
260 PicoDoHighPal555M4();
261
262 if (!(PicoOpt & POPT_DIS_32C_BORDER))
263 pd += 32;
264
265 for (i = 256/4; i > 0; i--) {
266 *pd++ = pal[*ps++];
267 *pd++ = pal[*ps++];
268 *pd++ = pal[*ps++];
269 *pd++ = pal[*ps++];
270 }
271}
272
87b0845f 273static void FinalizeLine8bitM4(void)
274{
275 memcpy32(DrawLineDest, (int *)(HighCol+8), 256/4);
276}
277
200772b7 278void PicoDrawSetColorFormatMode4(int which)
279{
280 switch (which)
281 {
87b0845f 282 case 2: FinalizeLineM4 = FinalizeLine8bitM4; break;
200772b7 283 case 1: FinalizeLineM4 = FinalizeLineRGB555M4; break;
284 default:FinalizeLineM4 = NULL; break;
285 }
200772b7 286}
287