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