release 1.80beta1 for pandora
[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
6ccf5504 11static void (*FinalizeLineM4)(int line);
200772b7 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{
19954be1 193 int lines = 192;
200772b7 194 skip_next_line = 0;
87b0845f 195 screen_offset = 24;
ae87bffa 196 rendstatus = PDRAW_32_COLS;
19954be1 197
87b0845f 198 if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18)) {
87b0845f 199 if (Pico.video.reg[1] & 0x08) {
19954be1 200 screen_offset = 0;
201 lines = 240;
87b0845f 202 }
19954be1 203 else {
87b0845f 204 screen_offset = 8;
19954be1 205 lines = 224;
206 }
207 }
208
ae87bffa 209 if (rendstatus != rendstatus_old || lines != rendlines) {
19954be1 210 rendstatus_old = rendstatus;
ae87bffa 211 rendlines = lines;
19954be1 212 emu_video_mode_change(screen_offset, lines, 1);
87b0845f 213 }
5a681086 214
215 DrawLineDest = (char *)DrawLineDestBase + screen_offset * DrawLineDestIncrement;
200772b7 216}
217
218void PicoLineMode4(int line)
219{
220 if (skip_next_line > 0) {
221 skip_next_line--;
222 return;
223 }
224
200772b7 225 if (PicoScanBegin != NULL)
87b0845f 226 skip_next_line = PicoScanBegin(line + screen_offset);
200772b7 227
228 // Draw screen:
87b0845f 229 BackFill(Pico.video.reg[7] & 0x0f, 0);
200772b7 230 if (Pico.video.reg[1] & 0x40)
87b0845f 231 DrawDisplayM4(line);
200772b7 232
233 if (FinalizeLineM4 != NULL)
6ccf5504 234 FinalizeLineM4(line);
200772b7 235
236 if (PicoScanEnd != NULL)
87b0845f 237 skip_next_line = PicoScanEnd(line + screen_offset);
5a681086 238
239 DrawLineDest = (char *)DrawLineDest + DrawLineDestIncrement;
200772b7 240}
241
242void PicoDoHighPal555M4(void)
243{
244 unsigned int *spal=(void *)Pico.cram;
245 unsigned int *dpal=(void *)HighPal;
246 unsigned int t;
247 int i;
248
249 Pico.m.dirtyPal = 0;
250
251 /* cram is always stored as shorts, even though real hardware probably uses bytes */
252 for (i = 0x20/2; i > 0; i--, spal++, dpal++) {
253 t = *spal;
254#ifdef USE_BGR555
255 t = ((t & 0x00030003)<< 3) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)<<10);
256#else
257 t = ((t & 0x00030003)<<14) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)>>1);
258#endif
259 t |= t >> 2;
260 t |= (t >> 4) & 0x08610861;
261 *dpal = t;
262 }
03065bb6 263 HighPal[0xe0] = 0;
200772b7 264}
265
6ccf5504 266static void FinalizeLineRGB555M4(int line)
200772b7 267{
200772b7 268 if (Pico.m.dirtyPal)
269 PicoDoHighPal555M4();
270
19954be1 271 // standard FinalizeLine can finish it for us,
272 // with features like scaling and such
5a681086 273 FinalizeLine555(0, line);
200772b7 274}
275
6ccf5504 276static void FinalizeLine8bitM4(int line)
87b0845f 277{
19954be1 278 unsigned char *pd = DrawLineDest;
279
280 if (!(PicoOpt & POPT_DIS_32C_BORDER))
281 pd += 32;
282
283 memcpy32((int *)pd, (int *)(HighCol+8), 256/4);
87b0845f 284}
285
5a681086 286void PicoDrawSetOutputMode4(pdso_t which)
200772b7 287{
288 switch (which)
289 {
5a681086 290 case PDF_8BIT: FinalizeLineM4 = FinalizeLine8bitM4; break;
291 case PDF_RGB555: FinalizeLineM4 = FinalizeLineRGB555M4; break;
292 default: FinalizeLineM4 = NULL; break;
200772b7 293 }
200772b7 294}
295