1 // license:BSD-3-Clause
2 // copyright-holders:David Haywood, Ryan Holtz
3
4 #include "emu.h"
5 #include "spg_renderer.h"
6
7 DEFINE_DEVICE_TYPE(SPG_RENDERER, spg_renderer_device, "spg_renderer", "SunPlus / GeneralPlus video rendering")
8
spg_renderer_device(const machine_config & mconfig,device_type type,const char * tag,device_t * owner,uint32_t clock)9 spg_renderer_device::spg_renderer_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) :
10 device_t(mconfig, type, tag, owner, clock),
11 m_space_read_cb(*this)
12 {
13 }
14
spg_renderer_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)15 spg_renderer_device::spg_renderer_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
16 spg_renderer_device(mconfig, SPG_RENDERER, tag, owner, clock)
17 {
18 }
19
device_start()20 void spg_renderer_device::device_start()
21 {
22 m_space_read_cb.resolve_safe(0);
23
24 for (uint8_t i = 0; i < 32; i++)
25 {
26 m_rgb5_to_rgb8[i] = (i << 3) | (i >> 2);
27 }
28 for (uint16_t i = 0; i < 0x8000; i++)
29 {
30 m_rgb555_to_rgb888[i] = (m_rgb5_to_rgb8[(i >> 10) & 0x1f] << 16) |
31 (m_rgb5_to_rgb8[(i >> 5) & 0x1f] << 8) |
32 (m_rgb5_to_rgb8[(i >> 0) & 0x1f] << 0);
33
34 m_rgb555_to_rgb888_current[i] = 0x0000;
35 }
36
37 save_item(NAME(m_video_regs_1c));
38 save_item(NAME(m_video_regs_1d));
39 save_item(NAME(m_video_regs_1e));
40
41 save_item(NAME(m_video_regs_2a));
42
43 save_item(NAME(m_video_regs_30));
44 save_item(NAME(m_video_regs_3c));
45
46 save_item(NAME(m_video_regs_42));
47
48 save_item(NAME(m_video_regs_7f));
49
50 save_item(NAME(m_ycmp_table));
51
52 save_item(NAME(m_rgb555_to_rgb888_current));
53 save_item(NAME(m_brightness_or_saturation_dirty));
54 }
55
device_reset()56 void spg_renderer_device::device_reset()
57 {
58 m_video_regs_1c = 0x0000;
59 m_video_regs_1d = 0x0000;
60 m_video_regs_1e = 0x0000;
61
62 m_video_regs_2a = 0x0000;
63
64 m_video_regs_30 = 0x0000;
65 m_video_regs_3c = 0x0020;
66
67 m_video_regs_42 = 0x0001;
68
69 m_video_regs_7f = 0x0000;
70
71 for (int i = 0; i < 480; i++)
72 {
73 m_ycmp_table[i] = 0xffffffff;
74 }
75
76 m_brightness_or_saturation_dirty = true;
77 }
78
79
80 // Perform a lerp between a and b
mix_channel(uint8_t bottom,uint8_t top,uint8_t alpha)81 inline uint8_t spg_renderer_device::mix_channel(uint8_t bottom, uint8_t top, uint8_t alpha)
82 {
83 return ((0x20 - alpha) * bottom + alpha * top) >> 5;
84 }
85
86 template<spg_renderer_device::blend_enable_t Blend, spg_renderer_device::flipx_t FlipX>
draw_tilestrip(bool read_from_csspace,uint32_t screenwidth,uint32_t drawwidthmask,const rectangle & cliprect,uint32_t tile_h,uint32_t tile_w,uint32_t tilegfxdata_addr,uint32_t tile,uint32_t tile_scanline,int drawx,bool flip_y,uint32_t palette_offset,const uint32_t nc_bpp,const uint32_t bits_per_row,const uint32_t words_per_tile,address_space & spc,uint16_t * paletteram,uint8_t blendlevel)87 void spg_renderer_device::draw_tilestrip(bool read_from_csspace, uint32_t screenwidth, uint32_t drawwidthmask, const rectangle& cliprect, uint32_t tile_h, uint32_t tile_w, uint32_t tilegfxdata_addr, uint32_t tile, uint32_t tile_scanline, int drawx, bool flip_y, uint32_t palette_offset, const uint32_t nc_bpp, const uint32_t bits_per_row, const uint32_t words_per_tile, address_space &spc, uint16_t* paletteram, uint8_t blendlevel)
88 {
89 const uint32_t yflipmask = flip_y ? tile_h - 1 : 0;
90 uint32_t m = tilegfxdata_addr + words_per_tile * tile + bits_per_row * (tile_scanline ^ yflipmask);
91 uint32_t bits = 0;
92 uint32_t nbits = 0;
93
94 for (int32_t x = FlipX ? (tile_w - 1) : 0; FlipX ? x >= 0 : x < tile_w; FlipX ? x-- : x++)
95 {
96 int realdrawpos = (drawx + x) & drawwidthmask;
97
98 bits <<= nc_bpp;
99
100 if (nbits < nc_bpp)
101 {
102 if (!read_from_csspace)
103 {
104 uint16_t b = spc.read_word(m++ & 0x3fffff);
105 b = (b << 8) | (b >> 8);
106 bits |= b << (nc_bpp - nbits);
107 nbits += 16;
108 }
109 else
110 {
111 uint16_t b;
112 const int addr = m & 0x7ffffff;
113 if (addr < m_csbase)
114 {
115 b = m_cpuspace->read_word(addr);
116 }
117 else
118 {
119 b = m_cs_space->read_word(addr-m_csbase);
120 }
121 m++;
122 b = (b << 8) | (b >> 8);
123 bits |= b << (nc_bpp - nbits);
124 nbits += 16;
125 }
126 }
127 nbits -= nc_bpp;
128
129 uint32_t pal = palette_offset + (bits >> 16);
130 bits &= 0xffff;
131
132
133 if (realdrawpos >= 0 && realdrawpos < screenwidth)
134 {
135 uint16_t rgb = paletteram[pal];
136
137 if (!(rgb & 0x8000))
138 {
139 if (Blend)
140 {
141
142 m_linebuf[realdrawpos] = (mix_channel((uint8_t)(m_linebuf[realdrawpos] >> 10) & 0x1f, (rgb >> 10) & 0x1f, blendlevel) << 10) |
143 (mix_channel((uint8_t)(m_linebuf[realdrawpos] >> 5) & 0x1f, (rgb >> 5) & 0x1f, blendlevel) << 5) |
144 (mix_channel((uint8_t)(m_linebuf[realdrawpos] >> 0) & 0x1f, (rgb >> 0) & 0x1f, blendlevel) << 0);
145 }
146 else
147 {
148 m_linebuf[realdrawpos] = rgb;
149 }
150 }
151 }
152 }
153 }
154
155
draw_linemap(bool has_extended_tilemaps,const rectangle & cliprect,uint32_t scanline,int priority,uint32_t tilegfxdata_addr,uint16_t * scrollregs,uint16_t * tilemapregs,address_space & spc,uint16_t * paletteram)156 void spg_renderer_device::draw_linemap(bool has_extended_tilemaps, const rectangle& cliprect, uint32_t scanline, int priority, uint32_t tilegfxdata_addr, uint16_t* scrollregs, uint16_t* tilemapregs, address_space &spc, uint16_t* paletteram)
157 {
158 if (has_extended_tilemaps)
159 {
160 uint32_t ctrl = tilemapregs[1];
161
162 if (0)
163 {
164 if (ctrl & 0x0010)
165 popmessage("bitmap mode %08x with rowscroll\n", tilegfxdata_addr);
166 else
167 popmessage("bitmap mode %08x\n", tilegfxdata_addr);
168 }
169
170 // note, in interlace modes it appears every other line is unused? (480 entry table, but with blank values)
171 // and furthermore the rowscroll and rowzoom tables only have 240 entries, not enough for every line
172 // the end of the rowscroll table (entries 240-255) contain something else, maybe garbage data as it's offscreen, maybe not
173 uint32_t tilemap = tilemapregs[2];
174 uint32_t palette_map = tilemapregs[3];
175
176 uint32_t linebase = spc.read_word(tilemap + scanline); // every other word is unused, but there are only enough entries for 240 lines then, sometimes to do with interlace mode?
177 uint16_t palette = spc.read_word(palette_map + (scanline / 2));
178
179 if (scanline & 1)
180 palette >>= 8;
181 else
182 palette &= 0xff;
183
184 if (!linebase)
185 return;
186
187 linebase = linebase | (palette << 16);
188
189 int upperpalselect = 0;
190 if (has_extended_tilemaps && (tilegfxdata_addr & 0x80000000))
191 upperpalselect = 1;
192
193 tilegfxdata_addr &= 0x7ffffff;
194
195 // this logic works for jak_s500 and the test modes to get the correct base, doesn't seem to work for jak_car2 ingame, maybe data is copied to wrong place?
196 int gfxbase = (tilegfxdata_addr&0x7ffffff) + (linebase&0x7ffffff);
197
198 for (int i = 0; i < 160; i++) // will have to be 320 for jak_car2 ingame, jak_s500 lines are wider than screen, and zoomed
199 {
200 uint16_t pix;
201 const int addr = gfxbase & 0x7ffffff;
202 if (addr < m_csbase)
203 {
204 pix = m_cpuspace->read_word(addr);
205 }
206 else
207 {
208 pix = m_cs_space->read_word(addr-m_csbase);
209 }
210 gfxbase++;
211
212 int xx;
213 uint16_t pal;
214
215 if ((scanline >= 0) && (scanline < 480))
216 {
217 xx = i * 2;
218
219 pal = (pix & 0xff) | 0x100;
220
221 if (upperpalselect)
222 pal |= 0x200;
223
224 if (xx >= 0 && xx <= cliprect.max_x)
225 {
226 uint16_t rgb = paletteram[pal];
227
228 if (!(rgb & 0x8000))
229 {
230 m_linebuf[xx] = rgb;
231 }
232 }
233
234 xx = (i * 2)+1;
235 pal = (pix >> 8) | 0x100;
236
237 if (upperpalselect)
238 pal |= 0x200;
239
240 if (xx >= 0 && xx <= cliprect.max_x)
241 {
242 uint16_t rgb = paletteram[pal];
243
244 if (!(rgb & 0x8000))
245 {
246 m_linebuf[xx] = rgb;
247 }
248 }
249 }
250 }
251 }
252 else // code used for spg2xx cases
253 {
254 if ((scanline < 0) || (scanline >= 240))
255 return;
256
257 uint32_t tilemap = tilemapregs[2];
258 uint32_t palette_map = tilemapregs[3];
259
260 //popmessage("draw draw_linemap bases %04x %04x\n", tilemap, palette_map);
261
262 //uint32_t xscroll = scrollregs[0];
263 uint32_t yscroll = scrollregs[1];
264
265 int realline = (scanline + yscroll) & 0xff;
266
267
268 uint32_t tile = spc.read_word(tilemap + realline);
269 uint16_t palette = 0;
270
271 //if (!tile)
272 // continue;
273
274 palette = spc.read_word(palette_map + realline / 2);
275 if (scanline & 1)
276 palette >>= 8;
277 else
278 palette &= 0x00ff;
279
280 //const int linewidth = 320 / 2;
281 int sourcebase = tile | (palette << 16);
282
283 uint32_t ctrl = tilemapregs[1];
284
285 if (ctrl & 0x80) // HiColor mode (rad_digi)
286 {
287 for (int i = 0; i < 320; i++)
288 {
289 const uint16_t data = spc.read_word(sourcebase + i);
290
291 if (!(data & 0x8000))
292 {
293 m_linebuf[i] = data & 0x7fff;
294 }
295 }
296 }
297 else
298 {
299 for (int i = 0; i < 320 / 2; i++)
300 {
301 uint8_t palette_entry;
302 uint16_t color;
303 const uint16_t data = spc.read_word(sourcebase + i);
304
305 palette_entry = (data & 0x00ff);
306 color = paletteram[palette_entry];
307
308 if (!(color & 0x8000))
309 {
310 m_linebuf[(i * 2) + 0] = color & 0x7fff;
311 }
312
313 palette_entry = (data & 0xff00) >> 8;
314 color = paletteram[palette_entry];
315
316 if (!(color & 0x8000))
317 {
318 m_linebuf[(i * 2) + 1] = color & 0x7fff;
319 }
320 }
321 }
322 }
323 }
324
325
326
327
328 // this builds up a line table for the vcmp effect, this is not correct when step is used
update_vcmp_table()329 void spg_renderer_device::update_vcmp_table()
330 {
331 int currentline = 0;
332
333 int step = m_video_regs_1e & 0xff;
334 if (step & 0x80)
335 step = step - 0x100;
336
337 int current_inc_value = (m_video_regs_1c<<4);
338
339 int counter = 0;
340
341 for (int i = 0; i < 480; i++)
342 {
343 if (i < m_video_regs_1d)
344 {
345 m_ycmp_table[i] = 0xffffffff;
346 }
347 else
348 {
349 if ((currentline >= 0) && (currentline < 256))
350 {
351 m_ycmp_table[i] = currentline;
352 }
353
354 counter += current_inc_value;
355
356 while (counter >= (0x20<<4))
357 {
358 currentline++;
359 current_inc_value += step;
360
361 counter -= (0x20<<4);
362 }
363 }
364 }
365 }
366
draw_tilestrip(bool read_from_csspace,uint32_t screenwidth,uint32_t drawwidthmask,spg_renderer_device::blend_enable_t blend,spg_renderer_device::flipx_t flip_x,const rectangle & cliprect,uint32_t tile_h,uint32_t tile_w,uint32_t tilegfxdata_addr,uint32_t tile,uint32_t tile_scanline,int drawx,bool flip_y,uint32_t palette_offset,const uint32_t nc_bpp,const uint32_t bits_per_row,const uint32_t words_per_tile,address_space & spc,uint16_t * paletteram,uint8_t blendlevel)367 void spg_renderer_device::draw_tilestrip(bool read_from_csspace, uint32_t screenwidth, uint32_t drawwidthmask, spg_renderer_device::blend_enable_t blend, spg_renderer_device::flipx_t flip_x, const rectangle& cliprect, uint32_t tile_h, uint32_t tile_w, uint32_t tilegfxdata_addr, uint32_t tile, uint32_t tile_scanline, int drawx, bool flip_y, uint32_t palette_offset, const uint32_t nc_bpp, const uint32_t bits_per_row, const uint32_t words_per_tile, address_space& spc, uint16_t* paletteram, uint8_t blendlevel)
368 {
369 if (blend)
370 {
371 if (flip_x)
372 {
373 draw_tilestrip<BlendOn, FlipXOn>(read_from_csspace, screenwidth, drawwidthmask, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, tile_scanline, drawx, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
374 }
375 else
376 {
377 draw_tilestrip<BlendOn, FlipXOff>(read_from_csspace, screenwidth, drawwidthmask, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, tile_scanline, drawx, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
378 }
379 }
380 else
381 {
382 if (flip_x)
383 {
384 draw_tilestrip<BlendOff, FlipXOn>(read_from_csspace, screenwidth, drawwidthmask, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, tile_scanline, drawx, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
385 }
386 else
387 {
388 draw_tilestrip<BlendOff, FlipXOff>(read_from_csspace, screenwidth, drawwidthmask, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, tile_scanline, drawx, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
389 }
390 }
391 }
392
draw_page(bool read_from_csspace,bool has_extended_tilemaps,bool use_alt_tile_addressing,uint32_t palbank,const rectangle & cliprect,uint32_t scanline,int priority,uint32_t tilegfxdata_addr,uint16_t * scrollregs,uint16_t * tilemapregs,address_space & spc,uint16_t * paletteram,uint16_t * scrollram,uint32_t which)393 void spg_renderer_device::draw_page(bool read_from_csspace, bool has_extended_tilemaps, bool use_alt_tile_addressing, uint32_t palbank, const rectangle& cliprect, uint32_t scanline, int priority, uint32_t tilegfxdata_addr, uint16_t* scrollregs, uint16_t* tilemapregs, address_space& spc, uint16_t* paletteram, uint16_t* scrollram, uint32_t which)
394 {
395 const uint32_t attr = tilemapregs[0];
396 const uint32_t ctrl = tilemapregs[1];
397
398 if (!(ctrl & 0x0008))
399 {
400 return;
401 }
402
403 if (((attr & 0x3000) >> 12) != priority)
404 {
405 return;
406 }
407
408 if (ctrl & 0x0001) // Bitmap / Linemap mode! (basically screen width tile mode)
409 {
410 draw_linemap(has_extended_tilemaps, cliprect, scanline, priority, tilegfxdata_addr, scrollregs, tilemapregs, spc, paletteram);
411 return;
412 }
413
414 uint32_t logical_scanline = scanline;
415
416 if (ctrl & 0x0040) // 'vertical compression feature' (later models only?)
417 {
418 // used by senspeed
419 //if (m_video_regs_1e != 0x0000)
420 // popmessage("vertical compression mode with non-0 step amount %04x offset %04x step %04x\n", m_video_regs_1c, m_video_regs_1d, m_video_regs_1e);
421
422 logical_scanline = m_ycmp_table[scanline];
423 if (logical_scanline == 0xffffffff)
424 return;
425 }
426
427 uint32_t total_width;
428 uint32_t y_mask;
429 uint32_t screenwidth;
430
431
432 if (read_from_csspace && (attr & 0x8000)) // is this only available in high res mode, or always?
433 {
434 // just a guess based on this being set on the higher resolution tilemaps we've seen, could be 100% incorrect register
435 total_width = 1024;
436 y_mask = 0x200;
437 screenwidth = 640;
438 }
439 else
440 {
441 total_width = 512;
442 y_mask = 0x100;
443 screenwidth = 320;
444 }
445
446 if (has_extended_tilemaps && (attr & 0x4000)) // is this only available in high res mode, or always?
447 {
448 y_mask <<= 1; // double height tilemap?
449 }
450
451 const uint32_t drawwidthmask = total_width - 1;
452 y_mask--; // turn into actual mask
453
454 const uint32_t xscroll = scrollregs[0];
455 const uint32_t yscroll = scrollregs[1];
456 const uint32_t tilemap_rambase = tilemapregs[2];
457 const uint32_t exattributemap_rambase = tilemapregs[3];
458 const int tile_width = (attr & 0x0030) >> 4;
459 const uint32_t tile_h = 8 << ((attr & 0x00c0) >> 6);
460 const uint32_t tile_w = 8 << (tile_width);
461 const uint32_t tile_count_x = total_width / tile_w; // tilemaps are 512 or 1024 wide depending on screen mode?
462 const uint32_t bitmap_y = (logical_scanline + yscroll) & y_mask; // tilemaps are 256 or 512 high depending on screen mode?
463 const uint32_t y0 = bitmap_y / tile_h;
464 const uint32_t tile_scanline = bitmap_y % tile_h;
465 const uint8_t bpp = attr & 0x0003;
466 const uint32_t nc_bpp = ((bpp)+1) << 1;
467 const uint32_t bits_per_row = nc_bpp * tile_w / 16;
468 //const uint32_t words_per_tile = bits_per_row * tile_h;
469 const bool row_scroll = (ctrl & 0x0010);
470 uint8_t blendlevel = (m_video_regs_2a & 3) << 3;
471
472 uint32_t words_per_tile;
473
474 // good for gormiti, smartfp, wrlshunt, paccon, jak_totm, jak_s500, jak_gtg
475 if (has_extended_tilemaps && use_alt_tile_addressing)
476 {
477 words_per_tile = 8;
478 }
479 else
480 {
481 words_per_tile = bits_per_row * tile_h;
482 }
483
484 int realxscroll = xscroll;
485 if (row_scroll)
486 {
487 if (!has_extended_tilemaps)
488 {
489 // Tennis in My Wireless Sports confirms the need to add the scroll value here rather than rowscroll being screen-aligned
490 realxscroll += (int16_t)scrollram[(logical_scanline + yscroll) & 0xff];
491 }
492 else
493 {
494 // the logic seems to be different on GPL16250, see Galaxian in paccon and Crazy Moto in myac220, is this mode be selected or did behavior just change?
495 realxscroll += (int16_t)scrollram[logical_scanline & 0xff];
496 }
497 }
498
499 const int upperscrollbits = (realxscroll >> (tile_width + 3));
500 const int endpos = (screenwidth + tile_w) / tile_w;
501
502 int upperpalselect = 0;
503 if (has_extended_tilemaps && (tilegfxdata_addr & 0x80000000))
504 upperpalselect = 1;
505
506 tilegfxdata_addr &= 0x7ffffff;
507
508 for (uint32_t x0 = 0; x0 < endpos; x0++)
509 {
510 spg_renderer_device::blend_enable_t blend;
511 spg_renderer_device::flipx_t flip_x;
512 bool flip_y;
513 uint32_t tile;
514 uint32_t palette_offset;
515
516 // get tile info
517 const int realx0 = (x0 + upperscrollbits) & (tile_count_x - 1);
518 uint32_t tile_address = realx0 + (tile_count_x * y0);
519
520 tile = (ctrl & 0x0004) ? spc.read_word(tilemap_rambase) : spc.read_word(tilemap_rambase + tile_address);
521
522 if (!tile)
523 {
524 if (!has_extended_tilemaps)
525 {
526 // always skip on older SPG types?
527 continue;
528 }
529 else if (m_video_regs_7f & 0x0002)
530 {
531 // Galaga in paccon won't render '0' characters in the scoring table if you skip empty tiles, so maybe GPL16250 doesn't skip? - extra tile bits from extended read make no difference
532
533 // probably not based on register m_video_regs_7f, but paccon galaga needs no skip, jak_gtg and jak_hmhsm needs to skip
534 //49 0100 1001 no skip (paccon galaga)
535 //4b 0100 1011 skip (paccon pacman)
536 //53 0101 0011 skip (jak_gtg, jak_hmhsm)
537 continue;
538 }
539 }
540
541
542 uint32_t tileattr = attr;
543 uint32_t tilectrl = ctrl;
544
545 if (has_extended_tilemaps && use_alt_tile_addressing)
546 {
547 // in this mode what would be the 'palette' bits get used for extra tile bits (even if the usual 'extended table' mode is disabled?)
548 // used by smartfp
549 uint16_t exattribute = (ctrl & 0x0004) ? spc.read_word(exattributemap_rambase) : spc.read_word(exattributemap_rambase + tile_address / 2);
550 if (realx0 & 1)
551 exattribute >>= 8;
552 else
553 exattribute &= 0x00ff;
554
555 tile |= (exattribute & 0x07) << 16;
556 //blendlevel = 0x1f; // hack
557 }
558 else if ((ctrl & 2) == 0)
559 { // -(1) bld(1) flip(2) pal(4)
560
561 uint16_t exattribute = (ctrl & 0x0004) ? spc.read_word(exattributemap_rambase) : spc.read_word(exattributemap_rambase + tile_address / 2);
562 if (realx0 & 1)
563 exattribute >>= 8;
564 else
565 exattribute &= 0x00ff;
566
567
568 tileattr &= ~0x000c;
569 tileattr |= (exattribute >> 2) & 0x000c; // flip
570
571 tileattr &= ~0x0f00;
572 tileattr |= (exattribute << 8) & 0x0f00; // palette
573
574 tilectrl &= ~0x0100;
575 tilectrl |= (exattribute << 2) & 0x0100; // blend
576 }
577
578 if (!has_extended_tilemaps)
579 blend = (tilectrl & 0x0100) ? BlendOn : BlendOff;
580 else
581 blend = ((/*tileattr & 0x4000 ||*/ tilectrl & 0x0100)) ? BlendOn : BlendOff; // is this logic even correct or should it just be like above? where is the extra enable needed?
582
583 flip_x = (tileattr & 0x0004) ? FlipXOn : FlipXOff;
584 flip_y = (tileattr & 0x0008);
585
586 palette_offset = (tileattr & 0x0f00) >> 4;
587 // got tile info
588
589 if (upperpalselect)
590 palette_offset |= 0x200;
591
592 palette_offset >>= nc_bpp;
593 palette_offset <<= nc_bpp;
594
595 const int drawx = (x0 * tile_w) - (realxscroll & (tile_w - 1));
596 draw_tilestrip(read_from_csspace, screenwidth, drawwidthmask, blend, flip_x, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, tile_scanline, drawx, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
597 }
598 }
599
draw_sprite(bool read_from_csspace,bool has_extended_sprites,bool alt_extrasprite_hack,uint32_t palbank,bool highres,const rectangle & cliprect,uint32_t scanline,int priority,uint32_t spritegfxdata_addr,uint32_t base_addr,address_space & spc,uint16_t * paletteram,uint16_t * spriteram)600 void spg_renderer_device::draw_sprite(bool read_from_csspace, bool has_extended_sprites, bool alt_extrasprite_hack, uint32_t palbank, bool highres, const rectangle& cliprect, uint32_t scanline, int priority, uint32_t spritegfxdata_addr, uint32_t base_addr, address_space &spc, uint16_t* paletteram, uint16_t* spriteram)
601 {
602 uint32_t tilegfxdata_addr = spritegfxdata_addr;
603 uint32_t tile = spriteram[base_addr + 0];
604 int16_t x = spriteram[base_addr + 1];
605 int16_t y = spriteram[base_addr + 2];
606 uint16_t attr = spriteram[base_addr + 3];
607
608 if (!tile)
609 {
610 return;
611 }
612
613 if (((attr & 0x3000) >> 12) != priority)
614 {
615 return;
616 }
617
618
619 uint32_t screenwidth = 320;
620 // uint32_t screenheight = 240;
621 uint32_t screenheight = 256;
622 uint32_t xmask = 0x1ff;
623 uint32_t ymask = 0x1ff;
624
625 if (highres)
626 {
627 screenwidth = 640;
628 // screenheight = 480;
629 screenheight = 512;
630 xmask = 0x3ff;
631 ymask = 0x3ff;
632 }
633
634 const uint32_t tile_h = 8 << ((attr & 0x00c0) >> 6);
635 const uint32_t tile_w = 8 << ((attr & 0x0030) >> 4);
636
637 if (!(m_video_regs_42 & 0x0002))
638 {
639 x = ((screenwidth/2) + x) - tile_w / 2;
640 // y = ((screenheight/2) - y) - (tile_h / 2) + 8;
641 y = ((screenheight/2) - y) - (tile_h / 2);
642 }
643
644 x &= xmask;
645 y &= ymask;
646
647 int firstline = y;
648 int lastline = y + (tile_h - 1);
649 lastline &= ymask;
650
651 const spg_renderer_device::blend_enable_t blend = (attr & 0x4000) ? BlendOn : BlendOff;
652 spg_renderer_device::flipx_t flip_x = (attr & 0x0004) ? FlipXOn : FlipXOff;
653 const uint8_t bpp = attr & 0x0003;
654 const uint32_t nc_bpp = ((bpp)+1) << 1;
655 const uint32_t bits_per_row = nc_bpp * tile_w / 16;
656 uint8_t blendlevel = (m_video_regs_2a & 3) << 3;
657
658 uint32_t words_per_tile;
659
660 // good for gormiti, smartfp, wrlshunt, paccon, jak_totm, jak_s500, jak_gtg
661 if (has_extended_sprites && ((m_video_regs_42 & 0x0010) == 0x10))
662 {
663 // paccon and smartfp use this mode
664 words_per_tile = 8;
665
666 if (!alt_extrasprite_hack) // 1 extra word for each sprite
667 {
668 // before or after the 0 tile check?
669 tile |= (spriteram[(base_addr / 4) + 0x400] & 0x01ff) << 16;
670 blendlevel = ((spriteram[(base_addr / 4) + 0x400] & 0x3e00) >> 9);
671 }
672 else // jak_prft - no /4 to offset in this mode - 4 extra words per sprite instead ? (or is RAM content incorrect for one of these cases?)
673 {
674 tile |= spriteram[(base_addr) + 0x400] << 16;
675 blendlevel = ((spriteram[(base_addr) + 0x400] & 0x3e00) >> 9);
676 }
677 }
678 else
679 {
680 words_per_tile = bits_per_row * tile_h;
681 }
682
683
684 bool flip_y = (attr & 0x0008);
685
686 // various games don't want the flip bits in the usual place, wrlshunt for example, there's probably a bit to control this
687 // and likewise these bits probably now have a different meaning, so this shouldn't be trusted
688 if (has_extended_sprites)
689 {
690 if (highres || alt_extrasprite_hack)
691 {
692 flip_x = FlipXOff;
693 flip_y = 0;
694 }
695 }
696
697 uint32_t palette_offset = (attr & 0x0f00) >> 4;
698
699 if (has_extended_sprites)
700 {
701 // guess, tkmag220 / myac220 don't set this bit and expect all sprite palettes to be from the same bank as background palettes
702 if (palbank & 1)
703 palette_offset |= 0x100;
704
705 // many other gpl16250 sets have this bit set when they want the upper 256 colours on a per-sprite basis, seems like an extended feature
706 if (attr & 0x8000)
707 palette_offset |= 0x200;
708 }
709
710 // the Circuit Racing game in PDC100 needs this or some graphics have bad colours at the edges when turning as it leaves stray lower bits set
711 palette_offset >>= nc_bpp;
712 palette_offset <<= nc_bpp;
713
714 if (firstline < lastline)
715 {
716 int scanx = scanline - firstline;
717
718 if ((scanx >= 0) && (scanline <= lastline))
719 {
720 draw_tilestrip(read_from_csspace, screenwidth, xmask, blend, flip_x, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, scanx, x, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
721 }
722 }
723 else
724 {
725 // clipped from top
726 int tempfirstline = firstline - (screenheight<<1);
727 int templastline = lastline;
728 int scanx = scanline - tempfirstline;
729
730 if ((scanx >= 0) && (scanline <= templastline))
731 {
732 draw_tilestrip(read_from_csspace, screenwidth, xmask, blend, flip_x, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, scanx, x, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
733 }
734 // clipped against the bottom
735 tempfirstline = firstline;
736 templastline = lastline + (screenheight<<1);
737 scanx = scanline - tempfirstline;
738
739 if ((scanx >= 0) && (scanline <= templastline))
740 {
741 draw_tilestrip(read_from_csspace, screenwidth, xmask, blend, flip_x, cliprect, tile_h, tile_w, tilegfxdata_addr, tile, scanx, x, flip_y, palette_offset, nc_bpp, bits_per_row, words_per_tile, spc, paletteram, blendlevel);
742 }
743 }
744 }
745
draw_sprites(bool read_from_csspace,bool has_extended_sprites,bool alt_extrasprite_hack,uint32_t palbank,bool highres,const rectangle & cliprect,uint32_t scanline,int priority,uint32_t spritegfxdata_addr,address_space & spc,uint16_t * paletteram,uint16_t * spriteram,int sprlimit)746 void spg_renderer_device::draw_sprites(bool read_from_csspace, bool has_extended_sprites, bool alt_extrasprite_hack, uint32_t palbank, bool highres, const rectangle &cliprect, uint32_t scanline, int priority, uint32_t spritegfxdata_addr, address_space &spc, uint16_t* paletteram, uint16_t* spriteram, int sprlimit)
747 {
748 if (!(m_video_regs_42 & 0x0001))
749 {
750 return;
751 }
752
753 // paccon suggests this, does older hardware have similar (if so, starting at what point?) or only GPL16250?
754 if (sprlimit == -1)
755 {
756 sprlimit = (m_video_regs_42 & 0xff00) >> 8;
757 if (sprlimit == 0)
758 sprlimit = 0x100;
759 }
760
761 for (uint32_t n = 0; n < sprlimit; n++)
762 {
763 draw_sprite(read_from_csspace, has_extended_sprites, alt_extrasprite_hack, palbank, highres, cliprect, scanline, priority, spritegfxdata_addr, 4 * n, spc, paletteram, spriteram);
764 }
765 }
766
new_line(const rectangle & cliprect)767 void spg_renderer_device::new_line(const rectangle& cliprect)
768 {
769 update_palette_lookup();
770
771 for (int x = cliprect.min_x; x <= cliprect.max_x; x++)
772 {
773 m_linebuf[x] = 0x0000;
774 }
775 }
776
update_palette_lookup()777 void spg_renderer_device::update_palette_lookup()
778 {
779 if (!m_brightness_or_saturation_dirty)
780 return;
781
782 static const float s_u8_to_float = 1.0f / 255.0f;
783 static const float s_gray_r = 0.299f;
784 static const float s_gray_g = 0.587f;
785 static const float s_gray_b = 0.114f;
786 const float sat_adjust = (0xff - (m_video_regs_3c & 0x00ff)) / (float)(0xff - 0x20);
787
788 const uint16_t fade_offset = m_video_regs_30;
789
790 for (uint16_t i = 0; i < 0x8000; i++)
791 {
792 uint32_t src = m_rgb555_to_rgb888[i];
793
794 if ((m_video_regs_3c & 0x00ff) != 0x0020) // apply saturation
795 {
796 const uint32_t src_rgb = src;
797 const float src_r = (uint8_t)(src_rgb >> 16) * s_u8_to_float;
798 const float src_g = (uint8_t)(src_rgb >> 8) * s_u8_to_float;
799 const float src_b = (uint8_t)(src_rgb >> 0) * s_u8_to_float;
800 const float luma = src_r * s_gray_r + src_g * s_gray_g + src_b * s_gray_b;
801 const float adjusted_r = luma + (src_r - luma) * sat_adjust;
802 const float adjusted_g = luma + (src_g - luma) * sat_adjust;
803 const float adjusted_b = luma + (src_b - luma) * sat_adjust;
804 const int integer_r = (int)floor(adjusted_r * 255.0f);
805 const int integer_g = (int)floor(adjusted_g * 255.0f);
806 const int integer_b = (int)floor(adjusted_b * 255.0f);
807 src = (integer_r > 255 ? 0xff0000 : (integer_r < 0 ? 0 : ((uint8_t)integer_r << 16))) |
808 (integer_g > 255 ? 0x00ff00 : (integer_g < 0 ? 0 : ((uint8_t)integer_g << 8))) |
809 (integer_b > 255 ? 0x0000ff : (integer_b < 0 ? 0 : (uint8_t)integer_b));
810 }
811
812 if (fade_offset != 0) // apply fade
813 {
814 const uint32_t src_rgb = src;
815 const uint8_t src_r = (src_rgb >> 16) & 0xff;
816 const uint8_t src_g = (src_rgb >> 8) & 0xff;
817 const uint8_t src_b = (src_rgb >> 0) & 0xff;
818 const uint8_t r = src_r - fade_offset;
819 const uint8_t g = src_g - fade_offset;
820 const uint8_t b = src_b - fade_offset;
821 src = (r > src_r ? 0 : (r << 16)) |
822 (g > src_g ? 0 : (g << 8)) |
823 (b > src_b ? 0 : (b << 0));
824 }
825
826 m_rgb555_to_rgb888_current[i] = src;
827 }
828
829 m_brightness_or_saturation_dirty = false;
830 }
831
apply_saturation_and_fade(bitmap_rgb32 & bitmap,const rectangle & cliprect,int scanline)832 void spg_renderer_device::apply_saturation_and_fade(bitmap_rgb32& bitmap, const rectangle& cliprect, int scanline)
833 {
834 uint32_t* src = &bitmap.pix(scanline, cliprect.min_x);
835
836 for (int x = cliprect.min_x; x <= cliprect.max_x; x++)
837 {
838 *src = m_rgb555_to_rgb888_current[m_linebuf[x]];
839 src++;
840 }
841 }
842