Previous 15994 Revisions Next

r11292 Sunday 24th April, 2011 at 22:42:09 UTC by Angelo Salese
Other two as well
[src/mess/includes]cpschngr.h
[src/mess/video]cpschngr.c

src/mess/video/cpschngr.c
r11291r11292
1/***************************************************************************
2
3    CPS Changer
4
5***************************************************************************/
6
7#include "emu.h"
8#include "includes/cpschngr.h"
9
10/********************************************************************
11
12            Configuration table:
13
14********************************************************************/
15
16/* Game specific data */
17
18#define GFXTYPE_SPRITES   (1<<0)
19#define GFXTYPE_SCROLL1   (1<<1)
20#define GFXTYPE_SCROLL2   (1<<2)
21#define GFXTYPE_SCROLL3   (1<<3)
22#define GFXTYPE_STARS     (1<<4)
23
24
25struct gfx_range
26{
27   // start and end are as passed by the game (shift adjusted to be all
28   // in the same scale a 8x8 tiles): they don't necessarily match the
29   // position in ROM.
30   int type;
31   int start;
32   int end;
33   int bank;
34};
35
36struct CPS1config
37{
38   const char *name;             /* game driver name */
39
40   /* Some games interrogate a couple of registers on bootup. */
41   /* These are CPS1 board B self test checks. They wander from game to */
42   /* game. */
43   int cpsb_addr;        /* CPS board B test register address */
44   int cpsb_value;       /* CPS board B test register expected value */
45
46   /* some games use as a protection check the ability to do 16-bit multiplies */
47   /* with a 32-bit result, by writing the factors to two ports and reading the */
48   /* result from two other ports. */
49   /* It looks like this feature was introduced with 3wonders (CPSB ID = 08xx) */
50   int mult_factor1;
51   int mult_factor2;
52   int mult_result_lo;
53   int mult_result_hi;
54
55   /* unknown registers which might be related to the multiply protection */
56   int unknown1;
57   int unknown2;
58   int unknown3;
59
60   int layer_control;
61   int priority[4];
62   int palette_control;
63
64   /* ideally, the layer enable masks should consist of only one bit, */
65   /* but in many cases it is unknown which bit is which. */
66   int layer_enable_mask[5];
67
68   /* these depend on the B-board model and PAL */
69   int bank_sizes[4];
70   const struct gfx_range *bank_mapper;
71
72   /* some C-boards have additional I/O for extra buttons/extra players */
73   int in2_addr;
74   int in3_addr;
75   int out2_addr;
76
77   int bootleg_kludge;
78};
79
80#define __not_applicable__   -1,-1,-1,-1,-1,-1,-1
81
82/*                     CPSB ID    multiply protection      unknown      ctrl     priority masks   palctrl    layer enable masks  */
83#define CPS_B_21_DEF 0x32,  -1,   0x00,0x02,0x04,0x06, 0x08, -1,  -1,   0x26,{0x28,0x2a,0x2c,0x2e},0x30, {0x02,0x04,0x08,0x30,0x30}   // pang3 sets layer enable to 0x26 on startup
84
85#define mapper_sfzch   { 0x20000, 0, 0, 0 }, mapper_sfzch_table
86static const struct gfx_range mapper_sfzch_table[] =
87{
88   /* type                                                                  start    end      bank */
89   { GFXTYPE_SPRITES | GFXTYPE_SCROLL1 | GFXTYPE_SCROLL2 | GFXTYPE_SCROLL3, 0x00000, 0x1ffff, 0 },
90   { 0 }
91};
92
93static const struct CPS1config cps1_config_table[]=
94{
95   /* name       CPSB         gfx mapper   in2  in3  out2   kludge */
96   {"sfach",    CPS_B_21_DEF, mapper_sfzch },
97   {"sfzbch",   CPS_B_21_DEF, mapper_sfzch },
98   {"sfzch",    CPS_B_21_DEF, mapper_sfzch },
99   {"wofch",    CPS_B_21_DEF, mapper_sfzch },
100   {0}      /* End of table */
101};
102
103
104/* Offset of each palette entry */
105#define cps1_palette_entries (32*6)  /* Number colour schemes in palette */
106
107enum
108{
109   cps1_scroll_size =0x4000,   /* scroll1, scroll2, scroll3 */
110   cps1_obj_size    =0x0800,
111   cps1_other_size  =0x0800,
112   cps1_palette_align=0x0400,   /* minimum alignment is a single palette page (512 colors). Verified on pcb. */
113   cps1_palette_size=cps1_palette_entries*32, /* Size of palette RAM */
114   /* first 0x4000 of gfx ROM are used, but 0x0000-0x1fff is == 0x2000-0x3fff */
115   cps1_stars_rom_size = 0x2000,
116
117};
118
119
120/* CPS-A registers */
121#define CPS1_OBJ_BASE           (0x00/2)    /* Base address of objects */
122#define CPS1_SCROLL1_BASE       (0x02/2)    /* Base address of scroll 1 */
123#define CPS1_SCROLL2_BASE       (0x04/2)    /* Base address of scroll 2 */
124#define CPS1_SCROLL3_BASE       (0x06/2)    /* Base address of scroll 3 */
125#define CPS1_OTHER_BASE         (0x08/2)    /* Base address of other video */
126#define CPS1_PALETTE_BASE       (0x0a/2)    /* Base address of palette */
127#define CPS1_SCROLL1_SCROLLX    (0x0c/2)    /* Scroll 1 X */
128#define CPS1_SCROLL1_SCROLLY    (0x0e/2)    /* Scroll 1 Y */
129#define CPS1_SCROLL2_SCROLLX    (0x10/2)    /* Scroll 2 X */
130#define CPS1_SCROLL2_SCROLLY    (0x12/2)    /* Scroll 2 Y */
131#define CPS1_SCROLL3_SCROLLX    (0x14/2)    /* Scroll 3 X */
132#define CPS1_SCROLL3_SCROLLY    (0x16/2)    /* Scroll 3 Y */
133#define CPS1_STARS1_SCROLLX     (0x18/2)    /* Stars 1 X */
134#define CPS1_STARS1_SCROLLY     (0x1a/2)    /* Stars 1 Y */
135#define CPS1_STARS2_SCROLLX     (0x1c/2)    /* Stars 2 X */
136#define CPS1_STARS2_SCROLLY     (0x1e/2)    /* Stars 2 Y */
137#define CPS1_ROWSCROLL_OFFS     (0x20/2)    /* base of row scroll offsets in other RAM */
138#define CPS1_VIDEOCONTROL       (0x22/2)    /* flip screen, rowscroll enable */
139
140
141static void cps1_build_palette(running_machine &machine, const UINT16* const palette_base);
142
143
144static MACHINE_RESET( cps )
145{
146   cpschngr_state *state = machine.driver_data<cpschngr_state>();
147   const char *gamename = machine.system().name;
148   const struct CPS1config *pCFG=&cps1_config_table[0];
149
150   while(pCFG->name)
151   {
152      if (strcmp(pCFG->name, gamename) == 0)
153      {
154         break;
155      }
156      pCFG++;
157   }
158   state->m_cps1_game_config=pCFG;
159}
160
161
162INLINE UINT16 *cps1_base(cpschngr_state *state, int offset, int boundary)
163{
164   int base = state->m_cps1_cps_a_regs[offset]*256;
165   base &= ~(boundary-1);
166   return &state->m_cps1_gfxram[(base&0x3ffff)/2];
167}
168
169
170
171WRITE16_HANDLER( cps1_cps_a_w )
172{
173   cpschngr_state *state = space->machine().driver_data<cpschngr_state>();
174   data = COMBINE_DATA(&state->m_cps1_cps_a_regs[offset]);
175
176   if (offset == CPS1_PALETTE_BASE)
177      cps1_build_palette(space->machine(), cps1_base(state, CPS1_PALETTE_BASE, cps1_palette_align));
178}
179
180
181READ16_HANDLER( cps1_cps_b_r )
182{
183   cpschngr_state *state = space->machine().driver_data<cpschngr_state>();
184   /* Some games interrogate a couple of registers on bootup. */
185   /* These are CPS1 board B self test checks. They wander from game to */
186   /* game. */
187   if (offset == state->m_cps1_game_config->cpsb_addr/2)
188      return state->m_cps1_game_config->cpsb_value;
189
190   /* some games use as a protection check the ability to do 16-bit multiplications */
191   /* with a 32-bit result, by writing the factors to two ports and reading the */
192   /* result from two other ports. */
193   if (offset == state->m_cps1_game_config->mult_result_lo/2)
194      return (state->m_cps1_cps_b_regs[state->m_cps1_game_config->mult_factor1/2] *
195            state->m_cps1_cps_b_regs[state->m_cps1_game_config->mult_factor2/2]) & 0xffff;
196   if (offset == state->m_cps1_game_config->mult_result_hi/2)
197      return (state->m_cps1_cps_b_regs[state->m_cps1_game_config->mult_factor1/2] *
198            state->m_cps1_cps_b_regs[state->m_cps1_game_config->mult_factor2/2]) >> 16;
199
200   if (offset == state->m_cps1_game_config->in2_addr/2)   /* Extra input ports (on C-board) */
201      return input_port_read(space->machine(), "IN2");
202
203   if (offset == state->m_cps1_game_config->in3_addr/2)   /* Player 4 controls (on C-board) ("Captain Commando") */
204      return input_port_read(space->machine(), "IN3");
205
206   return 0xffff;
207}
208
209
210WRITE16_HANDLER( cps1_cps_b_w )
211{
212   cpschngr_state *state = space->machine().driver_data<cpschngr_state>();
213   data = COMBINE_DATA(&state->m_cps1_cps_b_regs[offset]);
214
215   // additional outputs on C-board
216   if (offset == state->m_cps1_game_config->out2_addr/2)
217   {
218      if (ACCESSING_BITS_0_7)
219      {
220         if (state->m_cps1_game_config->cpsb_value == 0x0402)   // Mercs (CN2 connector)
221         {
222            coin_lockout_w(space->machine(),2,~data & 0x01);
223            set_led_status(space->machine(),0,data & 0x02);
224            set_led_status(space->machine(),1,data & 0x04);
225            set_led_status(space->machine(),2,data & 0x08);
226         }
227         else   // kod, captcomm, knights
228         {
229            coin_lockout_w(space->machine(),2,~data & 0x02);
230            coin_lockout_w(space->machine(),3,~data & 0x08);
231         }
232      }
233   }
234}
235
236
237
238static void cps1_gfx_decode(running_machine &machine)
239{
240   int size=machine.region("gfx")->bytes();
241   int i,j,gfxsize;
242   UINT8 *cps1_gfx = machine.region("gfx")->base();
243
244
245   gfxsize=size/4;
246
247   for (i = 0;i < gfxsize;i++)
248   {
249      UINT32 src = cps1_gfx[4*i] + (cps1_gfx[4*i+1]<<8) + (cps1_gfx[4*i+2]<<16) + (cps1_gfx[4*i+3]<<24);
250      UINT32 dwval = 0;
251
252      for (j = 0;j < 8;j++)
253      {
254         int n = 0;
255         UINT32 mask = (0x80808080 >> j) & src;
256
257         if (mask & 0x000000ff) n |= 1;
258         if (mask & 0x0000ff00) n |= 2;
259         if (mask & 0x00ff0000) n |= 4;
260         if (mask & 0xff000000) n |= 8;
261
262         dwval |= n << (j * 4);
263      }
264      cps1_gfx[4*i  ] = dwval>>0;
265      cps1_gfx[4*i+1] = dwval>>8;
266      cps1_gfx[4*i+2] = dwval>>16;
267      cps1_gfx[4*i+3] = dwval>>24;
268   }
269}
270
271#ifdef UNUSED_FUNCTION
272static void unshuffle(UINT64 *buf,int len)
273{
274   int i;
275   UINT64 t;
276
277   if (len == 2) return;
278
279   assert(len % 4 == 0);   /* must not happen */
280
281   len /= 2;
282
283   unshuffle(buf,len);
284   unshuffle(buf + len,len);
285
286   for (i = 0;i < len/2;i++)
287   {
288      t = buf[len/2 + i];
289      buf[len/2 + i] = buf[len + i];
290      buf[len + i] = t;
291   }
292}
293#endif
294
295
296DRIVER_INIT( cps1 )
297{
298   cps1_gfx_decode(machine);
299}
300
301
302static void cps1_get_video_base(cpschngr_state *state)
303{
304   int layercontrol, videocontrol, scroll1xoff, scroll2xoff, scroll3xoff;
305
306   /* Re-calculate the VIDEO RAM base */
307   if (state->m_cps1_scroll1 != cps1_base(state, CPS1_SCROLL1_BASE, cps1_scroll_size))
308   {
309      state->m_cps1_scroll1 = cps1_base(state, CPS1_SCROLL1_BASE, cps1_scroll_size);
310      tilemap_mark_all_tiles_dirty(state->m_cps1_bg_tilemap[0]);
311   }
312   if (state->m_cps1_scroll2 != cps1_base(state, CPS1_SCROLL2_BASE, cps1_scroll_size))
313   {
314      state->m_cps1_scroll2 = cps1_base(state, CPS1_SCROLL2_BASE, cps1_scroll_size);
315      tilemap_mark_all_tiles_dirty(state->m_cps1_bg_tilemap[1]);
316   }
317   if (state->m_cps1_scroll3 != cps1_base(state, CPS1_SCROLL3_BASE, cps1_scroll_size))
318   {
319      state->m_cps1_scroll3 = cps1_base(state, CPS1_SCROLL3_BASE, cps1_scroll_size);
320      tilemap_mark_all_tiles_dirty(state->m_cps1_bg_tilemap[2]);
321   }
322
323   /* Some of the sf2 hacks use only sprite port 0x9100 and the scroll layers are offset */
324   if (state->m_cps1_game_config->bootleg_kludge == 1)
325   {
326      state->m_cps1_cps_a_regs[CPS1_OBJ_BASE] = 0x9100;
327      state->m_cps1_obj = cps1_base(state, CPS1_OBJ_BASE, cps1_obj_size);
328      scroll1xoff = -0x0c;
329      scroll2xoff = -0x0e;
330      scroll3xoff = -0x10;
331   }
332   else
333   {
334      state->m_cps1_obj = cps1_base(state, CPS1_OBJ_BASE, cps1_obj_size);
335      scroll1xoff = 0;
336      scroll2xoff = 0;
337      scroll3xoff = 0;
338   }
339   state->m_cps1_other = cps1_base(state, CPS1_OTHER_BASE, cps1_other_size);
340
341   /* Get scroll values */
342   state->m_cps1_scroll1x = state->m_cps1_cps_a_regs[CPS1_SCROLL1_SCROLLX] + scroll1xoff;
343   state->m_cps1_scroll1y = state->m_cps1_cps_a_regs[CPS1_SCROLL1_SCROLLY];
344   state->m_cps1_scroll2x = state->m_cps1_cps_a_regs[CPS1_SCROLL2_SCROLLX] + scroll2xoff;
345   state->m_cps1_scroll2y = state->m_cps1_cps_a_regs[CPS1_SCROLL2_SCROLLY];
346   state->m_cps1_scroll3x = state->m_cps1_cps_a_regs[CPS1_SCROLL3_SCROLLX] + scroll3xoff;
347   state->m_cps1_scroll3y = state->m_cps1_cps_a_regs[CPS1_SCROLL3_SCROLLY];
348   state->m_stars1x = state->m_cps1_cps_a_regs[CPS1_STARS1_SCROLLX];
349   state->m_stars1y = state->m_cps1_cps_a_regs[CPS1_STARS1_SCROLLY];
350   state->m_stars2x = state->m_cps1_cps_a_regs[CPS1_STARS2_SCROLLX];
351   state->m_stars2y = state->m_cps1_cps_a_regs[CPS1_STARS2_SCROLLY];
352
353   /* Get layer enable bits */
354   layercontrol = state->m_cps1_cps_b_regs[state->m_cps1_game_config->layer_control/2];
355   videocontrol = state->m_cps1_cps_a_regs[CPS1_VIDEOCONTROL];
356   tilemap_set_enable(state->m_cps1_bg_tilemap[0],layercontrol & state->m_cps1_game_config->layer_enable_mask[0]);
357   tilemap_set_enable(state->m_cps1_bg_tilemap[1],(layercontrol & state->m_cps1_game_config->layer_enable_mask[1]) && (videocontrol & 4));
358   tilemap_set_enable(state->m_cps1_bg_tilemap[2],(layercontrol & state->m_cps1_game_config->layer_enable_mask[2]) && (videocontrol & 8));
359   state->m_cps1_stars_enabled[0] = layercontrol & state->m_cps1_game_config->layer_enable_mask[3];
360   state->m_cps1_stars_enabled[1] = layercontrol & state->m_cps1_game_config->layer_enable_mask[4];
361}
362
363
364WRITE16_HANDLER( cps1_gfxram_w )
365{
366   cpschngr_state *state = space->machine().driver_data<cpschngr_state>();
367   int page = (offset >> 7) & 0x3c0;
368   COMBINE_DATA(&state->m_cps1_gfxram[offset]);
369
370   if (page == (state->m_cps1_cps_a_regs[CPS1_SCROLL1_BASE] & 0x3c0))
371      tilemap_mark_tile_dirty(state->m_cps1_bg_tilemap[0],offset/2 & 0x0fff);
372   if (page == (state->m_cps1_cps_a_regs[CPS1_SCROLL2_BASE] & 0x3c0))
373      tilemap_mark_tile_dirty(state->m_cps1_bg_tilemap[1],offset/2 & 0x0fff);
374   if (page == (state->m_cps1_cps_a_regs[CPS1_SCROLL3_BASE] & 0x3c0))
375      tilemap_mark_tile_dirty(state->m_cps1_bg_tilemap[2],offset/2 & 0x0fff);
376}
377
378
379
380static int gfxrom_bank_mapper(running_machine &machine, int type, int code)
381{
382   cpschngr_state *state = machine.driver_data<cpschngr_state>();
383   const struct gfx_range *range = state->m_cps1_game_config->bank_mapper;
384   int shift = 0;
385
386   assert(range);
387
388   switch (type)
389   {
390      case GFXTYPE_SPRITES: shift = 1; break;
391      case GFXTYPE_SCROLL1: shift = 0; break;
392      case GFXTYPE_SCROLL2: shift = 1; break;
393      case GFXTYPE_SCROLL3: shift = 3; break;
394   }
395
396   code <<= shift;
397
398   while (range->type)
399   {
400      if (code >= range->start && code <= range->end)
401      {
402         if (range->type & type)
403         {
404            int base = 0;
405            int i;
406
407            for (i = 0; i < range->bank; ++i)
408               base += state->m_cps1_game_config->bank_sizes[i];
409
410            return (base + (code & (state->m_cps1_game_config->bank_sizes[range->bank] - 1))) >> shift;
411         }
412      }
413
414      ++range;
415   }
416
417   return -1;
418}
419
420
421/***************************************************************************
422
423  Start the video hardware emulation.
424
425***************************************************************************/
426
427static TILEMAP_MAPPER( tilemap0_scan )
428{
429   /* logical (col,row) -> memory offset */
430   return (row & 0x1f) + ((col & 0x3f) << 5) + ((row & 0x20) << 6);
431}
432
433static TILEMAP_MAPPER( tilemap1_scan )
434{
435   /* logical (col,row) -> memory offset */
436   return (row & 0x0f) + ((col & 0x3f) << 4) + ((row & 0x30) << 6);
437}
438
439static TILEMAP_MAPPER( tilemap2_scan )
440{
441   /* logical (col,row) -> memory offset */
442   return (row & 0x07) + ((col & 0x3f) << 3) + ((row & 0x38) << 6);
443}
444
445
446static TILE_GET_INFO( get_tile0_info )
447{
448   cpschngr_state *state = machine.driver_data<cpschngr_state>();
449   int code = state->m_cps1_scroll1[2*tile_index];
450   int attr = state->m_cps1_scroll1[2*tile_index+1];
451   int gfxset;
452
453   code = gfxrom_bank_mapper(machine, GFXTYPE_SCROLL1, code);
454
455   /* allows us to reproduce a problem seen with a ffight board where USA and Japanese
456         roms have been mixed to be reproduced (ffightua) -- it looks like each column
457         should alternate between the left and right side of the 16x16 tiles */
458   gfxset = (tile_index & 0x20) >> 5;
459
460   SET_TILE_INFO(
461         gfxset,
462         code,
463         (attr & 0x1f) + 0x20,
464         TILE_FLIPYX((attr & 0x60) >> 5));
465   tileinfo->group = (attr & 0x0180) >> 7;
466
467   // for out of range tiles, switch to fully transparent data
468   // (but still call SET_TILE_INFO, otherwise problems might occur on boot e.g. unsquad)
469   if (code == -1)
470      tileinfo->pen_data = state->m_empty_tile8x8;
471}
472
473static TILE_GET_INFO( get_tile1_info )
474{
475   cpschngr_state *state = machine.driver_data<cpschngr_state>();
476   int code = state->m_cps1_scroll2[2*tile_index];
477   int attr = state->m_cps1_scroll2[2*tile_index+1];
478
479   code = gfxrom_bank_mapper(machine, GFXTYPE_SCROLL2, code);
480
481   SET_TILE_INFO(
482         2,
483         code,
484         (attr & 0x1f) + 0x40,
485         TILE_FLIPYX((attr & 0x60) >> 5));
486   tileinfo->group = (attr & 0x0180) >> 7;
487
488   // for out of range tiles, switch to fully transparent data
489   if (code == -1)
490      tileinfo->pen_data = state->m_empty_tile;
491}
492
493static TILE_GET_INFO( get_tile2_info )
494{
495   cpschngr_state *state = machine.driver_data<cpschngr_state>();
496   int code = state->m_cps1_scroll3[2*tile_index] & 0x3fff;
497   int attr = state->m_cps1_scroll3[2*tile_index+1];
498
499   code = gfxrom_bank_mapper(machine, GFXTYPE_SCROLL3, code);
500
501   SET_TILE_INFO(
502         3,
503         code,
504         (attr & 0x1f) + 0x60,
505         TILE_FLIPYX((attr & 0x60) >> 5));
506   tileinfo->group = (attr & 0x0180) >> 7;
507
508   // for out of range tiles, switch to fully transparent data
509   // (but still call SET_TILE_INFO, otherwise problems might occur on boot e.g. unsquad)
510   if (code == -1)
511      tileinfo->pen_data = state->m_empty_tile;
512}
513
514
515
516static void cps1_update_transmasks(cpschngr_state *state)
517{
518   int i;
519
520   for (i = 0;i < 4;i++)
521   {
522      int mask;
523
524      /* Get transparency registers */
525      if (state->m_cps1_game_config->priority[i] != -1)
526         mask = state->m_cps1_cps_b_regs[state->m_cps1_game_config->priority[i]/2] ^ 0xffff;
527      else mask = 0xffff;   /* completely transparent if priority masks not defined (qad) */
528
529      tilemap_set_transmask(state->m_cps1_bg_tilemap[0],i,mask,0x8000);
530      tilemap_set_transmask(state->m_cps1_bg_tilemap[1],i,mask,0x8000);
531      tilemap_set_transmask(state->m_cps1_bg_tilemap[2],i,mask,0x8000);
532   }
533}
534
535VIDEO_START( cps1 )
536{
537   cpschngr_state *state = machine.driver_data<cpschngr_state>();
538   int i;
539
540   MACHINE_RESET_CALL(cps);
541
542   state->m_cps1_bg_tilemap[0] = tilemap_create(machine, get_tile0_info,tilemap0_scan, 8, 8,64,64);
543   state->m_cps1_bg_tilemap[1] = tilemap_create(machine, get_tile1_info,tilemap1_scan,16,16,64,64);
544   state->m_cps1_bg_tilemap[2] = tilemap_create(machine, get_tile2_info,tilemap2_scan,32,32,64,64);
545
546   /* front masks will change at runtime to handle sprite occluding */
547   cps1_update_transmasks(state);
548
549   memset(state->m_empty_tile8x8,0x0f,sizeof(state->m_empty_tile8x8));
550   memset(state->m_empty_tile,0xff,sizeof(state->m_empty_tile));   // 16x16 and 32x32 use packed graphics, 8x8 does not
551
552   for (i = 0;i < cps1_palette_entries*16;i++)
553   {
554      palette_set_color(machine,i,MAKE_RGB(0,0,0));
555   }
556
557   state->m_cps1_buffered_obj = auto_alloc_array_clear(machine, UINT16, cps1_obj_size / sizeof(UINT16));
558
559   memset(state->m_cps1_gfxram, 0, state->m_cps1_gfxram_size);   /* Clear GFX RAM */
560   memset(state->m_cps1_cps_a_regs, 0, 0x40);   /* Clear CPS-A registers */
561   memset(state->m_cps1_cps_b_regs, 0, 0x40);   /* Clear CPS-B registers */
562
563   /* Put in some defaults */
564   state->m_cps1_cps_a_regs[CPS1_OBJ_BASE]     = 0x9200;
565   state->m_cps1_cps_a_regs[CPS1_SCROLL1_BASE] = 0x9000;
566   state->m_cps1_cps_a_regs[CPS1_SCROLL2_BASE] = 0x9040;
567   state->m_cps1_cps_a_regs[CPS1_SCROLL3_BASE] = 0x9080;
568   state->m_cps1_cps_a_regs[CPS1_OTHER_BASE]   = 0x9100;
569
570   assert_always(state->m_cps1_game_config, "state->m_cps1_game_config hasn't been set up yet");
571
572   /* Set up old base */
573   cps1_get_video_base(state);   /* Calculate base pointers */
574   cps1_get_video_base(state);   /* Calculate old base pointers */
575}
576
577
578
579/***************************************************************************
580
581  Build palette from palette RAM
582
583  12 bit RGB with a 4 bit brightness value.
584
585***************************************************************************/
586
587static void cps1_build_palette(running_machine &machine, const UINT16* const palette_base)
588{
589   cpschngr_state *state = machine.driver_data<cpschngr_state>();
590   int offset, page;
591   const UINT16 *palette_ram = palette_base;
592
593   int ctrl = state->m_cps1_cps_b_regs[state->m_cps1_game_config->palette_control/2];
594
595   for (page = 0; page < 6; ++page)
596   {
597      if (BIT(ctrl,page))
598      {
599         for (offset = 0; offset < 0x200; ++offset)
600         {
601            int palette = *(palette_ram++);
602            int r, g, b, bright;
603
604            // from my understanding of the schematics, when the 'brightness'
605            // component is set to 0 it should reduce brightness to 1/3
606
607            bright = 0x0f + ((palette>>12)<<1);
608
609            r = ((palette>>8)&0x0f) * 0x11 * bright / 0x2d;
610            g = ((palette>>4)&0x0f) * 0x11 * bright / 0x2d;
611            b = ((palette>>0)&0x0f) * 0x11 * bright / 0x2d;
612
613            palette_set_color (machine, 0x200*page + offset, MAKE_RGB(r, g, b));
614         }
615      }
616      else
617      {
618         // skip page in gfxram, but only if we have already copied at least one page
619         if (palette_ram != palette_base)
620            palette_ram += 0x200;
621      }
622   }
623}
624
625static void cps1_find_last_sprite(cpschngr_state *state)    /* Find the offset of last sprite */
626{
627   int offset=0;
628   /* Locate the end of table marker */
629   while (offset < cps1_obj_size/2)
630   {
631        int colour=state->m_cps1_buffered_obj[offset+3];
632      if ((colour & 0xff00) == 0xff00)
633      {
634         /* Marker found. This is the last sprite. */
635         state->m_cps1_last_sprite_offset=offset-4;
636         return;
637      }
638        offset+=4;
639   }
640   /* Sprites must use full sprite RAM */
641   state->m_cps1_last_sprite_offset=cps1_obj_size/2-4;
642}
643
644
645static void cps1_render_sprites(running_machine &machine, bitmap_t *bitmap, const rectangle *cliprect)
646{
647   cpschngr_state *state = machine.driver_data<cpschngr_state>();
648#define DRAWSPRITE(CODE,COLOR,FLIPX,FLIPY,SX,SY)               \
649{                                                   \
650   if (flip_screen_get(machine))                                 \
651      pdrawgfx_transpen(bitmap,\
652            cliprect,machine.gfx[2],                     \
653            CODE,                                    \
654            COLOR,                                    \
655            !(FLIPX),!(FLIPY),                           \
656            511-16-(SX),255-16-(SY),                     machine.priority_bitmap,0x02,15);               \
657   else                                             \
658      pdrawgfx_transpen(bitmap,\
659            cliprect,machine.gfx[2],                     \
660            CODE,                                    \
661            COLOR,                                    \
662            FLIPX,FLIPY,                              \
663            SX,SY,                                    machine.priority_bitmap,0x02,15);               \
664}
665
666
667   int i, baseadd;
668   UINT16 *base=state->m_cps1_buffered_obj;
669
670   /* some sf2 hacks draw the sprites in reverse order */
671   if (state->m_cps1_game_config->bootleg_kludge == 1)
672   {
673      base += state->m_cps1_last_sprite_offset;
674      baseadd = -4;
675   }
676   else
677   {
678      baseadd = 4;
679   }
680
681   for (i=state->m_cps1_last_sprite_offset; i>=0; i-=4)
682   {
683      int x=*(base+0);
684      int y=*(base+1);
685      int code  =*(base+2);
686      int colour=*(base+3);
687      int col=colour&0x1f;
688
689      code = gfxrom_bank_mapper(machine, GFXTYPE_SPRITES, code);
690
691      if (code != -1)
692      {
693         if (colour & 0xff00 )
694         {
695            /* handle blocked sprites */
696            int nx=(colour & 0x0f00) >> 8;
697            int ny=(colour & 0xf000) >> 12;
698            int nxs,nys,sx,sy;
699            nx++;
700            ny++;
701
702            if (colour & 0x40)
703            {
704               /* Y flip */
705               if (colour &0x20)
706               {
707                  for (nys=0; nys<ny; nys++)
708                  {
709                     for (nxs=0; nxs<nx; nxs++)
710                     {
711                        sx = (x+nxs*16) & 0x1ff;
712                        sy = (y+nys*16) & 0x1ff;
713
714                        DRAWSPRITE(
715                              (code & ~0xf) + ((code + (nx-1) - nxs) & 0xf) + 0x10*(ny-1-nys),
716                              (col&0x1f),
717                              1,1,
718                              sx,sy);
719                     }
720                  }
721               }
722               else
723               {
724                  for (nys=0; nys<ny; nys++)
725                  {
726                     for (nxs=0; nxs<nx; nxs++)
727                     {
728                        sx = (x+nxs*16) & 0x1ff;
729                        sy = (y+nys*16) & 0x1ff;
730
731                        DRAWSPRITE(
732                              (code & ~0xf) + ((code + nxs) & 0xf) + 0x10*(ny-1-nys),
733                              (col&0x1f),
734                              0,1,
735                              sx,sy);
736                     }
737                  }
738               }
739            }
740            else
741            {
742               if (colour &0x20)
743               {
744                  for (nys=0; nys<ny; nys++)
745                  {
746                     for (nxs=0; nxs<nx; nxs++)
747                     {
748                        sx = (x+nxs*16) & 0x1ff;
749                        sy = (y+nys*16) & 0x1ff;
750
751                        DRAWSPRITE(
752                              (code & ~0xf) + ((code + (nx-1) - nxs) & 0xf) + 0x10*nys,
753                              (col&0x1f),
754                              1,0,
755                              sx,sy);
756                     }
757                  }
758               }
759               else
760               {
761                  for (nys=0; nys<ny; nys++)
762                  {
763                     for (nxs=0; nxs<nx; nxs++)
764                     {
765                        sx = (x+nxs*16) & 0x1ff;
766                        sy = (y+nys*16) & 0x1ff;
767
768                        DRAWSPRITE(
769                              (code & ~0xf) + ((code + nxs) & 0xf) + 0x10*nys,   // fix 00406: qadj: When playing as the ninja, there is one broekn frame in his animation loop when walking.
770                              (col&0x1f),
771                              0,0,
772                              sx,sy);
773                     }
774                  }
775               }
776            }
777         }
778         else
779         {
780            /* Simple case... 1 sprite */
781                  DRAWSPRITE(
782                  code,
783                  (col&0x1f),
784                  colour&0x20,colour&0x40,
785                  x & 0x1ff,y & 0x1ff);
786         }
787      }
788      base += baseadd;
789   }
790#undef DRAWSPRITE
791}
792
793
794static void cps1_render_stars(screen_device *screen, bitmap_t *bitmap,const rectangle *cliprect)
795{
796   cpschngr_state *state = screen->machine().driver_data<cpschngr_state>();
797   int offs;
798   UINT8 *stars_rom = screen->machine().region("stars")->base();
799
800   if (!stars_rom && (state->m_cps1_stars_enabled[0] || state->m_cps1_stars_enabled[1]))
801   {
802      return;
803   }
804
805   if (state->m_cps1_stars_enabled[0])
806   {
807      for (offs = 0;offs < cps1_stars_rom_size/2;offs++)
808      {
809         int col = stars_rom[8*offs+4];
810         if (col != 0x0f)
811         {
812            int sx = (offs / 256) * 32;
813            int sy = (offs % 256);
814            sx = (sx - state->m_stars2x + (col & 0x1f)) & 0x1ff;
815            sy = (sy - state->m_stars2y) & 0xff;
816            if (flip_screen_get(screen->machine()))
817            {
818               sx = 511 - sx;
819               sy = 255 - sy;
820            }
821
822            col = ((col & 0xe0) >> 1) + (screen->frame_number()/16 & 0x0f);
823
824            if (sx >= cliprect->min_x && sx <= cliprect->max_x &&
825               sy >= cliprect->min_y && sy <= cliprect->max_y)
826               *BITMAP_ADDR16(bitmap, sy, sx) = 0xa00 + col;
827         }
828      }
829   }
830
831   if (state->m_cps1_stars_enabled[1])
832   {
833      for (offs = 0;offs < cps1_stars_rom_size/2;offs++)
834      {
835         int col = stars_rom[8*offs];
836         if (col != 0x0f)
837         {
838            int sx = (offs / 256) * 32;
839            int sy = (offs % 256);
840            sx = (sx - state->m_stars1x + (col & 0x1f)) & 0x1ff;
841            sy = (sy - state->m_stars1y) & 0xff;
842            if (flip_screen_get(screen->machine()))
843            {
844               sx = 511 - sx;
845               sy = 255 - sy;
846            }
847
848            col = ((col & 0xe0) >> 1) + (screen->frame_number()/16 & 0x0f);
849
850            if (sx >= cliprect->min_x && sx <= cliprect->max_x &&
851               sy >= cliprect->min_y && sy <= cliprect->max_y)
852               *BITMAP_ADDR16(bitmap, sy, sx) = 0x800 + col;
853         }
854      }
855   }
856}
857
858
859static void cps1_render_layer(running_machine &machine, bitmap_t *bitmap, const rectangle *cliprect, int layer, int primask)
860{
861   cpschngr_state *state = machine.driver_data<cpschngr_state>();
862   switch (layer)
863   {
864      case 0:
865         cps1_render_sprites(machine,bitmap,cliprect);
866         break;
867      case 1:
868      case 2:
869      case 3:
870         tilemap_draw(bitmap,cliprect,state->m_cps1_bg_tilemap[layer-1],TILEMAP_DRAW_LAYER1,primask);
871         break;
872   }
873}
874
875static void cps1_render_high_layer(running_machine &machine, bitmap_t *bitmap, const rectangle *cliprect, int layer)
876{
877   cpschngr_state *state = machine.driver_data<cpschngr_state>();
878   switch (layer)
879   {
880      case 0:
881         /* there are no high priority sprites */
882         break;
883      case 1:
884      case 2:
885      case 3:
886         tilemap_draw(NULL,cliprect,state->m_cps1_bg_tilemap[layer-1],TILEMAP_DRAW_LAYER0,1);
887         break;
888   }
889}
890
891
892/***************************************************************************
893
894    Refresh screen
895
896***************************************************************************/
897
898SCREEN_UPDATE( cps1 )
899{
900   cpschngr_state *state = screen->machine().driver_data<cpschngr_state>();
901   int layercontrol,l0,l1,l2,l3;
902   int videocontrol = state->m_cps1_cps_a_regs[CPS1_VIDEOCONTROL];
903
904
905   flip_screen_set(screen->machine(), videocontrol & 0x8000);
906
907   layercontrol = state->m_cps1_cps_b_regs[state->m_cps1_game_config->layer_control/2];
908
909   /* Get video memory base registers */
910   cps1_get_video_base(state);
911
912   /* Find the offset of the last sprite in the sprite table */
913   cps1_find_last_sprite(state);
914   cps1_update_transmasks(state);
915
916   tilemap_set_scrollx(state->m_cps1_bg_tilemap[0],0,state->m_cps1_scroll1x);
917   tilemap_set_scrolly(state->m_cps1_bg_tilemap[0],0,state->m_cps1_scroll1y);
918   if (videocontrol & 0x01)   /* linescroll enable */
919   {
920      int scrly=-state->m_cps1_scroll2y;
921      int i;
922      int otheroffs;
923
924      tilemap_set_scroll_rows(state->m_cps1_bg_tilemap[1],1024);
925
926      otheroffs = state->m_cps1_cps_a_regs[CPS1_ROWSCROLL_OFFS];
927
928      for (i = 0;i < 256;i++)
929         tilemap_set_scrollx(state->m_cps1_bg_tilemap[1],(i - scrly) & 0x3ff,state->m_cps1_scroll2x + state->m_cps1_other[(i + otheroffs) & 0x3ff]);
930   }
931   else
932   {
933      tilemap_set_scroll_rows(state->m_cps1_bg_tilemap[1],1);
934      tilemap_set_scrollx(state->m_cps1_bg_tilemap[1],0,state->m_cps1_scroll2x);
935   }
936   tilemap_set_scrolly(state->m_cps1_bg_tilemap[1],0,state->m_cps1_scroll2y);
937   tilemap_set_scrollx(state->m_cps1_bg_tilemap[2],0,state->m_cps1_scroll3x);
938   tilemap_set_scrolly(state->m_cps1_bg_tilemap[2],0,state->m_cps1_scroll3y);
939
940
941   /* Blank screen */
942   bitmap_fill(bitmap,cliprect,0xbff);
943
944   cps1_render_stars(screen, bitmap,cliprect);
945
946   /* Draw layers (0 = sprites, 1-3 = tilemaps) */
947   l0 = (layercontrol >> 0x06) & 03;
948   l1 = (layercontrol >> 0x08) & 03;
949   l2 = (layercontrol >> 0x0a) & 03;
950   l3 = (layercontrol >> 0x0c) & 03;
951   bitmap_fill(screen->machine().priority_bitmap,cliprect,0);
952
953   cps1_render_layer(screen->machine(),bitmap,cliprect,l0,0);
954   if (l1 == 0) cps1_render_high_layer(screen->machine(), bitmap, cliprect, l0); /* prepare mask for sprites */
955   cps1_render_layer(screen->machine(),bitmap,cliprect,l1,0);
956   if (l2 == 0) cps1_render_high_layer(screen->machine(), bitmap, cliprect, l1); /* prepare mask for sprites */
957   cps1_render_layer(screen->machine(),bitmap,cliprect,l2,0);
958   if (l3 == 0) cps1_render_high_layer(screen->machine(), bitmap, cliprect, l2); /* prepare mask for sprites */
959   cps1_render_layer(screen->machine(),bitmap,cliprect,l3,0);
960
961   return 0;
962}
963
964SCREEN_EOF( cps1 )
965{
966   cpschngr_state *state = machine.driver_data<cpschngr_state>();
967   /* Get video memory base registers */
968   cps1_get_video_base(state);
969
970   /* CPS1 sprites have to be delayed one frame */
971   memcpy(state->m_cps1_buffered_obj, state->m_cps1_obj, cps1_obj_size);
972}
src/mess/includes/cpschngr.h
r11291r11292
1#ifndef _CPSCHNGHR_H_
2#define _CPSCHNGHR_H_
3
4
5class cpschngr_state : public driver_device
6{
7public:
8   cpschngr_state(running_machine &machine, const driver_device_config_base &config)
9      : driver_device(machine, config) { }
10
11   UINT8 *m_qsound_sharedram1;
12   UINT8 *m_qsound_sharedram2;
13   UINT16 *m_cps1_gfxram;
14   UINT16 *m_cps1_cps_a_regs;
15   UINT16 *m_cps1_cps_b_regs;
16   size_t m_cps1_gfxram_size;
17   UINT16 *m_cps1_scroll1;
18   UINT16 *m_cps1_scroll2;
19   UINT16 *m_cps1_scroll3;
20   UINT16 *m_cps1_obj;
21   UINT16 *m_cps1_buffered_obj;
22   UINT16 *m_cps1_other;
23   int m_cps1_last_sprite_offset;
24   int m_cps1_stars_enabled[2];
25   tilemap_t *m_cps1_bg_tilemap[3];
26   int m_cps1_scroll1x;
27   int m_cps1_scroll1y;
28   int m_cps1_scroll2x;
29   int m_cps1_scroll2y;
30   int m_cps1_scroll3x;
31   int m_cps1_scroll3y;
32   int m_stars1x;
33   int m_stars1y;
34   int m_stars2x;
35   int m_stars2y;
36   const struct CPS1config *m_cps1_game_config;
37   UINT8 m_empty_tile8x8[8*8];
38   UINT8 m_empty_tile[32*32/2];
39};
40
41
42/*----------- defined in machine/kabuki.c -----------*/
43
44void wof_decode(running_machine &machine);
45
46
47/*----------- defined in video/cpschngr.c -----------*/
48
49WRITE16_HANDLER( cps1_cps_a_w );
50WRITE16_HANDLER( cps1_cps_b_w );
51READ16_HANDLER( cps1_cps_b_r );
52WRITE16_HANDLER( cps1_gfxram_w );
53
54DRIVER_INIT( cps1 );
55
56VIDEO_START( cps1 );
57SCREEN_UPDATE( cps1 );
58SCREEN_EOF( cps1 );
59
60#endif

Previous 15994 Revisions Next


© 1998-2018 The MESS Team