Previous 15994 Revisions Next

r13436 Thursday 1st December, 2011 at 20:03:17 UTC by Wilbert Pol
smsvdp.c: Converted to C++.
[src/mess/drivers]sms.c
[src/mess/includes]sms.h
[src/mess/machine]sms.c
[src/mess/video]smsvdp.c smsvdp.h

src/mess/machine/sms.c
r13435r13436
11#include "emu.h"
22#include "crsshair.h"
33#include "image.h"
4#include "includes/sms.h"
54#include "video/smsvdp.h"
65#include "sound/2413intf.h"
76#include "imagedev/cartslot.h"
7#include "includes/sms.h"
88
99#define VERBOSE 0
1010#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
r13435r13436
9494   UINT8 tmp = ((hpos_tmp - 46) >> 1) & 0xff;
9595
9696   //printf ("sms_vdp_hcount_lphaser: hpos %3d hpos_tmp %3d => hcount %2X\n", hpos, hpos_tmp, tmp);
97   sms_vdp_hcount_latch_w(state->m_vdp, 0, tmp);
97   state->m_vdp->hcount_latch_write(*state->m_space, 0, tmp);
9898}
9999
100100
r13435r13436
167167
168168      if (!pos_changed)
169169      {
170         result = sms_vdp_check_brightness(state->m_vdp, beam_x, beam_y);
170         result = state->m_vdp->check_brightness(beam_x, beam_y);
171171         /* next check at same line */
172172         beam_x += LGUN_X_INTERVAL;
173173         pos_changed = 1;
r13435r13436
210210}
211211
212212
213static void sms_vdp_hcount_latch( running_machine &machine )
213static void sms_vdp_hcount_latch( address_space *space )
214214{
215   sms_state *state = machine.driver_data<sms_state>();
216   UINT8 value = sms_vdp_hcount(machine);
215   sms_state *state = space->machine().driver_data<sms_state>();
216   UINT8 value = sms_vdp_hcount(space->machine());
217217
218   sms_vdp_hcount_latch_w(state->m_vdp, 0, value);
218   state->m_vdp->hcount_latch_write(*space, 0, value);
219219}
220220
221221
r13435r13436
522522      /* check if TH pin level is high (1) and was low last time */
523523      if (data & 0x80 && !(state->m_ctrl_reg & 0x80))
524524      {
525         sms_vdp_hcount_latch(space->machine());
525         sms_vdp_hcount_latch(space);
526526      }
527527      sms_input_write(space, 0, (data & 0x20) >> 5);
528528   }
r13435r13436
531531   {
532532      if (data & 0x20 && !(state->m_ctrl_reg & 0x20))
533533      {
534         sms_vdp_hcount_latch(space->machine());
534         sms_vdp_hcount_latch(space);
535535      }
536536      sms_input_write(space, 1, (data & 0x80) >> 7);
537537   }
r13435r13436
545545   sms_state *state = space->machine().driver_data<sms_state>();
546546
547547   if (offset & 0x01)
548      return sms_vdp_hcount_latch_r(state->m_vdp, offset);
548      return state->m_vdp->hcount_latch_read(*state->m_space, offset);
549549   else
550      return sms_vdp_vcount_r(state->m_vdp, offset);
550      return state->m_vdp->vcount_read(*state->m_space, offset);
551551}
552552
553553
r13435r13436
17071707
17081708   state->m_main_cpu = machine.device("maincpu");
17091709   state->m_control_cpu = machine.device("control");
1710   state->m_vdp = machine.device("sms_vdp");
1710   state->m_vdp = machine.device<sega315_5124_device>("sms_vdp");
17111711   state->m_ym = machine.device("ym2413");
17121712   state->m_main_scr = machine.device("screen");
17131713   state->m_left_lcd = machine.device("left_lcd");
17141714   state->m_right_lcd = machine.device("right_lcd");
1715   state->m_space = machine.device("maincpu")->memory().space(AS_PROGRAM);
17151716
17161717   /* Check if lightgun has been chosen as input: if so, enable crosshair */
17171718   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
r13435r13436
17411742      space->install_legacy_write_handler(0x0000, 0x0003, FUNC(sms_korean_zemina_banksw_w));
17421743
17431744   if (state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE)
1744      sms_vdp_set_ggsmsmode(state->m_vdp, 1);
1745   {
1746      state->m_vdp->set_gg_sms_mode(1);
1747   }
17451748
17461749   /* Initialize SIO stuff for GG */
17471750   state->m_gg_sio[0] = 0x7f;
r13435r13436
19651968
19661969   if (!occluded_view)
19671970   {
1968      sms_vdp_update(state->m_vdp, bitmap, cliprect);
1971      state->m_vdp->update_video(bitmap, cliprect);
19691972
19701973      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
19711974      // save a copy of current bitmap for the binocular hack
r13435r13436
19921995SCREEN_UPDATE( sms )
19931996{
19941997   sms_state *state = screen->machine().driver_data<sms_state>();
1995   sms_vdp_update(state->m_vdp, bitmap, cliprect);
1998   state->m_vdp->update_video(bitmap, cliprect);
19961999   return 0;
19972000}
19982001
r13435r13436
20152018   int height = screen->height();
20162019   int x, y;
20172020
2018   sms_vdp_update(state->m_vdp, state->m_tmp_bitmap, cliprect);
2021   state->m_vdp->update_video(state->m_tmp_bitmap, cliprect);
20192022
20202023   // HACK: fake LCD persistence effect
20212024   // (it would be better to generalize this in the core, to be used for all LCD systems)
src/mess/video/smsvdp.c
r13435r13436
4242  - bottom border      48  C0-EF       32  E0-FF      24  F0-07
4343  - bottom blanking     3  F0-F2        3  00-02       3  08-0A
4444
45  TODO:
46    - implement differences between SMS1_VDP & SMS_VDP/GG_VDP
47
4845*/
4946
5047#include "emu.h"
5148#include "video/smsvdp.h"
49#include "machine/devhelpr.h"
5250
53#define IS_SMS1_VDP           (smsvdp->m_features & MODEL_315_5124)
54#define IS_SMS2_VDP           (smsvdp->m_features & MODEL_315_5246)
55#define IS_GAMEGEAR_VDP       (smsvdp->m_features & MODEL_315_5378)
5651
5752#define STATUS_VINT           0x80   /* Pending vertical interrupt flag */
5853#define STATUS_SPROVR         0x40   /* Sprite overflow flag */
r13435r13436
6762#define SPRCOL_BASEHPOS       42
6863#define DISPLAY_CB_HPOS       5  /* fix X-Scroll latchtime (Flubba's VDPTest) */
6964
70#define GG_CRAM_SIZE          0x40   /* 32 colors x 2 bytes per color = 64 bytes */
71#define SMS_CRAM_SIZE         0x20   /* 32 colors x 1 bytes per color = 32 bytes */
72#define MAX_CRAM_SIZE         0x40
65#define SEGA315_5378_CRAM_SIZE    0x40   /* 32 colors x 2 bytes per color = 64 bytes */
66#define SEGA315_5124_CRAM_SIZE    0x20   /* 32 colors x 1 bytes per color = 32 bytes */
7367
7468#define VRAM_SIZE             0x4000
7569
7670#define PRIORITY_BIT          0x1000
77#define BACKDROP_COLOR        ((smsvdp->m_vdp_mode == 4 ? 0x10 : 0x00) + (smsvdp->m_reg[0x07] & 0x0f))
71#define BACKDROP_COLOR        ((m_vdp_mode == 4 ? 0x10 : 0x00) + (m_reg[0x07] & 0x0f))
7872
79#define NUM_OF_REGISTER       0x10  /* 16 registers */
80
8173#define INIT_VCOUNT           0
8274#define VERTICAL_BLANKING     1
8375#define TOP_BLANKING          2
r13435r13436
8678#define BOTTOM_BORDER         5
8779#define BOTTOM_BLANKING       6
8880
89static const UINT8 sms_ntsc_192[7] = { 0xd5, 3, 13, 27, 192, 24, 3 };
90static const UINT8 sms_ntsc_224[7] = { 0xe5, 3, 13, 11, 224,  8, 3 };
91static const UINT8 sms_ntsc_240[7] = { 0xee, 3, 13,  3, 240,  0, 3 };
92static const UINT8 sms_pal_192[7]  = { 0xba, 3, 13, 54, 192, 48, 3 };
93static const UINT8 sms_pal_224[7]  = { 0xca, 3, 13, 38, 224, 32, 3 };
94static const UINT8 sms_pal_240[7]  = { 0xd2, 3, 13, 30, 240, 24, 3 };
81static const UINT8 ntsc_192[7] = { 0xd5, 3, 13, 27, 192, 24, 3 };
82static const UINT8 ntsc_224[7] = { 0xe5, 3, 13, 11, 224,  8, 3 };
83static const UINT8 ntsc_240[7] = { 0xee, 3, 13,  3, 240,  0, 3 };
84static const UINT8 pal_192[7]  = { 0xba, 3, 13, 54, 192, 48, 3 };
85static const UINT8 pal_224[7]  = { 0xca, 3, 13, 38, 224, 32, 3 };
86static const UINT8 pal_240[7]  = { 0xd2, 3, 13, 30, 240, 24, 3 };
9587
9688
97typedef struct _smsvdp_t smsvdp_t;
98struct _smsvdp_t
89const device_type SEGA315_5124 = &device_creator<sega315_5124_device>;
90const device_type SEGA315_5246 = &device_creator<sega315_5246_device>;
91const device_type SEGA315_5378 = &device_creator<sega315_5378_device>;
92
93
94PALETTE_INIT( sega315_5124 )
9995{
100   UINT32           m_features;
101   UINT8            m_reg[NUM_OF_REGISTER];     /* All the registers */
102   UINT8            m_status;                   /* Status register */
103   UINT8            m_reg9copy;                 /* Internal copy of register 9 */
104   UINT8            m_addrmode;                 /* Type of VDP action */
105   UINT16           m_addr;                     /* Contents of internal VDP address register */
106   UINT8            m_cram_mask;                /* Mask to switch between SMS and GG CRAM sizes */
107   int              m_cram_dirty;               /* Have there been any changes to the CRAM area */
108   int              m_pending;
109   UINT8            m_buffer;
110   int              m_gg_sms_mode;              /* Shrunk SMS screen on GG lcd mode flag */
111   int              m_irq_state;                /* The status of the IRQ line of the VDP */
112   int              m_vdp_mode;                 /* Current mode of the VDP: 0,1,2,3,4 */
113   int              m_y_pixels;                 /* 192, 224, 240 */
114   UINT8            m_line_counter;
115   UINT8            m_hcounter;
116   memory_region    *m_VRAM;                    /* Pointer to VRAM */
117   memory_region    *m_CRAM;                    /* Pointer to CRAM */
118   const UINT8      *m_sms_frame_timing;
119   bitmap_t         *m_tmpbitmap;
120   UINT8            *m_collision_buffer;
96   int i;
97   for (i = 0; i < 64; i++)
98   {
99      int r = i & 0x03;
100      int g = (i & 0x0c) >> 2;
101      int b = (i & 0x30) >> 4;
102      palette_set_color_rgb(machine, i, r << 6, g << 6, b << 6);
103   }
104   /* TMS9918 palette */
105   palette_set_color_rgb(machine, 64+ 0,   0,   0,   0);
106   palette_set_color_rgb(machine, 64+ 1,   0,   0,   0);
107   palette_set_color_rgb(machine, 64+ 2,  33, 200,  66);
108   palette_set_color_rgb(machine, 64+ 3,  94, 220, 120);
109   palette_set_color_rgb(machine, 64+ 4,  84,  85, 237);
110   palette_set_color_rgb(machine, 64+ 5, 125, 118, 252);
111   palette_set_color_rgb(machine, 64+ 6, 212,  82,  77);
112   palette_set_color_rgb(machine, 64+ 7,  66, 235, 245);
113   palette_set_color_rgb(machine, 64+ 8, 252,  85,  84);
114   palette_set_color_rgb(machine, 64+ 9, 255, 121, 120);
115   palette_set_color_rgb(machine, 64+10, 212, 193,  84);
116   palette_set_color_rgb(machine, 64+11, 230, 206, 128);
117   palette_set_color_rgb(machine, 64+12,  33, 176,  59);
118   palette_set_color_rgb(machine, 64+13, 201,  91, 186);
119   palette_set_color_rgb(machine, 64+14, 204, 204, 204);
120   palette_set_color_rgb(machine, 64+15, 255, 255, 255);
121}
121122
122   /* line_buffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
123       Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
124       sms compatibility mode. */
125   int              *m_line_buffer;
126   int              m_current_palette[32];
127   devcb_resolved_write_line   m_int_callback;
128   devcb_resolved_write_line   m_pause_callback;
129   emu_timer        *m_smsvdp_display_timer;
130   screen_device    *m_screen;
131};
132123
133static TIMER_CALLBACK( smsvdp_display_callback );
134static void sms_refresh_line( running_machine &machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int offsetx, int offsety, int line );
135static void sms_update_palette( smsvdp_t *smsvdp );
124PALETTE_INIT( sega315_5378 )
125{
126   int i;
127   for (i = 0; i < 4096; i++)
128   {
129      int r = i & 0x000f;
130      int g = (i & 0x00f0) >> 4;
131      int b = (i & 0x0f00) >> 8;
132      palette_set_color_rgb(machine, i, r << 4, g << 4, b << 4);
133   }
134}
136135
137136
138/*****************************************************************************
139    INLINE FUNCTIONS
140*****************************************************************************/
137sega315_5124_device::sega315_5124_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
138   : device_t( mconfig, SEGA315_5124, "Sega 315-5124", tag, owner, clock )
139   , m_cram_size( SEGA315_5124_CRAM_SIZE )
140   , m_palette_offset( 0 )
141{
142}
141143
142INLINE smsvdp_t *get_safe_token(device_t *device)
144
145sega315_5124_device::sega315_5124_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock, UINT8 cram_size, UINT8 palette_offset)
146   : device_t( mconfig, type, name, tag, owner, clock )
147   , m_cram_size( cram_size )
148   , m_palette_offset( palette_offset )
143149{
144   assert(device != NULL);
145   assert(device->type() == SMSVDP);
150}
146151
147   return (smsvdp_t *)downcast<legacy_device_base *>(device)->token();
152
153sega315_5246_device::sega315_5246_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
154   : sega315_5124_device( mconfig, SEGA315_5246, "Sega 315-5246", tag, owner, clock, SEGA315_5124_CRAM_SIZE, 0 )
155{
148156}
149157
150INLINE const smsvdp_interface *get_interface(device_t *device)
158
159sega315_5378_device::sega315_5378_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
160   : sega315_5124_device( mconfig, SEGA315_5378, "Sega 315-5378", tag, owner, clock, SEGA315_5378_CRAM_SIZE, 0x10 )
151161{
152   assert(device != NULL);
153   assert((device->type() == SMSVDP));
154   return (const smsvdp_interface *) device->static_config();
155162}
156163
157/*************************************
158 *
159 *  Utilities
160 *
161 *************************************/
162164
163static void set_display_settings( device_t *device )
165void sega315_5124_device::set_display_settings()
164166{
165   smsvdp_t *smsvdp = get_safe_token(device);
167   bool M1 = m_reg[0x01] & 0x10;
168   bool M2 = m_reg[0x00] & 0x02;
169   bool M3 = m_reg[0x01] & 0x08;
170   bool M4 = m_reg[0x00] & 0x04;
166171
167   int height = smsvdp->m_screen->height();
168   int M1, M2, M3, M4;
169   M1 = smsvdp->m_reg[0x01] & 0x10;
170   M2 = smsvdp->m_reg[0x00] & 0x02;
171   M3 = smsvdp->m_reg[0x01] & 0x08;
172   M4 = smsvdp->m_reg[0x00] & 0x04;
173   smsvdp->m_y_pixels = 192;
172   m_y_pixels = 192;
173
174174   if (M4)
175175   {
176176      /* mode 4 */
177      smsvdp->m_vdp_mode = 4;
178      if (M2 && (IS_SMS2_VDP || IS_GAMEGEAR_VDP))
177      m_vdp_mode = 4;
178   }
179   else
180   {
181      /* original TMS9918 mode */
182      if (!M1 && !M2 && !M3)
179183      {
184         m_vdp_mode = 0;
185      }
186      else
187//      if (M1 && !M2 && !M3)
188//      {
189//          m_vdp_mode = 1;
190//      }
191//      else
192      if (!M1 && M2 && !M3)
193      {
194         m_vdp_mode = 2;
195//      }
196//      else
197//      if (!M1 && !M2 && M3)
198//      {
199//          m_vdp_mode = 3;
200      }
201      else
202      {
203         logerror("Unknown video mode detected (M1 = %c, M2 = %c, M3 = %c, M4 = %c)\n", M1 ? '1' : '0', M2 ? '1' : '0', M3 ? '1' : '0', M4 ? '1' : '0');
204      }
205   }
206
207   switch (m_y_pixels)
208   {
209   case 192:
210      m_frame_timing = (m_is_pal) ? pal_192 : ntsc_192;
211      break;
212
213   case 224:
214      m_frame_timing = (m_is_pal) ? pal_224 : ntsc_224;
215      break;
216
217   case 240:
218      m_frame_timing = (m_is_pal) ? pal_240 : ntsc_240;
219      break;
220   }
221   m_cram_dirty = 1;
222}
223
224
225void sega315_5246_device::set_display_settings()
226{
227   bool M1 = m_reg[0x01] & 0x10;
228   bool M2 = m_reg[0x00] & 0x02;
229   bool M3 = m_reg[0x01] & 0x08;
230   bool M4 = m_reg[0x00] & 0x04;
231
232   m_y_pixels = 192;
233
234   if (M4)
235   {
236      /* mode 4 */
237      m_vdp_mode = 4;
238      if (M2)
239      {
180240         if (M1 && !M3)
181            smsvdp->m_y_pixels = 224;   /* 224-line display */
241         {
242            m_y_pixels = 224;   /* 224-line display */
243         }
182244         else if (!M1 && M3)
183            smsvdp->m_y_pixels = 240;   /* 240-line display */
245         {
246            m_y_pixels = 240;   /* 240-line display */
247         }
184248      }
185249   }
186250   else
r13435r13436
188252      /* original TMS9918 mode */
189253      if (!M1 && !M2 && !M3)
190254      {
191         smsvdp->m_vdp_mode = 0;
255         m_vdp_mode = 0;
192256      }
193257      else
194258//      if (M1 && !M2 && !M3)
195259//      {
196//          smsvdp->m_vdp_mode = 1;
260//          m_vdp_mode = 1;
197261//      }
198262//      else
199263      if (!M1 && M2 && !M3)
200264      {
201         smsvdp->m_vdp_mode = 2;
265         m_vdp_mode = 2;
202266//      }
203267//      else
204268//      if (!M1 && !M2 && M3)
205269//      {
206//          smsvdp->m_vdp_mode = 3;
270//          m_vdp_mode = 3;
207271      }
208272      else
209273      {
r13435r13436
211275      }
212276   }
213277
214   switch (smsvdp->m_y_pixels)
278   switch (m_y_pixels)
215279   {
216280   case 192:
217      smsvdp->m_sms_frame_timing = (height == SMS_PAL_Y_PIXELS) ? sms_pal_192 : sms_ntsc_192;
281      m_frame_timing = (m_is_pal) ? pal_192 : ntsc_192;
218282      break;
219283
220284   case 224:
221      smsvdp->m_sms_frame_timing = (height == SMS_PAL_Y_PIXELS) ? sms_pal_224 : sms_ntsc_224;
285      m_frame_timing = (m_is_pal) ? pal_224 : ntsc_224;
222286      break;
223287
224288   case 240:
225      smsvdp->m_sms_frame_timing = (height == SMS_PAL_Y_PIXELS) ? sms_pal_240 : sms_ntsc_240;
289      m_frame_timing = (m_is_pal) ? pal_240 : ntsc_240;
226290      break;
227291   }
228   smsvdp->m_cram_dirty = 1;
292   m_cram_dirty = 1;
229293}
230294
231295
232READ8_DEVICE_HANDLER( sms_vdp_vcount_r )
296void sega315_5378_device::set_display_settings()
233297{
234   smsvdp_t *smsvdp = get_safe_token(device);
235   int vpos = smsvdp->m_screen->vpos();
298   bool M1 = m_reg[0x01] & 0x10;
299   bool M2 = m_reg[0x00] & 0x02;
300   bool M3 = m_reg[0x01] & 0x08;
301   bool M4 = m_reg[0x00] & 0x04;
236302
237   if (smsvdp->m_screen->hpos() < VCOUNT_CHANGE_HPOS)
303   m_y_pixels = 192;
304
305   if (M4)
238306   {
239      vpos--;
240      if (vpos < 0)
241         vpos += smsvdp->m_screen->height();
307      /* mode 4 */
308      m_vdp_mode = 4;
309      if (M2)
310      {
311         if (M1 && !M3)
312         {
313            m_y_pixels = 224;   /* 224-line display */
314         }
315         else if (!M1 && M3)
316         {
317            m_y_pixels = 240;   /* 240-line display */
318         }
319      }
242320   }
321   else
322   {
323      /* original TMS9918 mode */
324      if (!M1 && !M2 && !M3)
325      {
326         m_vdp_mode = 0;
327      }
328      else
329//      if (M1 && !M2 && !M3)
330//      {
331//          m_vdp_mode = 1;
332//      }
333//      else
334      if (!M1 && M2 && !M3)
335      {
336         m_vdp_mode = 2;
337//      }
338//      else
339//      if (!M1 && !M2 && M3)
340//      {
341//          m_vdp_mode = 3;
342      }
343      else
344      {
345         logerror("Unknown video mode detected (M1 = %c, M2 = %c, M3 = %c, M4 = %c)\n", M1 ? '1' : '0', M2 ? '1' : '0', M3 ? '1' : '0', M4 ? '1' : '0');
346      }
347   }
243348
244   return (smsvdp->m_sms_frame_timing[INIT_VCOUNT] + vpos) & 0xff;
349   switch (m_y_pixels)
350   {
351   case 192:
352      m_frame_timing = (m_is_pal) ? pal_192 : ntsc_192;
353      break;
354
355   case 224:
356      m_frame_timing = (m_is_pal) ? pal_224 : ntsc_224;
357      break;
358
359   case 240:
360      m_frame_timing = (m_is_pal) ? pal_240 : ntsc_240;
361      break;
362   }
363   m_cram_dirty = 1;
245364}
246365
247366
248READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r )
367READ8_MEMBER( sega315_5124_device::vcount_read )
249368{
250   smsvdp_t *smsvdp = get_safe_token(device);
369   int vpos = m_screen->vpos();
251370
252   return smsvdp->m_hcounter;
371   if (m_screen->hpos() < VCOUNT_CHANGE_HPOS)
372   {
373      vpos--;
374      if (vpos < 0)
375         vpos += m_screen->height();
376   }
377
378   return (m_frame_timing[INIT_VCOUNT] + vpos) & 0xff;
253379}
254380
255WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w )
381
382READ8_MEMBER( sega315_5124_device::hcount_latch_read )
256383{
257   smsvdp_t *smsvdp = get_safe_token(device);
258
259   smsvdp->m_hcounter = data;
384   return m_hcounter;
260385}
261386
262387
263void sms_vdp_set_ggsmsmode( device_t *device, int mode )
388WRITE8_MEMBER( sega315_5124_device::hcount_latch_write )
264389{
265   smsvdp_t *smsvdp = get_safe_token(device);
266
267   smsvdp->m_gg_sms_mode = mode;
268   smsvdp->m_cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->m_gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
390   m_hcounter = data;
269391}
270392
271393
272static TIMER_CALLBACK( smsvdp_set_status )
394void sega315_5378_device::set_gg_sms_mode( int mode )
273395{
274   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
275
276   smsvdp->m_status |= (UINT8) param;
396   m_gg_sms_mode = mode;
397   m_cram_mask = (!m_gg_sms_mode) ? (SEGA315_5378_CRAM_SIZE - 1) : (SEGA315_5124_CRAM_SIZE - 1);
277398}
278399
279400
280static TIMER_CALLBACK( smsvdp_check_hint )
401void sega315_5124_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
281402{
282   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
283
284   if (smsvdp->m_line_counter == 0x00)
403   switch( id )
285404   {
286      smsvdp->m_line_counter = smsvdp->m_reg[0x0a];
287      smsvdp->m_status |= STATUS_HINT;
288   }
289   else
290   {
291      smsvdp->m_line_counter--;
292   }
405   case TIMER_LINE:
406      process_line_timer();
407      break;
293408
294   if ((smsvdp->m_status & STATUS_HINT) && (smsvdp->m_reg[0x00] & 0x10))
295   {
296      smsvdp->m_irq_state = 1;
409   case TIMER_SET_STATUS_VINT:
410      m_status |= STATUS_VINT;
411      break;
297412
298      smsvdp->m_int_callback(ASSERT_LINE);
299   }
300}
413   case TIMER_SET_STATUS_SPROVR:
414      m_status |= STATUS_SPROVR;
415      break;
301416
417   case TIMER_CHECK_HINT:
418      if (m_line_counter == 0x00)
419      {
420         m_line_counter = m_reg[0x0a];
421         m_status |= STATUS_HINT;
422      }
423      else
424      {
425         m_line_counter--;
426      }
302427
303static TIMER_CALLBACK( smsvdp_check_vint )
304{
305   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
428      if ((m_status & STATUS_HINT) && (m_reg[0x00] & 0x10))
429      {
430         m_irq_state = 1;
306431
307   if ((smsvdp->m_status & STATUS_VINT) && (smsvdp->m_reg[0x01] & 0x20))
308   {
309      smsvdp->m_irq_state = 1;
432         if ( !m_cb_int.isnull() )
433            m_cb_int(ASSERT_LINE);
434      }
435      break;
310436
311      smsvdp->m_int_callback(ASSERT_LINE);
437   case TIMER_CHECK_VINT:
438      if ((m_status & STATUS_VINT) && (m_reg[0x01] & 0x20))
439      {
440         m_irq_state = 1;
441
442         if ( !m_cb_int.isnull() )
443            m_cb_int(ASSERT_LINE);
444      }
445      break;
312446   }
313447}
314448
315449
316static TIMER_CALLBACK( smsvdp_display_callback )
450void sega315_5124_device::process_line_timer()
317451{
318   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
319
320452   rectangle rec;
321   int vpos = smsvdp->m_screen->vpos();
322   int vpos_limit = smsvdp->m_sms_frame_timing[VERTICAL_BLANKING] + smsvdp->m_sms_frame_timing[TOP_BLANKING]
323                  + smsvdp->m_sms_frame_timing[TOP_BORDER] + smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V]
324                  + smsvdp->m_sms_frame_timing[BOTTOM_BORDER] + smsvdp->m_sms_frame_timing[BOTTOM_BLANKING];
453   int vpos = m_screen->vpos();
454   int vpos_limit = m_frame_timing[VERTICAL_BLANKING] + m_frame_timing[TOP_BLANKING]
455                  + m_frame_timing[TOP_BORDER] + m_frame_timing[ACTIVE_DISPLAY_V]
456                  + m_frame_timing[BOTTOM_BORDER] + m_frame_timing[BOTTOM_BLANKING];
325457
326458   rec.min_y = rec.max_y = vpos;
327459
328460   /* Check if we're on the last line of a frame */
329461   if (vpos == vpos_limit - 1)
330462   {
331      smsvdp->m_line_counter = smsvdp->m_reg[0x0a];
332      smsvdp->m_pause_callback(0);
463      m_line_counter = m_reg[0x0a];
464      if ( !m_cb_pause.isnull() )
465         m_cb_pause(0);
333466
334467      return;
335468   }
336469
337   vpos_limit -= smsvdp->m_sms_frame_timing[BOTTOM_BLANKING];
470   vpos_limit -= m_frame_timing[BOTTOM_BLANKING];
338471
339472   /* Check if we're below the bottom border */
340473   if (vpos >= vpos_limit)
341474   {
342      smsvdp->m_line_counter = smsvdp->m_reg[0x0a];
475      m_line_counter = m_reg[0x0a];
343476      return;
344477   }
345478
346   vpos_limit -= smsvdp->m_sms_frame_timing[BOTTOM_BORDER];
479   vpos_limit -= m_frame_timing[BOTTOM_BORDER];
347480
348481   /* Check if we're in the bottom border area */
349482   if (vpos >= vpos_limit)
350483   {
351484      if (vpos == vpos_limit)
352485      {
353         machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(vpos, HINT_HPOS), FUNC(smsvdp_check_hint),0 ,smsvdp);
486         m_check_hint_timer->adjust( m_screen->time_until_pos( vpos, HINT_HPOS ) );
354487      }
355488      else
356489      {
357         smsvdp->m_line_counter = smsvdp->m_reg[0x0a];
490         m_line_counter = m_reg[0x0a];
358491      }
359492
360493      if (vpos == vpos_limit + 1)
361494      {
362         machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(vpos, VINT_FLAG_HPOS), FUNC(smsvdp_set_status), (int)STATUS_VINT, smsvdp);
363         machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(vpos, VINT_HPOS), FUNC(smsvdp_check_vint), 0, smsvdp);
495         m_set_status_vint_timer->adjust( m_screen->time_until_pos( vpos, VINT_FLAG_HPOS ) );
496         m_check_vint_timer->adjust( m_screen->time_until_pos( vpos, VINT_HPOS ) );
364497      }
365498
366      sms_update_palette(smsvdp);
499      update_palette();
367500
368501      /* Draw left border */
369502      rec.min_x = SMS_LBORDER_START;
370503      rec.max_x = SMS_LBORDER_START + SMS_LBORDER_X_PIXELS - 1;
371      bitmap_fill(smsvdp->m_tmpbitmap, &rec, machine.pens[smsvdp->m_current_palette[BACKDROP_COLOR]]);
504      bitmap_fill(m_tmpbitmap, &rec, machine().pens[m_current_palette[BACKDROP_COLOR]]);
372505
373506      /* Draw right border */
374507      rec.min_x = SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256;
375508      rec.max_x = rec.min_x + SMS_RBORDER_X_PIXELS - 1;
376      bitmap_fill(smsvdp->m_tmpbitmap, &rec, machine.pens[smsvdp->m_current_palette[BACKDROP_COLOR]]);
509      bitmap_fill(m_tmpbitmap, &rec, machine().pens[m_current_palette[BACKDROP_COLOR]]);
377510
378511      /* Draw middle of the border */
379512      /* We need to do this through the regular drawing function so it will */
380513      /* be included in the gamegear scaling functions */
381      sms_refresh_line(machine, smsvdp, smsvdp->m_tmpbitmap, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS, vpos_limit - smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V], vpos - (vpos_limit - smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V]));
514      refresh_line( SMS_LBORDER_START + SMS_LBORDER_X_PIXELS, vpos_limit - m_frame_timing[ACTIVE_DISPLAY_V], vpos - (vpos_limit - m_frame_timing[ACTIVE_DISPLAY_V]) );
382515      return;
383516   }
384517
385   vpos_limit -= smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V];
518   vpos_limit -= m_frame_timing[ACTIVE_DISPLAY_V];
386519
387520   /* Check if we're in the active display area */
388521   if (vpos >= vpos_limit)
389522   {
390523      if (vpos == vpos_limit)
391524      {
392         smsvdp->m_reg9copy = smsvdp->m_reg[0x09];
525         m_reg9copy = m_reg[0x09];
393526      }
394527
395      machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(vpos, HINT_HPOS), FUNC(smsvdp_check_hint),0, smsvdp);
528      m_check_hint_timer->adjust( m_screen->time_until_pos( vpos, HINT_HPOS ) );
396529
397      sms_update_palette(smsvdp);
530      update_palette();
398531
399532      /* Draw left border */
400533      rec.min_x = SMS_LBORDER_START;
401534      rec.max_x = SMS_LBORDER_START + SMS_LBORDER_X_PIXELS - 1;
402      bitmap_fill(smsvdp->m_tmpbitmap, &rec, machine.pens[smsvdp->m_current_palette[BACKDROP_COLOR]]);
535      bitmap_fill(m_tmpbitmap, &rec, machine().pens[m_current_palette[BACKDROP_COLOR]]);
403536
404537      /* Draw right border */
405538      rec.min_x = SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256;
406539      rec.max_x = rec.min_x + SMS_RBORDER_X_PIXELS - 1;
407      bitmap_fill(smsvdp->m_tmpbitmap, &rec, machine.pens[smsvdp->m_current_palette[BACKDROP_COLOR]]);
540      bitmap_fill(m_tmpbitmap, &rec, machine().pens[m_current_palette[BACKDROP_COLOR]]);
408541
409      sms_refresh_line(machine, smsvdp, smsvdp->m_tmpbitmap, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit);
542      refresh_line( SMS_LBORDER_START + SMS_LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit );
410543
411544      return;
412545   }
413546
414   vpos_limit -= smsvdp->m_sms_frame_timing[TOP_BORDER];
547   vpos_limit -= m_frame_timing[TOP_BORDER];
415548
416549   /* Check if we're in the top border area */
417550   if (vpos >= vpos_limit)
418551   {
419      smsvdp->m_line_counter = smsvdp->m_reg[0x0a];
420      sms_update_palette(smsvdp);
552      m_line_counter = m_reg[0x0a];
553      update_palette();
421554
422555      /* Draw left border */
423556      rec.min_x = SMS_LBORDER_START;
424557      rec.max_x = SMS_LBORDER_START + SMS_LBORDER_X_PIXELS - 1;
425      bitmap_fill(smsvdp->m_tmpbitmap, &rec, machine.pens[smsvdp->m_current_palette[BACKDROP_COLOR]]);
558      bitmap_fill(m_tmpbitmap, &rec, machine().pens[m_current_palette[BACKDROP_COLOR]]);
426559
427560      /* Draw right border */
428561      rec.min_x = SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256;
429562      rec.max_x = rec.min_x + SMS_RBORDER_X_PIXELS - 1;
430      bitmap_fill(smsvdp->m_tmpbitmap, &rec, machine.pens[smsvdp->m_current_palette[BACKDROP_COLOR]]);
563      bitmap_fill(m_tmpbitmap, &rec, machine().pens[m_current_palette[BACKDROP_COLOR]]);
431564
432565      /* Draw middle of the border */
433566      /* We need to do this through the regular drawing function so it will */
434567      /* be included in the gamegear scaling functions */
435      sms_refresh_line(machine, smsvdp, smsvdp->m_tmpbitmap, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS, vpos_limit + smsvdp->m_sms_frame_timing[TOP_BORDER], vpos - (vpos_limit + smsvdp->m_sms_frame_timing[TOP_BORDER]));
568      refresh_line( SMS_LBORDER_START + SMS_LBORDER_X_PIXELS, vpos_limit + m_frame_timing[TOP_BORDER], vpos - (vpos_limit + m_frame_timing[TOP_BORDER]) );
436569      return;
437570   }
438571
439572   /* we're in the vertical or top blanking area */
440   smsvdp->m_line_counter = smsvdp->m_reg[0x0a];
573   m_line_counter = m_reg[0x0a];
441574}
442575
443576
444READ8_DEVICE_HANDLER( sms_vdp_data_r )
577READ8_MEMBER( sega315_5124_device::vram_read )
445578{
446   smsvdp_t *smsvdp = get_safe_token(device);
447579   UINT8 temp;
448580
449581   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
450582   /* to the address register when in the middle of doing a command.               */
451583   /* Cosmic Spacehead needs this, among others                                    */
452584   /* Clear pending write flag */
453   smsvdp->m_pending = 0;
585   m_pending = 0;
454586
455587   /* Return read buffer contents */
456   temp = smsvdp->m_buffer;
588   temp = m_buffer;
457589
458590   /* Load read buffer */
459   smsvdp->m_buffer = smsvdp->m_VRAM->u8((smsvdp->m_addr & 0x3fff));
591   m_buffer = m_VRAM->u8(m_addr & 0x3fff);
460592
461593   /* Bump internal address register */
462   smsvdp->m_addr += 1;
594   m_addr += 1;
463595   return temp;
464596}
465597
466598
467READ8_DEVICE_HANDLER( sms_vdp_ctrl_r )
599READ8_MEMBER( sega315_5124_device::register_read )
468600{
469   smsvdp_t *smsvdp = get_safe_token(device);
601   UINT8 temp = m_status;
470602
471   UINT8 temp = smsvdp->m_status;
472
473603   /* Clear pending write flag */
474   smsvdp->m_pending = 0;
604   m_pending = 0;
475605
476   smsvdp->m_status &= ~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT);
606   m_status &= ~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT);
477607
478   if (smsvdp->m_irq_state == 1)
608   if (m_irq_state == 1)
479609   {
480      smsvdp->m_irq_state = 0;
610      m_irq_state = 0;
481611
482      smsvdp->m_int_callback(CLEAR_LINE);
612      if ( !m_cb_int.isnull() )
613         m_cb_int(CLEAR_LINE);
483614   }
484615
485616   /* low 5 bits return non-zero data (it fixes PGA Tour Golf course map introduction) */
r13435r13436
487618}
488619
489620
490WRITE8_DEVICE_HANDLER( sms_vdp_data_w )
621WRITE8_MEMBER( sega315_5124_device::vram_write )
491622{
492   smsvdp_t *smsvdp = get_safe_token(device);
493   int address;
494
495623   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
496624   /* to the address register when in the middle of doing a command.               */
497625   /* Cosmic Spacehead needs this, among others                                    */
498626   /* Clear pending write flag */
499   smsvdp->m_pending = 0;
627   m_pending = 0;
500628
501   switch(smsvdp->m_addrmode)
629   switch(m_addrmode)
502630   {
503631      case 0x00:
504632      case 0x01:
505633      case 0x02:
506         address = (smsvdp->m_addr & 0x3fff);
507         smsvdp->m_VRAM->u8(address) = data;
634         m_VRAM->u8(m_addr & 0x3fff) = data;
508635         break;
509636
510637      case 0x03:
511         address = smsvdp->m_addr & smsvdp->m_cram_mask;
512         if (data != smsvdp->m_CRAM->u8(address))
513638         {
514            smsvdp->m_CRAM->u8(address) = data;
515            smsvdp->m_cram_dirty = 1;
639            UINT16 address = m_addr & m_cram_mask;
640            if (data != m_CRAM->u8(address))
641            {
642               m_CRAM->u8(address) = data;
643               m_cram_dirty = 1;
644            }
516645         }
517646         break;
518647   }
519648
520   smsvdp->m_buffer = data;
521   smsvdp->m_addr += 1;
649   m_buffer = data;
650   m_addr += 1;
522651}
523652
524653
525WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w )
654WRITE8_MEMBER( sega315_5124_device::register_write )
526655{
527   smsvdp_t *smsvdp = get_safe_token(device);
528
529656   int reg_num;
530657
531   if (smsvdp->m_pending == 0)
658   if (m_pending == 0)
532659   {
533      smsvdp->m_addr = (smsvdp->m_addr & 0xff00) | data;
534      smsvdp->m_pending = 1;
660      m_addr = (m_addr & 0xff00) | data;
661      m_pending = 1;
535662   }
536663   else
537664   {
538665      /* Clear pending write flag */
539      smsvdp->m_pending = 0;
666      m_pending = 0;
540667
541      smsvdp->m_addrmode = (data >> 6) & 0x03;
542      smsvdp->m_addr = (data << 8) | (smsvdp->m_addr & 0xff);
543      switch (smsvdp->m_addrmode)
668      m_addrmode = (data >> 6) & 0x03;
669      m_addr = (data << 8) | (m_addr & 0xff);
670      switch (m_addrmode)
544671      {
545672      case 0:      /* VRAM reading mode */
546         smsvdp->m_buffer = smsvdp->m_VRAM->u8(smsvdp->m_addr & 0x3fff);
547         smsvdp->m_addr += 1;
673         m_buffer = m_VRAM->u8(m_addr & 0x3fff);
674         m_addr += 1;
548675         break;
549676
550677      case 1:      /* VRAM writing mode */
r13435r13436
552679
553680      case 2:      /* VDP register write */
554681         reg_num = data & 0x0f;
555         smsvdp->m_reg[reg_num] = smsvdp->m_addr & 0xff;
556         if (reg_num == 0 && smsvdp->m_addr & 0x02)
682         m_reg[reg_num] = m_addr & 0xff;
683         if ( reg_num == 0 && ( m_addr & 0x02 ) )
557684            logerror("overscan enabled.\n");
558685
559686         if (reg_num == 0 || reg_num == 1)
560            set_display_settings(device);
687            set_display_settings();
561688
562689         if (reg_num == 1)
563690         {
564            device->machine().scheduler().timer_set(smsvdp->m_screen->time_until_pos(smsvdp->m_screen->vpos(), VINT_HPOS), FUNC(smsvdp_check_vint),0 ,smsvdp);
691            m_check_vint_timer->adjust( m_screen->time_until_pos( m_screen->vpos(), VINT_HPOS) );
565692         }
566         smsvdp->m_addrmode = 0;
693         m_addrmode = 0;
567694         break;
568695
569696      case 3:      /* CRAM writing mode */
r13435r13436
573700}
574701
575702
576static void sms_refresh_line_mode4( smsvdp_t *smsvdp, int *line_buffer, int *priority_selected, int line )
703UINT16 sega315_5124_device::get_name_table_address()
577704{
578   int tile_column;
579   int x_scroll, y_scroll, x_scroll_start_column;
580   int pixel_x, pixel_plot_x;
581   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
582   int scroll_mod;
583   UINT16 name_table_address;
584   UINT8 *name_table;
705   UINT16 result;
585706
586   if (smsvdp->m_y_pixels != 192)
707   if ( m_y_pixels != 192 )
587708   {
588      name_table_address = ((smsvdp->m_reg[0x02] & 0x0c) << 10) | 0x0700;
589      scroll_mod = 256;
709      result = ((m_reg[0x02] & 0x0c) << 10) | 0x0700;
590710   }
591711   else
592712   {
593      name_table_address = (smsvdp->m_reg[0x02] << 10) & 0x3800;
594      scroll_mod = 224;
713      result = (m_reg[0x02] << 10) & 0x3800;
595714   }
596715
597   if (IS_SMS1_VDP)
598      name_table_address = name_table_address & (((smsvdp->m_reg[0x02] & 0x01) << 10) | 0x3bff);
716   return result & (((m_reg[0x02] & 0x01) << 10) | 0x3bff);
717}
599718
719
720UINT16 sega315_5246_device::get_name_table_address()
721{
722   UINT16 result;
723
724   if ( m_y_pixels != 192 )
725   {
726      result = ((m_reg[0x02] & 0x0c) << 10) | 0x0700;
727   }
728   else
729   {
730      result = (m_reg[0x02] << 10) & 0x3800;
731   }
732
733   return result;
734}
735
736
737UINT16 sega315_5378_device::get_name_table_address()
738{
739   UINT16 result;
740
741   if ( m_y_pixels != 192 )
742   {
743      result = ((m_reg[0x02] & 0x0c) << 10) | 0x0700;
744   }
745   else
746   {
747      result = (m_reg[0x02] << 10) & 0x3800;
748   }
749
750   return result;
751}
752
753
754void sega315_5124_device::refresh_line_mode4( int *line_buffer, int *priority_selected, int line )
755{
756   int tile_column;
757   int x_scroll, y_scroll, x_scroll_start_column;
758   int pixel_x, pixel_plot_x;
759   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
760   int scroll_mod = ( m_y_pixels != 192 ) ? 256 : 224;
761   UINT16 name_table_address = get_name_table_address();
762   UINT8 *name_table;
763
600764   /* if top 2 rows of screen not affected by horizontal scrolling, then x_scroll = 0 */
601765   /* else x_scroll = m_reg[0x08]                                                       */
602   x_scroll = (((smsvdp->m_reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - smsvdp->m_reg[0x08]);
766   x_scroll = (((m_reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - m_reg[0x08]);
603767
604768   x_scroll_start_column = (x_scroll >> 3);          /* x starting column tile */
605769
r13435r13436
612776
613777      /* Rightmost 8 columns for SMS (or 2 columns for GG) not affected by */
614778      /* vertical scrolling when bit 7 of reg[0x00] is set */
615      y_scroll = ((smsvdp->m_reg[0x00] & 0x80) && (tile_column > 23)) ? 0 : smsvdp->m_reg9copy;
779      y_scroll = ((m_reg[0x00] & 0x80) && (tile_column > 23)) ? 0 : m_reg9copy;
616780
617      name_table = smsvdp->m_VRAM->base() + name_table_address + ((((line + y_scroll) % scroll_mod) >> 3) << 6);
781      name_table = m_VRAM->base() + name_table_address + ((((line + y_scroll) % scroll_mod) >> 3) << 6);
618782
619783      tile_line = ((tile_column + x_scroll_start_column) & 0x1f) * 2;
620784      tile_data = name_table[tile_line] | (name_table[tile_line + 1] << 8);
r13435r13436
629793      if (vert_selected)
630794         tile_line = 0x07 - tile_line;
631795
632      bit_plane_0 = smsvdp->m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x00);
633      bit_plane_1 = smsvdp->m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x01);
634      bit_plane_2 = smsvdp->m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x02);
635      bit_plane_3 = smsvdp->m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x03);
796      bit_plane_0 = m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x00);
797      bit_plane_1 = m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x01);
798      bit_plane_2 = m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x02);
799      bit_plane_3 = m_VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x03);
636800
637801      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
638802      {
r13435r13436
661825         if (pixel_plot_x >= 0 && pixel_plot_x < 256)
662826         {
663827//              logerror("%x %x\n", pixel_plot_x + pixel_offset_x, pixel_plot_y);
664            line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
828            line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
665829            priority_selected[pixel_plot_x] = priority_select | (pen_selected & 0x0f);
666830         }
667831      }
668832   }
669833}
670834
671static void sms_refresh_mode4_sprites( running_machine &machine, smsvdp_t *smsvdp, int *line_buffer, int *priority_selected, int pixel_plot_y, int line )
835void sega315_5124_device::refresh_mode4_sprites( int *line_buffer, int *priority_selected, int pixel_plot_y, int line )
672836{
673837   int sprite_index;
674838   int pixel_x, pixel_plot_x;
r13435r13436
676840   int sprite_col_occurred, sprite_col_x;
677841   int sprite_buffer[8], sprite_buffer_count, sprite_buffer_index;
678842   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
679   UINT8 *sprite_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x05] << 7) & 0x3f00);
843   UINT8 *sprite_table = m_VRAM->base() + ((m_reg[0x05] << 7) & 0x3f00);
680844
681845   /* Draw sprite layer */
682   sprite_height = (smsvdp->m_reg[0x01] & 0x02 ? 16 : 8);
846   sprite_height = (m_reg[0x01] & 0x02 ? 16 : 8);
683847   sprite_zoom = 1;
684848
685   if (smsvdp->m_reg[0x01] & 0x01)      /* sprite doubling */
849   if (m_reg[0x01] & 0x01)      /* sprite doubling */
686850      sprite_zoom = 2;
687851
688852   sprite_buffer_count = 0;
689   for (sprite_index = 0; (sprite_index < 64) && (sprite_table[sprite_index] != 0xd0 || smsvdp->m_y_pixels != 192) && (sprite_buffer_count < 9); sprite_index++)
853   for (sprite_index = 0; (sprite_index < 64) && (sprite_table[sprite_index] != 0xd0 || m_y_pixels != 192) && (sprite_buffer_count < 9); sprite_index++)
690854   {
691855      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
692856
r13435r13436
699863         {
700864            sprite_buffer[sprite_buffer_count] = sprite_index;
701865         }
702         else if (line >= 0 && line < smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V])
866         else if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
703867         {
704868            /* Too many sprites per line */
705            machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(pixel_plot_y + line, SPROVR_HPOS), FUNC(smsvdp_set_status), (int)STATUS_SPROVR, smsvdp);
869            m_set_status_sprovr_timer->adjust( m_screen->time_until_pos( pixel_plot_y + line, SPROVR_HPOS ) );
706870         }
707871         sprite_buffer_count++;
708872      }
709873   }
710874
711875   /* Check if display is disabled */
712   if (!(smsvdp->m_reg[0x01] & 0x40))
876   if (!(m_reg[0x01] & 0x40))
713877      return;
714878
715879   if (sprite_buffer_count > 8)
716880      sprite_buffer_count = 8;
717881
718   memset(smsvdp->m_collision_buffer, 0, SMS_X_PIXELS);
882   memset(m_collision_buffer, 0, SMS_X_PIXELS);
719883   sprite_buffer_count--;
720884
721885   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
r13435r13436
728892
729893      sprite_x = sprite_table[0x80 + (sprite_index << 1)];
730894
731      if (smsvdp->m_reg[0x00] & 0x08)
895      if (m_reg[0x00] & 0x08)
732896         sprite_x -= 0x08;    /* sprite shift */
733897
734898      sprite_tile_selected = sprite_table[0x81 + (sprite_index << 1)];
735899
736      if (smsvdp->m_reg[0x06] & 0x04)
900      if (m_reg[0x06] & 0x04)
737901         sprite_tile_selected += 256; /* pattern table select */
738902
739      if (smsvdp->m_reg[0x01] & 0x02)
903      if (m_reg[0x01] & 0x02)
740904         sprite_tile_selected &= 0x01fe; /* force even index */
741905
742906      sprite_line = (line - sprite_y) / sprite_zoom;
r13435r13436
744908      if (sprite_line > 0x07)
745909         sprite_tile_selected += 1;
746910
747      bit_plane_0 = smsvdp->m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x00);
748      bit_plane_1 = smsvdp->m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x01);
749      bit_plane_2 = smsvdp->m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x02);
750      bit_plane_3 = smsvdp->m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x03);
911      bit_plane_0 = m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x00);
912      bit_plane_1 = m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x01);
913      bit_plane_2 = m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x02);
914      bit_plane_3 = m_VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x03);
751915
752916      sprite_col_occurred = 0;
753917      sprite_col_x = 0;
r13435r13436
769933            continue;
770934         }
771935
772         if (smsvdp->m_reg[0x01] & 0x01)
936         if (m_reg[0x01] & 0x01)
773937         {
774938            /* sprite doubling is enabled */
775939            pixel_plot_x = sprite_x + (pixel_x << 1);
r13435r13436
782946
783947            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
784948            {
785               line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
786               line_buffer[pixel_plot_x + 1] = smsvdp->m_current_palette[pen_selected];
949               line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
950               line_buffer[pixel_plot_x + 1] = m_current_palette[pen_selected];
787951            }
788952            else
789953            {
790954               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
791955               {
792                  line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
956                  line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
793957               }
794958               if (priority_selected[pixel_plot_x + 1] == PRIORITY_BIT)
795959               {
796                  line_buffer[pixel_plot_x + 1] = smsvdp->m_current_palette[pen_selected];
960                  line_buffer[pixel_plot_x + 1] = m_current_palette[pen_selected];
797961               }
798962            }
799            if (smsvdp->m_collision_buffer[pixel_plot_x] != 1)
963            if (m_collision_buffer[pixel_plot_x] != 1)
800964            {
801               smsvdp->m_collision_buffer[pixel_plot_x] = 1;
965               m_collision_buffer[pixel_plot_x] = 1;
802966            }
803967            else
804968            {
r13435r13436
808972                  sprite_col_x = pixel_plot_x;
809973               }
810974            }
811            if (smsvdp->m_collision_buffer[pixel_plot_x + 1] != 1)
975            if (m_collision_buffer[pixel_plot_x + 1] != 1)
812976            {
813               smsvdp->m_collision_buffer[pixel_plot_x + 1] = 1;
977               m_collision_buffer[pixel_plot_x + 1] = 1;
814978            }
815979            else
816980            {
r13435r13436
833997
834998            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
835999            {
836               line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1000               line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
8371001            }
8381002            else
8391003            {
8401004               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
8411005               {
842                  line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1006                  line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
8431007               }
8441008            }
845            if (smsvdp->m_collision_buffer[pixel_plot_x] != 1)
1009            if (m_collision_buffer[pixel_plot_x] != 1)
8461010            {
847               smsvdp->m_collision_buffer[pixel_plot_x] = 1;
1011               m_collision_buffer[pixel_plot_x] = 1;
8481012            }
8491013            else
8501014            {
r13435r13436
8571021         }
8581022      }
8591023      if (sprite_col_occurred)
860         machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x), FUNC(smsvdp_set_status), (int)STATUS_SPRCOL, smsvdp);
1024      {
1025         m_set_status_sprovr_timer->adjust( m_screen->time_until_pos( pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x ) );
1026      }
8611027   }
8621028
8631029   /* Fill column 0 with overscan color from m_reg[0x07] */
864   if (smsvdp->m_reg[0x00] & 0x20)
1030   if (m_reg[0x00] & 0x20)
8651031   {
866      line_buffer[0] = smsvdp->m_current_palette[BACKDROP_COLOR];
867      line_buffer[1] = smsvdp->m_current_palette[BACKDROP_COLOR];
868      line_buffer[2] = smsvdp->m_current_palette[BACKDROP_COLOR];
869      line_buffer[3] = smsvdp->m_current_palette[BACKDROP_COLOR];
870      line_buffer[4] = smsvdp->m_current_palette[BACKDROP_COLOR];
871      line_buffer[5] = smsvdp->m_current_palette[BACKDROP_COLOR];
872      line_buffer[6] = smsvdp->m_current_palette[BACKDROP_COLOR];
873      line_buffer[7] = smsvdp->m_current_palette[BACKDROP_COLOR];
1032      line_buffer[0] = m_current_palette[BACKDROP_COLOR];
1033      line_buffer[1] = m_current_palette[BACKDROP_COLOR];
1034      line_buffer[2] = m_current_palette[BACKDROP_COLOR];
1035      line_buffer[3] = m_current_palette[BACKDROP_COLOR];
1036      line_buffer[4] = m_current_palette[BACKDROP_COLOR];
1037      line_buffer[5] = m_current_palette[BACKDROP_COLOR];
1038      line_buffer[6] = m_current_palette[BACKDROP_COLOR];
1039      line_buffer[7] = m_current_palette[BACKDROP_COLOR];
8741040   }
8751041}
8761042
8771043
878static void sms_refresh_tms9918_sprites( running_machine &machine, smsvdp_t *smsvdp, int *line_buffer, int pixel_plot_y, int line )
1044void sega315_5124_device::refresh_tms9918_sprites( int *line_buffer, int pixel_plot_y, int line )
8791045{
8801046   int pixel_plot_x;
8811047   int sprite_col_occurred, sprite_col_x;
r13435r13436
8831049   UINT8 *sprite_table, *sprite_pattern_table;
8841050
8851051   /* Draw sprite layer */
886   sprite_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x05] & 0x7f) << 7);
887   sprite_pattern_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x06] & 0x07) << 11);
1052   sprite_table = m_VRAM->base() + ((m_reg[0x05] & 0x7f) << 7);
1053   sprite_pattern_table = m_VRAM->base() + ((m_reg[0x06] & 0x07) << 11);
8881054   sprite_height = 8;
8891055
890   if (smsvdp->m_reg[0x01] & 0x02)                         /* Check if SI is set */
1056   if (m_reg[0x01] & 0x02)                         /* Check if SI is set */
8911057      sprite_height = sprite_height * 2;
892   if (smsvdp->m_reg[0x01] & 0x01)                         /* Check if MAG is set */
1058   if (m_reg[0x01] & 0x01)                         /* Check if MAG is set */
8931059      sprite_height = sprite_height * 2;
8941060
8951061   sprite_buffer_count = 0;
r13435r13436
9061072         {
9071073            sprite_buffer[sprite_buffer_count] = sprite_index;
9081074         }
909         else if (line >= 0 && line < smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V])
1075         else if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
9101076         {
9111077            /* Too many sprites per line */
912            machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(pixel_plot_y + line, SPROVR_HPOS), FUNC(smsvdp_set_status), (int)STATUS_SPROVR, smsvdp);
1078            m_set_status_sprovr_timer->adjust( m_screen->time_until_pos( pixel_plot_y + line, SPROVR_HPOS ) );
9131079         }
9141080         sprite_buffer_count++;
9151081      }
9161082   }
9171083
9181084   /* Check if display is disabled */
919   if (!(smsvdp->m_reg[0x01] & 0x40))
1085   if (!(m_reg[0x01] & 0x40))
9201086      return;
9211087
9221088   if (sprite_buffer_count > 4)
9231089      sprite_buffer_count = 4;
9241090
925   memset(smsvdp->m_collision_buffer, 0, SMS_X_PIXELS);
1091   memset(m_collision_buffer, 0, SMS_X_PIXELS);
9261092   sprite_buffer_count--;
9271093
9281094   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
r13435r13436
9391105         sprite_y -= 256;
9401106
9411107      sprite_x = sprite_table[sprite_index + 1];
942      pen_selected = sprite_table[sprite_index + 3] & 0x0f;
1108      pen_selected = m_palette_offset + ( sprite_table[sprite_index + 3] & 0x0f );
9431109
944      if (IS_GAMEGEAR_VDP)
945         pen_selected |= 0x10;
946
9471110      if (sprite_table[sprite_index + 3] & 0x80)
9481111         sprite_x -= 32;
9491112
9501113      sprite_tile_selected = sprite_table[sprite_index + 2];
9511114      sprite_line = line - sprite_y;
9521115
953      if (smsvdp->m_reg[0x01] & 0x01)
1116      if (m_reg[0x01] & 0x01)
9541117         sprite_line >>= 1;
9551118
956      if (smsvdp->m_reg[0x01] & 0x02)
1119      if (m_reg[0x01] & 0x02)
9571120      {
9581121         sprite_tile_selected &= 0xfc;
9591122
r13435r13436
9711134
9721135      for (pixel_x = 0; pixel_x < 8; pixel_x++)
9731136      {
974         if (smsvdp->m_reg[0x01] & 0x01)
1137         if (m_reg[0x01] & 0x01)
9751138         {
9761139            pixel_plot_x = sprite_x + pixel_x * 2;
9771140            if (pixel_plot_x < 0 || pixel_plot_x > 255)
r13435r13436
9811144
9821145            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
9831146            {
984               line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1147               line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
9851148
986               if (smsvdp->m_collision_buffer[pixel_plot_x] != 1)
1149               if (m_collision_buffer[pixel_plot_x] != 1)
9871150               {
988                  smsvdp->m_collision_buffer[pixel_plot_x] = 1;
1151                  m_collision_buffer[pixel_plot_x] = 1;
9891152               }
9901153               else
9911154               {
r13435r13436
9961159                  }
9971160               }
9981161
999               line_buffer[pixel_plot_x+1] = smsvdp->m_current_palette[pen_selected];
1162               line_buffer[pixel_plot_x+1] = m_current_palette[pen_selected];
10001163
1001               if (smsvdp->m_collision_buffer[pixel_plot_x + 1] != 1)
1164               if (m_collision_buffer[pixel_plot_x + 1] != 1)
10021165               {
1003                  smsvdp->m_collision_buffer[pixel_plot_x + 1] = 1;
1166                  m_collision_buffer[pixel_plot_x + 1] = 1;
10041167               }
10051168               else
10061169               {
r13435r13436
10231186
10241187            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
10251188            {
1026               line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1189               line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
10271190
1028               if (smsvdp->m_collision_buffer[pixel_plot_x] != 1)
1191               if (m_collision_buffer[pixel_plot_x] != 1)
10291192               {
1030                  smsvdp->m_collision_buffer[pixel_plot_x] = 1;
1193                  m_collision_buffer[pixel_plot_x] = 1;
10311194               }
10321195               else
10331196               {
r13435r13436
10411204         }
10421205      }
10431206
1044      if (smsvdp->m_reg[0x01] & 0x02)
1207      if (m_reg[0x01] & 0x02)
10451208      {
10461209         sprite_tile_selected += 2;
10471210         pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
1048         sprite_x += (smsvdp->m_reg[0x01] & 0x01 ? 16 : 8);
1211         sprite_x += (m_reg[0x01] & 0x01 ? 16 : 8);
10491212
10501213         for (pixel_x = 0; pixel_x < 8; pixel_x++)
10511214         {
1052            if (smsvdp->m_reg[0x01] & 0x01)
1215            if (m_reg[0x01] & 0x01)
10531216            {
10541217               pixel_plot_x = sprite_x + pixel_x * 2;
10551218
r13435r13436
10601223
10611224               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
10621225               {
1063                  line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1226                  line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
10641227
1065                  if (smsvdp->m_collision_buffer[pixel_plot_x] != 1)
1228                  if (m_collision_buffer[pixel_plot_x] != 1)
10661229                  {
1067                     smsvdp->m_collision_buffer[pixel_plot_x] = 1;
1230                     m_collision_buffer[pixel_plot_x] = 1;
10681231                  }
10691232                  else
10701233                  {
r13435r13436
10751238                     }
10761239                  }
10771240
1078                  line_buffer[pixel_plot_x+1] = smsvdp->m_current_palette[pen_selected];
1241                  line_buffer[pixel_plot_x+1] = m_current_palette[pen_selected];
10791242
1080                  if (smsvdp->m_collision_buffer[pixel_plot_x + 1] != 1)
1243                  if (m_collision_buffer[pixel_plot_x + 1] != 1)
10811244                  {
1082                     smsvdp->m_collision_buffer[pixel_plot_x + 1] = 1;
1245                     m_collision_buffer[pixel_plot_x + 1] = 1;
10831246                  }
10841247                  else
10851248                  {
r13435r13436
11021265
11031266               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
11041267               {
1105                  line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1268                  line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
11061269
1107                  if (smsvdp->m_collision_buffer[pixel_plot_x] != 1)
1270                  if (m_collision_buffer[pixel_plot_x] != 1)
11081271                  {
1109                     smsvdp->m_collision_buffer[pixel_plot_x] = 1;
1272                     m_collision_buffer[pixel_plot_x] = 1;
11101273                  }
11111274                  else
11121275                  {
r13435r13436
11211284         }
11221285      }
11231286      if (sprite_col_occurred)
1124         machine.scheduler().timer_set(smsvdp->m_screen->time_until_pos(pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x), FUNC(smsvdp_set_status), (int)STATUS_SPRCOL, smsvdp);
1287      {
1288         m_set_status_sprovr_timer->adjust( m_screen->time_until_pos( pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x ) );
1289      }
11251290   }
11261291}
11271292
11281293
1129static void sms_refresh_line_mode2( smsvdp_t *smsvdp, int *line_buffer, int line )
1294void sega315_5124_device::refresh_line_mode2( int *line_buffer, int line )
11301295{
11311296   int tile_column;
11321297   int pixel_x, pixel_plot_x;
r13435r13436
11341299   int pattern_mask, color_mask, pattern_offset;
11351300
11361301   /* Draw background layer */
1137   name_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1138   color_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x03] & 0x80) << 6);
1139   color_mask = ((smsvdp->m_reg[0x03] & 0x7f) << 3) | 0x07;
1140   pattern_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x04] & 0x04) << 11);
1141   pattern_mask = ((smsvdp->m_reg[0x04] & 0x03) << 8) | 0xff;
1302   name_table = m_VRAM->base() + ((m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1303   color_table = m_VRAM->base() + ((m_reg[0x03] & 0x80) << 6);
1304   color_mask = ((m_reg[0x03] & 0x7f) << 3) | 0x07;
1305   pattern_table = m_VRAM->base() + ((m_reg[0x04] & 0x04) << 11);
1306   pattern_mask = ((m_reg[0x04] & 0x03) << 8) | 0xff;
11421307   pattern_offset = (line & 0xc0) << 2;
11431308
11441309   for (tile_column = 0; tile_column < 32; tile_column++)
r13435r13436
11671332
11681333         pixel_plot_x = (tile_column << 3) + pixel_x;
11691334
1170         if (IS_GAMEGEAR_VDP)
1171            pen_selected |= 0x10;
1335         pen_selected += m_palette_offset;
11721336
1173         line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1337         line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
11741338      }
11751339   }
11761340}
11771341
11781342
1179static void sms_refresh_line_mode0( smsvdp_t *smsvdp, int *line_buffer, int line)
1343void sega315_5124_device::refresh_line_mode0( int *line_buffer, int line )
11801344{
11811345   int tile_column;
11821346   int pixel_x, pixel_plot_x;
11831347   UINT8 *name_table, *color_table, *pattern_table;
11841348
11851349   /* Draw background layer */
1186   name_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1187   color_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x03] << 6) & (VRAM_SIZE - 1));
1188   pattern_table = smsvdp->m_VRAM->base() + ((smsvdp->m_reg[0x04] << 11) & (VRAM_SIZE - 1));
1350   name_table = m_VRAM->base() + ((m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1351   color_table = m_VRAM->base() + ((m_reg[0x03] << 6) & (VRAM_SIZE - 1));
1352   pattern_table = m_VRAM->base() + ((m_reg[0x04] << 11) & (VRAM_SIZE - 1));
11891353
11901354   for (tile_column = 0; tile_column < 32; tile_column++)
11911355   {
r13435r13436
12041368         else
12051369            pen_selected = colors & 0x0f;
12061370
1207         if (IS_GAMEGEAR_VDP)
1208            pen_selected |= 0x10;
1371         pen_selected += m_palette_offset;
12091372
12101373         pixel_plot_x = (tile_column << 3) + pixel_x;
1211         line_buffer[pixel_plot_x] = smsvdp->m_current_palette[pen_selected];
1374         line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
12121375      }
12131376   }
12141377}
12151378
12161379
1217static void sms_refresh_line( running_machine &machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int pixel_offset_x, int pixel_plot_y, int line )
1380void sega315_5124_device::refresh_line( int pixel_offset_x, int pixel_plot_y, int line )
12181381{
12191382   int x;
1220   int *blitline_buffer = smsvdp->m_line_buffer;
1383   int *blitline_buffer = m_line_buffer;
12211384   int priority_selected[256];
12221385
1223   switch( smsvdp->m_vdp_mode )
1386   switch( m_vdp_mode )
12241387   {
12251388   case 0:
1226      if (line >= 0 && line < smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V])
1227         sms_refresh_line_mode0(smsvdp, blitline_buffer, line);
1228      sms_refresh_tms9918_sprites(machine, smsvdp, blitline_buffer, pixel_plot_y, line);
1389      if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
1390      {
1391         refresh_line_mode0( blitline_buffer, line );
1392      }
1393      refresh_tms9918_sprites( blitline_buffer, pixel_plot_y, line );
12291394      break;
12301395
12311396   case 2:
1232      if (line >= 0 && line < smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V])
1233         sms_refresh_line_mode2(smsvdp, blitline_buffer, line);
1234      sms_refresh_tms9918_sprites(machine, smsvdp, blitline_buffer, pixel_plot_y, line);
1397      if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
1398      {
1399         refresh_line_mode2( blitline_buffer, line );
1400      }
1401      refresh_tms9918_sprites( blitline_buffer, pixel_plot_y, line );
12351402      break;
12361403
12371404   case 4:
12381405   default:
12391406      memset(priority_selected, 0, sizeof(priority_selected));
1240      if (line >= 0 && line < smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V])
1241         sms_refresh_line_mode4(smsvdp, blitline_buffer, priority_selected, line);
1242      sms_refresh_mode4_sprites(machine, smsvdp, blitline_buffer, priority_selected, pixel_plot_y, line);
1407      if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
1408      {
1409         refresh_line_mode4( blitline_buffer, priority_selected, line );
1410      }
1411      refresh_mode4_sprites( blitline_buffer, priority_selected, pixel_plot_y, line );
12431412      break;
12441413   }
12451414
12461415   /* Check if display is disabled or we're below/above active area */
1247   if (!(smsvdp->m_reg[0x01] & 0x40) || line < 0 || line >= smsvdp->m_sms_frame_timing[ACTIVE_DISPLAY_V])
1416   if (!(m_reg[0x01] & 0x40) || line < 0 || line >= m_frame_timing[ACTIVE_DISPLAY_V])
12481417   {
12491418      for (x = 0; x < 256; x++)
12501419      {
1251         blitline_buffer[x] = smsvdp->m_current_palette[BACKDROP_COLOR];
1420         blitline_buffer[x] = m_current_palette[BACKDROP_COLOR];
12521421      }
12531422   }
12541423
1255   if (IS_GAMEGEAR_VDP && smsvdp->m_gg_sms_mode)
1424   for (x = 0; x < 256; x++)
12561425   {
1257      int *combineline_buffer = smsvdp->m_line_buffer + ((line & 0x03) + 1) * 256;
1426      *BITMAP_ADDR32(m_tmpbitmap, pixel_plot_y + line, pixel_offset_x + x) = machine().pens[blitline_buffer[x]];
1427   }
1428}
1429
1430
1431void sega315_5378_device::refresh_line( int pixel_offset_x, int pixel_plot_y, int line )
1432{
1433   int x;
1434   int *blitline_buffer = m_line_buffer;
1435   int priority_selected[256];
1436
1437   switch( m_vdp_mode )
1438   {
1439   case 0:
1440      if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
1441      {
1442         refresh_line_mode0( blitline_buffer, line );
1443      }
1444      refresh_tms9918_sprites( blitline_buffer, pixel_plot_y, line );
1445      break;
1446
1447   case 2:
1448      if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
1449      {
1450         refresh_line_mode2( blitline_buffer, line );
1451      }
1452      refresh_tms9918_sprites( blitline_buffer, pixel_plot_y, line );
1453      break;
1454
1455   case 4:
1456   default:
1457      memset(priority_selected, 0, sizeof(priority_selected));
1458      if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
1459      {
1460         refresh_line_mode4( blitline_buffer, priority_selected, line );
1461      }
1462      refresh_mode4_sprites( blitline_buffer, priority_selected, pixel_plot_y, line );
1463      break;
1464   }
1465
1466   /* Check if display is disabled or we're below/above active area */
1467   if (!(m_reg[0x01] & 0x40) || line < 0 || line >= m_frame_timing[ACTIVE_DISPLAY_V])
1468   {
1469      for (x = 0; x < 256; x++)
1470      {
1471         blitline_buffer[x] = m_current_palette[BACKDROP_COLOR];
1472      }
1473   }
1474
1475   if (m_gg_sms_mode)
1476   {
1477      int *combineline_buffer = m_line_buffer + ((line & 0x03) + 1) * 256;
12581478      int plot_x = 48;
12591479
12601480      /* Do horizontal scaling */
r13435r13436
13091529         pixel_plot_y = SMS_TBORDER_START + SMS_NTSC_192_TBORDER_Y_PIXELS + 24 + gg_line;
13101530
13111531         /* Setup our source lines */
1312         line1 = smsvdp->m_line_buffer + (((my_line - 3) & 0x03) + 1) * 256;
1313         line2 = smsvdp->m_line_buffer + (((my_line - 2) & 0x03) + 1) * 256;
1314         line3 = smsvdp->m_line_buffer + (((my_line - 1) & 0x03) + 1) * 256;
1315         line4 = smsvdp->m_line_buffer + (((my_line - 0) & 0x03) + 1) * 256;
1532         line1 = m_line_buffer + (((my_line - 3) & 0x03) + 1) * 256;
1533         line2 = m_line_buffer + (((my_line - 2) & 0x03) + 1) * 256;
1534         line3 = m_line_buffer + (((my_line - 1) & 0x03) + 1) * 256;
1535         line4 = m_line_buffer + (((my_line - 0) & 0x03) + 1) * 256;
13161536
13171537         for (x = 0 + 48; x < 160 + 48; x++)
13181538         {
1319            rgb_t   c1 = machine.pens[line1[x]];
1320            rgb_t   c2 = machine.pens[line2[x]];
1321            rgb_t   c3 = machine.pens[line3[x]];
1322            rgb_t   c4 = machine.pens[line4[x]];
1323            *BITMAP_ADDR32(bitmap, pixel_plot_y, pixel_offset_x + x) =
1539            rgb_t   c1 = machine().pens[line1[x]];
1540            rgb_t   c2 = machine().pens[line2[x]];
1541            rgb_t   c3 = machine().pens[line3[x]];
1542            rgb_t   c4 = machine().pens[line4[x]];
1543            *BITMAP_ADDR32(m_tmpbitmap, pixel_plot_y, pixel_offset_x + x) =
13241544               MAKE_RGB((RGB_RED(c1) / 6 + RGB_RED(c2) / 3 + RGB_RED(c3) / 3 + RGB_RED(c4) / 6 ),
13251545                  (RGB_GREEN(c1) / 6 + RGB_GREEN(c2) / 3 + RGB_GREEN(c3) / 3 + RGB_GREEN(c4) / 6 ),
13261546                  (RGB_BLUE(c1) / 6 + RGB_BLUE(c2) / 3 + RGB_BLUE(c3) / 3 + RGB_BLUE(c4) / 6 ) );
r13435r13436
13321552
13331553   for (x = 0; x < 256; x++)
13341554   {
1335      *BITMAP_ADDR32(bitmap, pixel_plot_y + line, pixel_offset_x + x) = machine.pens[blitline_buffer[x]];
1555      *BITMAP_ADDR32(m_tmpbitmap, pixel_plot_y + line, pixel_offset_x + x) = machine().pens[blitline_buffer[x]];
13361556   }
13371557}
13381558
1339
13401559// This is only used by Light Phaser. Should it be moved elsewhere?
1341int sms_vdp_check_brightness(device_t *device, int x, int y)
1560int sega315_5124_device::check_brightness(int x, int y)
13421561{
13431562   /* brightness of the lightgray color in the frame drawn by Light Phaser games */
13441563   const UINT8 sensor_min_brightness = 0x7f;
r13435r13436
13481567   if (x < SMS_LBORDER_START + SMS_LBORDER_X_PIXELS || x >= SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256)
13491568      return 0;
13501569
1351   smsvdp_t *smsvdp = get_safe_token(device);
1352   rgb_t color = *BITMAP_ADDR32(smsvdp->m_tmpbitmap, y, x);
1570   rgb_t color = *BITMAP_ADDR32(m_tmpbitmap, y, x);
13531571
13541572   /* reference: http://www.w3.org/TR/AERT#color-contrast */
13551573   UINT8 brightness = (RGB_RED(color) * 0.299) + (RGB_GREEN(color) * 0.587) + (RGB_BLUE(color) * 0.114);
r13435r13436
13591577}
13601578
13611579
1362static void sms_update_palette( smsvdp_t *smsvdp )
1580void sega315_5124_device::update_palette()
13631581{
13641582   int i;
13651583
13661584   /* Exit if palette is has no changes */
1367   if (smsvdp->m_cram_dirty == 0)
1585   if (m_cram_dirty == 0)
13681586   {
13691587      return;
13701588   }
1371   smsvdp->m_cram_dirty = 0;
1589   m_cram_dirty = 0;
13721590
1373   if (smsvdp->m_vdp_mode != 4 && ! IS_GAMEGEAR_VDP)
1591   if (m_vdp_mode != 4)
13741592   {
13751593      for(i = 0; i < 16; i++)
13761594      {
1377         smsvdp->m_current_palette[i] = 64 + i;
1595         m_current_palette[i] = 64 + i;
13781596      }
13791597      return;
13801598   }
13811599
1382   if (IS_GAMEGEAR_VDP)
1600   for (i = 0; i < 32; i++)
13831601   {
1384      if (smsvdp->m_gg_sms_mode)
1602      m_current_palette[i] = m_CRAM->u8(i) & 0x3f;
1603   }
1604}
1605
1606
1607void sega315_5378_device::update_palette()
1608{
1609   int i;
1610
1611   /* Exit if palette is has no changes */
1612   if (m_cram_dirty == 0)
1613   {
1614      return;
1615   }
1616   m_cram_dirty = 0;
1617
1618   if (m_gg_sms_mode)
1619   {
1620      for (i = 0; i < 32; i++)
13851621      {
1386         for (i = 0; i < 32; i++)
1387         {
1388            smsvdp->m_current_palette[i] = ((smsvdp->m_CRAM->u8(i) & 0x30) << 6) | ((smsvdp->m_CRAM->u8(i) & 0x0c ) << 4) | ((smsvdp->m_CRAM->u8(i) & 0x03) << 2);
1389         }
1622         m_current_palette[i] = ((m_CRAM->u8(i) & 0x30) << 6) | ((m_CRAM->u8(i) & 0x0c ) << 4) | ((m_CRAM->u8(i) & 0x03) << 2);
13901623      }
1391      else
1392      {
1393         for (i = 0; i < 32; i++)
1394         {
1395            smsvdp->m_current_palette[i] = ((smsvdp->m_CRAM->u8(i * 2 + 1) << 8) | smsvdp->m_CRAM->u8(i * 2)) & 0x0fff;
1396         }
1397      }
13981624   }
13991625   else
14001626   {
14011627      for (i = 0; i < 32; i++)
14021628      {
1403         smsvdp->m_current_palette[i] = smsvdp->m_CRAM->u8(i) & 0x3f;
1629         m_current_palette[i] = ((m_CRAM->u8(i * 2 + 1) << 8) | m_CRAM->u8(i * 2)) & 0x0fff;
14041630      }
14051631   }
14061632}
14071633
14081634
1409UINT32 sms_vdp_update( device_t *device, bitmap_t *bitmap, const rectangle *cliprect )
1635void sega315_5124_device::update_video( bitmap_t *bitmap, const rectangle *cliprect )
14101636{
1411   smsvdp_t *smsvdp = get_safe_token(device);
1412   copybitmap(bitmap, smsvdp->m_tmpbitmap, 0, 0, 0, 0, cliprect);
1413   return 0;
1637   copybitmap(bitmap, m_tmpbitmap, 0, 0, 0, 0, cliprect);
14141638}
14151639
14161640
r13435r13436
14181642    DEVICE INTERFACE
14191643*****************************************************************************/
14201644
1421static DEVICE_START( smsvdp )
1645void sega315_5124_device::device_config_complete()
14221646{
1423   smsvdp_t *smsvdp = get_safe_token(device);
1424   const smsvdp_interface *intf = get_interface(device);
1647   const smsvdp_interface *intf = reinterpret_cast<const smsvdp_interface *>(static_config());
14251648
1426   smsvdp->m_screen = device->machine().device<screen_device>( intf->screen_tag );
1427   int width = smsvdp->m_screen->width();
1428   int height = smsvdp->m_screen->height();
1649   if ( intf != NULL )
1650   {
1651      *static_cast<smsvdp_interface *>(this) = *intf;
1652   }
1653   else
1654   {
1655      fatalerror("No smsvdp_interface supplied\n");
1656   }
1657}
14291658
1430   smsvdp->m_features = intf->model;
14311659
1660void sega315_5124_device::device_start()
1661{
1662   m_screen = machine().device<screen_device>( m_screen_tag );
1663   int width = m_screen->width();
1664   int height = m_screen->height();
1665
14321666   /* Resolve callbacks */
1433   smsvdp->m_int_callback.resolve( intf->int_callback, *device );
1434   smsvdp->m_pause_callback.resolve( intf->pause_callback, *device );
1667   m_cb_int.resolve( m_int_callback, *this );
1668   m_cb_pause.resolve( m_pause_callback, *this );
14351669
14361670   /* Allocate video RAM */
1437   smsvdp->m_VRAM = device->machine().region_alloc("vdp_vram", VRAM_SIZE, 1, ENDIANNESS_LITTLE);
1438   smsvdp->m_CRAM = device->machine().region_alloc("vdp_cram", MAX_CRAM_SIZE, 1, ENDIANNESS_LITTLE);
1439   smsvdp->m_line_buffer = auto_alloc_array(device->machine(), int, 256 * 5);
1671   astring tempstring;
1672   m_VRAM = machine().region_alloc(subtag(tempstring,"vdp_vram"), VRAM_SIZE, 1, ENDIANNESS_LITTLE);
1673   m_CRAM = machine().region_alloc(subtag(tempstring,"vdp_cram"), SEGA315_5378_CRAM_SIZE, 1, ENDIANNESS_LITTLE);
1674   m_line_buffer = auto_alloc_array(machine(), int, 256 * 5);
14401675
1441   smsvdp->m_collision_buffer = auto_alloc_array(device->machine(), UINT8, SMS_X_PIXELS);
1442   smsvdp->m_sms_frame_timing = sms_ntsc_192;
1676   m_collision_buffer = auto_alloc_array(machine(), UINT8, SMS_X_PIXELS);
1677   m_frame_timing = (m_is_pal) ? pal_192 : ntsc_192;
14431678
14441679   /* Make temp bitmap for rendering */
1445   smsvdp->m_tmpbitmap = auto_bitmap_alloc(device->machine(), width, height, BITMAP_FORMAT_INDEXED32);
1680   m_tmpbitmap = auto_bitmap_alloc(machine(), width, height, BITMAP_FORMAT_INDEXED32);
14461681
1447   smsvdp->m_smsvdp_display_timer = device->machine().scheduler().timer_alloc(FUNC(smsvdp_display_callback), smsvdp);
1448   smsvdp->m_smsvdp_display_timer->adjust(smsvdp->m_screen->time_until_pos(0, DISPLAY_CB_HPOS), 0, smsvdp->m_screen->scan_period());
1682   m_smsvdp_display_timer = timer_alloc(TIMER_LINE);
1683   m_smsvdp_display_timer->adjust(m_screen->time_until_pos(0, DISPLAY_CB_HPOS), 0, m_screen->scan_period());
1684   m_set_status_vint_timer = timer_alloc( TIMER_SET_STATUS_VINT );
1685   m_set_status_sprovr_timer = timer_alloc( TIMER_SET_STATUS_SPROVR );
1686   m_check_hint_timer = timer_alloc( TIMER_CHECK_HINT );
1687   m_check_vint_timer = timer_alloc( TIMER_CHECK_VINT );
14491688
1450   device->save_item(NAME(smsvdp->m_status));
1451   device->save_item(NAME(smsvdp->m_reg9copy));
1452   device->save_item(NAME(smsvdp->m_addrmode));
1453   device->save_item(NAME(smsvdp->m_addr));
1454   device->save_item(NAME(smsvdp->m_cram_mask));
1455   device->save_item(NAME(smsvdp->m_cram_dirty));
1456   device->save_item(NAME(smsvdp->m_pending));
1457   device->save_item(NAME(smsvdp->m_buffer));
1458   device->save_item(NAME(smsvdp->m_gg_sms_mode));
1459   device->save_item(NAME(smsvdp->m_irq_state));
1460   device->save_item(NAME(smsvdp->m_vdp_mode));
1461   device->save_item(NAME(smsvdp->m_y_pixels));
1462   device->save_item(NAME(smsvdp->m_line_counter));
1463   device->save_item(NAME(smsvdp->m_hcounter));
1464   device->save_item(NAME(smsvdp->m_reg));
1465   device->save_item(NAME(smsvdp->m_current_palette));
1466   device->save_pointer(NAME(smsvdp->m_line_buffer), 256 * 5);
1467   device->save_pointer(NAME(smsvdp->m_collision_buffer), SMS_X_PIXELS);
1468   device->save_item(NAME(*smsvdp->m_tmpbitmap));
1689   save_item(NAME(m_status));
1690   save_item(NAME(m_reg9copy));
1691   save_item(NAME(m_addrmode));
1692   save_item(NAME(m_addr));
1693   save_item(NAME(m_cram_mask));
1694   save_item(NAME(m_cram_dirty));
1695   save_item(NAME(m_pending));
1696   save_item(NAME(m_buffer));
1697   save_item(NAME(m_gg_sms_mode));
1698   save_item(NAME(m_irq_state));
1699   save_item(NAME(m_vdp_mode));
1700   save_item(NAME(m_y_pixels));
1701   save_item(NAME(m_line_counter));
1702   save_item(NAME(m_hcounter));
1703   save_item(NAME(m_reg));
1704   save_item(NAME(m_current_palette));
1705   save_pointer(NAME(m_line_buffer), 256 * 5);
1706   save_pointer(NAME(m_collision_buffer), SMS_X_PIXELS);
1707   save_item(NAME(*m_tmpbitmap));
14691708}
14701709
1471static DEVICE_RESET( smsvdp )
1710
1711void sega315_5124_device::device_reset()
14721712{
1473   smsvdp_t *smsvdp = get_safe_token(device);
14741713   int i;
14751714
14761715   /* Most register are 0x00 at power-up */
1477   for (i = 0; i < NUM_OF_REGISTER; i++)
1478      smsvdp->m_reg[i] = 0x00;
1716   for (i = 0; i < 16; i++)
1717      m_reg[i] = 0x00;
14791718
1480   smsvdp->m_reg[0x02] = 0x0e;
1481   smsvdp->m_reg[0x0a] = 0xff;
1719   m_reg[0x02] = 0x0e;
1720   m_reg[0x0a] = 0xff;
14821721
1483   smsvdp->m_status = 0;
1484   smsvdp->m_reg9copy = 0;
1485   smsvdp->m_addrmode = 0;
1486   smsvdp->m_addr = 0;
1487   smsvdp->m_gg_sms_mode = 0;
1488   smsvdp->m_cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->m_gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
1489   smsvdp->m_cram_dirty = 1;
1490   smsvdp->m_pending = 0;
1491   smsvdp->m_buffer = 0;
1492   smsvdp->m_irq_state = 0;
1493   smsvdp->m_line_counter = 0;
1494   smsvdp->m_hcounter = 0;
1722   m_status = 0;
1723   m_reg9copy = 0;
1724   m_addrmode = 0;
1725   m_addr = 0;
1726   m_gg_sms_mode = 0;
1727   m_cram_mask = m_cram_size - 1;
1728   m_cram_dirty = 1;
1729   m_pending = 0;
1730   m_buffer = 0;
1731   m_irq_state = 0;
1732   m_line_counter = 0;
1733   m_hcounter = 0;
14951734
14961735   for (i = 0; i < 0x20; i++)
1497      smsvdp->m_current_palette[i] = 0;
1736      m_current_palette[i] = 0;
14981737
1499   set_display_settings(device);
1738   set_display_settings();
15001739
15011740   /* Clear RAM */
1502   memset(smsvdp->m_VRAM->base(), 0, VRAM_SIZE);
1503   memset(smsvdp->m_CRAM->base(), 0, MAX_CRAM_SIZE);
1504   memset(smsvdp->m_line_buffer, 0, 256 * 5 * sizeof(int));
1741   memset(m_VRAM->base(), 0, VRAM_SIZE);
1742   memset(m_CRAM->base(), 0, SEGA315_5378_CRAM_SIZE);
1743   memset(m_line_buffer, 0, 256 * 5 * sizeof(int));
15051744}
15061745
1507DEVICE_GET_INFO( smsvdp )
1508{
1509   switch (state)
1510   {
1511      /* --- the following bits of info are returned as 64-bit signed integers --- */
1512      case DEVINFO_INT_TOKEN_BYTES:         info->i = sizeof(smsvdp_t);               break;
1513
1514      /* --- the following bits of info are returned as pointers to data or functions --- */
1515      case DEVINFO_FCT_START:               info->start = DEVICE_START_NAME(smsvdp);      break;
1516      case DEVINFO_FCT_STOP:               /* Nothing */                           break;
1517      case DEVINFO_FCT_RESET:               info->reset = DEVICE_RESET_NAME(smsvdp);      break;
1518
1519      /* --- the following bits of info are returned as NULL-terminated strings --- */
1520      case DEVINFO_STR_NAME:               strcpy(info->s, "Sega Master SYstem / Game Gear VDP");            break;
1521      case DEVINFO_STR_FAMILY:            strcpy(info->s, "Sega MS VDP");               break;
1522      case DEVINFO_STR_VERSION:            strcpy(info->s, "1.0");                     break;
1523      case DEVINFO_STR_SOURCE_FILE:         strcpy(info->s, __FILE__);                  break;
1524      case DEVINFO_STR_CREDITS:            strcpy(info->s, "Copyright MAME / MESS Team");         break;
1525   }
1526}
1527
1528DEFINE_LEGACY_DEVICE(SMSVDP, smsvdp);
src/mess/video/smsvdp.h
r13435r13436
4040//#define PAL_240_BBORDER_Y_PIXELS  (0x18)      /* 24 lines */
4141
4242
43#define SEGA315_5124_PALETTE_SIZE   (64+16)
44#define SEGA315_5378_PALETTE_SIZE   4096
45
46PALETTE_INIT( sega315_5124 );
47PALETTE_INIT( sega315_5378 );
48
49
4350/***************************************************************************
4451    TYPE DEFINITIONS
4552***************************************************************************/
r13435r13436
4754typedef struct _smsvdp_interface smsvdp_interface;
4855struct _smsvdp_interface
4956{
50   UINT32             model;                /* Select model/features for the emulation */
51   const char         *screen_tag;
52   devcb_write_line   int_callback;         /* Interrupt callback function */
53   devcb_write_line   pause_callback;       /* Pause callback function */
57   bool               m_is_pal;             /* false = NTSC, true = PAL */
58   const char         *m_screen_tag;
59   devcb_write_line   m_int_callback;       /* Interrupt callback function */
60   devcb_write_line   m_pause_callback;     /* Pause callback function */
5461};
5562
56/***************************************************************************
57    DEVICE CONFIGURATION MACROS
58***************************************************************************/
5963
60DECLARE_LEGACY_DEVICE(SMSVDP, smsvdp);
64extern const device_type SEGA315_5124;      /* SMS1 vdp */
65extern const device_type SEGA315_5246;      /* SMS2 vdp */
66extern const device_type SEGA315_5378;      /* Gamegear vdp */
6167
62#define MCFG_SMSVDP_ADD(_tag, _interface) \
63   MCFG_DEVICE_ADD(_tag, SMSVDP, 0) \
64   MCFG_DEVICE_CONFIG(_interface)
6568
69class sega315_5124_device : public device_t,
70                            public smsvdp_interface
71{
72public:
73   // construction/destruction
74   sega315_5124_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
75   sega315_5124_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock, UINT8 cram_size, UINT8 palette_offset);
6676
77   DECLARE_READ8_MEMBER( vram_read );
78   DECLARE_WRITE8_MEMBER( vram_write );
79   DECLARE_READ8_MEMBER( register_read );
80   DECLARE_WRITE8_MEMBER( register_write );
81   DECLARE_READ8_MEMBER( vcount_read );
82   DECLARE_READ8_MEMBER( hcount_latch_read );
83   DECLARE_WRITE8_MEMBER( hcount_latch_write );
84
85   /* update the screen */
86   void update_video( bitmap_t *bitmap, const rectangle *cliprect );
87
88   int check_brightness( int x, int y );
89   virtual void set_gg_sms_mode( int mode ) { };
90
91protected:
92   virtual void set_display_settings();
93   virtual void update_palette();
94   virtual void refresh_line( int pixel_offset_x, int pixel_plot_y, int line );
95   virtual UINT16 get_name_table_address();
96   void process_line_timer();
97   void refresh_line_mode4( int *line_buffer, int *priority_selected, int line );
98   void refresh_mode4_sprites( int *line_buffer, int *priority_selected, int pixel_plot_y, int line );
99   void refresh_tms9918_sprites( int *line_buffer, int pixel_plot_y, int line );
100   void refresh_line_mode2( int *line_buffer, int line );
101   void refresh_line_mode0( int *line_buffer, int line );
102
103   // device-level overrides
104   virtual void device_config_complete();
105   virtual void device_start();
106   virtual void device_reset();
107   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
108
109   UINT8            m_reg[16];                  /* All the registers */
110   UINT8            m_status;                   /* Status register */
111   UINT8            m_reg9copy;                 /* Internal copy of register 9 */
112   UINT8            m_addrmode;                 /* Type of VDP action */
113   UINT16           m_addr;                     /* Contents of internal VDP address register */
114   UINT8            m_cram_size;                /* CRAM size */
115   UINT8            m_cram_mask;                /* Mask to switch between SMS and GG CRAM sizes */
116   int              m_cram_dirty;               /* Have there been any changes to the CRAM area */
117   int              m_pending;
118   UINT8            m_buffer;
119   int              m_gg_sms_mode;              /* Shrunk SMS screen on GG lcd mode flag */
120   int              m_irq_state;                /* The status of the IRQ line of the VDP */
121   int              m_vdp_mode;                 /* Current mode of the VDP: 0,1,2,3,4 */
122   int              m_y_pixels;                 /* 192, 224, 240 */
123   UINT8            m_line_counter;
124   UINT8            m_hcounter;
125   memory_region    *m_VRAM;                    /* Pointer to VRAM */
126   memory_region    *m_CRAM;                    /* Pointer to CRAM */
127   const UINT8      *m_frame_timing;
128   bitmap_t         *m_tmpbitmap;
129   UINT8            *m_collision_buffer;
130   UINT8            m_palette_offset;
131
132   /* line_buffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
133       Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
134       sms compatibility mode. */
135   int              *m_line_buffer;
136   int              m_current_palette[32];
137   devcb_resolved_write_line   m_cb_int;
138   devcb_resolved_write_line   m_cb_pause;
139   emu_timer        *m_smsvdp_display_timer;
140   emu_timer        *m_set_status_vint_timer;
141   emu_timer        *m_set_status_sprovr_timer;
142   emu_timer        *m_check_hint_timer;
143   emu_timer        *m_check_vint_timer;
144   screen_device    *m_screen;
145
146   /* Timers */
147   static const device_timer_id TIMER_LINE = 0;
148   static const device_timer_id TIMER_SET_STATUS_VINT = 1;
149   static const device_timer_id TIMER_SET_STATUS_SPROVR = 2;
150   static const device_timer_id TIMER_CHECK_HINT = 3;
151   static const device_timer_id TIMER_CHECK_VINT = 4;
152};
153
154
155class sega315_5246_device : public sega315_5124_device
156{
157public:
158   sega315_5246_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
159
160protected:
161   virtual void set_display_settings();
162   virtual UINT16 get_name_table_address();
163};
164
165
166class sega315_5378_device : public sega315_5124_device
167{
168public:
169   sega315_5378_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
170
171   virtual void set_gg_sms_mode( int mode );
172
173protected:
174   virtual void set_display_settings();
175   virtual void update_palette();
176   virtual void refresh_line( int pixel_offset_x, int pixel_plot_y, int line );
177   virtual UINT16 get_name_table_address();
178};
179
180
67181/***************************************************************************
68    DEVICE I/O FUNCTIONS
182    DEVICE CONFIGURATION MACROS
69183***************************************************************************/
70184
71/* prototypes */
185#define MCFG_SEGA315_5124_ADD(_tag, _interface) \
186   MCFG_DEVICE_ADD(_tag, SEGA315_5124, 0) \
187   MCFG_DEVICE_CONFIG(_interface)
72188
73UINT32 sms_vdp_update( device_t *device, bitmap_t *bitmap, const rectangle *cliprect );
74READ8_DEVICE_HANDLER( sms_vdp_vcount_r );
75READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r );
76WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w );
77READ8_DEVICE_HANDLER( sms_vdp_data_r );
78WRITE8_DEVICE_HANDLER( sms_vdp_data_w );
79READ8_DEVICE_HANDLER( sms_vdp_ctrl_r );
80WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w );
81void sms_vdp_set_ggsmsmode( device_t *device, int mode );
82int sms_vdp_check_brightness( device_t *device, int x, int y );
189#define MCFG_SEGA315_5246_ADD(_tag, _interface) \
190   MCFG_DEVICE_ADD(_tag, SEGA315_5246, 0) \
191   MCFG_DEVICE_CONFIG(_interface)
83192
193#define MCFG_SEGA315_5378_ADD(_tag, _interface) \
194   MCFG_DEVICE_ADD(_tag, SEGA315_5378, 0) \
195   MCFG_DEVICE_CONFIG(_interface)
196
84197#endif /* __SMSVDP_H__ */
src/mess/includes/sms.h
r13435r13436
2525   // device_ts
2626   device_t *m_main_cpu;
2727   device_t *m_control_cpu;
28   device_t *m_vdp;
28   sega315_5124_device *m_vdp;
2929   device_t *m_ym;
3030   device_t *m_main_scr;
3131   device_t *m_left_lcd;
3232   device_t *m_right_lcd;
33   address_space *m_space;
3334
3435   UINT8 m_bios_page_count;
3536   UINT8 m_fm_detect;
src/mess/drivers/sms.c
r13435r13436
6363#include "cpu/z80/z80.h"
6464#include "sound/sn76496.h"
6565#include "sound/2413intf.h"
66#include "includes/sms.h"
6766#include "video/smsvdp.h"
6867#include "imagedev/cartslot.h"
68#include "includes/sms.h"
6969
7070#include "sms1.lh"
7171
r13435r13436
114114   AM_RANGE(0x01, 0x01) AM_MIRROR(0x3e) AM_WRITE(sms_io_control_w)
115115   AM_RANGE(0x40, 0x7f)                 AM_READ(sms_count_r)
116116   AM_RANGE(0x40, 0x7f)                 AM_DEVWRITE("segapsg", sn76496_w)
117   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
118   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
117   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE_MODERN("sms_vdp", sega315_5124_device, vram_read, vram_write)
118   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE_MODERN("sms_vdp", sega315_5124_device, register_read, register_write)
119119   AM_RANGE(0xc0, 0xc0) AM_MIRROR(0x1e) AM_READ(sms_input_port_0_r)
120120   AM_RANGE(0xc1, 0xc1) AM_MIRROR(0x1e) AM_READ(sms_input_port_1_r)
121121   AM_RANGE(0xe0, 0xe0) AM_MIRROR(0x0e) AM_READ(sms_input_port_0_r)
r13435r13436
145145   AM_RANGE(0x21, 0x21) AM_MIRROR(0x1e) AM_WRITE(sms_io_control_w)
146146   AM_RANGE(0x40, 0x7f)                 AM_READ(sms_count_r)
147147   AM_RANGE(0x40, 0x7f)                 AM_DEVWRITE("gamegear", sn76496_w)
148   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
149   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
148   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE_MODERN("sms_vdp", sega315_5124_device, vram_read, vram_write)
149   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE_MODERN("sms_vdp", sega315_5124_device, register_read, register_write)
150150   AM_RANGE(0xc0, 0xc0)                 AM_READ_PORT("PORT_DC")
151151   AM_RANGE(0xc1, 0xc1)                 AM_READ_PORT("PORT_DD")
152152   AM_RANGE(0xdc, 0xdc)                 AM_READ_PORT("PORT_DC")
r13435r13436
302302   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_START ) PORT_NAME("Start") /* Game Gear START */
303303INPUT_PORTS_END
304304
305static PALETTE_INIT( sms )
306{
307   int i;
308   for (i = 0; i < 64; i++)
309   {
310      int r = i & 0x03;
311      int g = (i & 0x0c) >> 2;
312      int b = (i & 0x30) >> 4;
313      palette_set_color_rgb(machine, i, r << 6, g << 6, b << 6);
314   }
315   /* TMS9918 palette */
316   palette_set_color_rgb(machine, 64+ 0,   0,   0,   0);
317   palette_set_color_rgb(machine, 64+ 1,   0,   0,   0);
318   palette_set_color_rgb(machine, 64+ 2,  33, 200,  66);
319   palette_set_color_rgb(machine, 64+ 3,  94, 220, 120);
320   palette_set_color_rgb(machine, 64+ 4,  84,  85, 237);
321   palette_set_color_rgb(machine, 64+ 5, 125, 118, 252);
322   palette_set_color_rgb(machine, 64+ 6, 212,  82,  77);
323   palette_set_color_rgb(machine, 64+ 7,  66, 235, 245);
324   palette_set_color_rgb(machine, 64+ 8, 252,  85,  84);
325   palette_set_color_rgb(machine, 64+ 9, 255, 121, 120);
326   palette_set_color_rgb(machine, 64+10, 212, 193,  84);
327   palette_set_color_rgb(machine, 64+11, 230, 206, 128);
328   palette_set_color_rgb(machine, 64+12,  33, 176,  59);
329   palette_set_color_rgb(machine, 64+13, 201,  91, 186);
330   palette_set_color_rgb(machine, 64+14, 204, 204, 204);
331   palette_set_color_rgb(machine, 64+15, 255, 255, 255);
332}
333305
334static PALETTE_INIT( gamegear )
335{
336   int i;
337   for (i = 0; i < 4096; i++)
338   {
339      int r = i & 0x000f;
340      int g = (i & 0x00f0) >> 4;
341      int b = (i & 0x0f00) >> 8;
342      palette_set_color_rgb(machine, i, r << 4, g << 4, b << 4);
343   }
344}
345
346
347
348306static WRITE_LINE_DEVICE_HANDLER( sms_int_callback )
349307{
350308   cputag_set_input_line(device->machine(), "maincpu", 0, state);
351309}
352310
353static const smsvdp_interface _315_5124_intf =
311static const smsvdp_interface _315_5124_ntsc_intf =
354312{
355   MODEL_315_5124,
313   false,
356314   "screen",
357315   DEVCB_LINE(sms_int_callback),
358316   DEVCB_LINE(sms_pause_callback)
359317};
360318
361static const smsvdp_interface _315_5246_intf =
319static const smsvdp_interface _315_5124_pal_intf =
362320{
363   MODEL_315_5246,
321   true,
364322   "screen",
365323   DEVCB_LINE(sms_int_callback),
366324   DEVCB_LINE(sms_pause_callback)
367325};
368326
369static const smsvdp_interface _315_5378_intf =
370{
371   MODEL_315_5378,
372   "screen",
373   DEVCB_LINE(sms_int_callback),
374   DEVCB_LINE(sms_pause_callback)
375};
376
377327static const smsvdp_interface sms_store_intf =
378328{
379   MODEL_315_5124,
329   false,
380330   "screen",
381331   DEVCB_LINE(sms_store_int_callback),
382332   DEVCB_LINE(sms_pause_callback)
r13435r13436
430380   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS - 2, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256 + 10, SMS_NTSC_Y_PIXELS, SMS_TBORDER_START + SMS_NTSC_224_TBORDER_Y_PIXELS, SMS_TBORDER_START + SMS_NTSC_224_TBORDER_Y_PIXELS + 224)
431381   MCFG_SCREEN_UPDATE(sms)
432382
433   MCFG_PALETTE_LENGTH(64+16)
434   MCFG_PALETTE_INIT(sms)
383   MCFG_PALETTE_LENGTH(SEGA315_5124_PALETTE_SIZE)
384   MCFG_PALETTE_INIT(sega315_5124)
435385
436   MCFG_SMSVDP_ADD("sms_vdp", _315_5246_intf)
386   MCFG_SEGA315_5246_ADD("sms_vdp", _315_5124_ntsc_intf)
437387MACHINE_CONFIG_END
438388
439389static MACHINE_CONFIG_DERIVED( sms1_ntsc, sms_ntsc_base )
r13435r13436
460410
461411   MCFG_DEFAULT_LAYOUT(layout_sms1)
462412
463   MCFG_PALETTE_LENGTH(64+16)
464   MCFG_PALETTE_INIT(sms)
413   MCFG_PALETTE_LENGTH(SEGA315_5124_PALETTE_SIZE)
414   MCFG_PALETTE_INIT(sega315_5124)
465415
466416   MCFG_VIDEO_START(sms1)
467417
468   MCFG_SMSVDP_ADD("sms_vdp", _315_5124_intf)
418   MCFG_SEGA315_5124_ADD("sms_vdp", _315_5124_ntsc_intf)
469419MACHINE_CONFIG_END
470420
471421#define MCFG_SMSSDISP_CARTSLOT_ADD(_tag) \
r13435r13436
479429static MACHINE_CONFIG_DERIVED( sms_sdisp, sms2_ntsc )
480430
481431   MCFG_DEVICE_REMOVE("sms_vdp")
482   MCFG_SMSVDP_ADD("sms_vdp", sms_store_intf)
432   MCFG_SEGA315_5246_ADD("sms_vdp", sms_store_intf)
483433
484434   MCFG_CPU_ADD("control", Z80, XTAL_53_693175MHz/15)
485435   MCFG_CPU_PROGRAM_MAP(sms_store_mem)
r13435r13436
516466   MCFG_CPU_PROGRAM_MAP(sms_mem)
517467   MCFG_CPU_IO_MAP(sms_io)
518468
519   MCFG_QUANTUM_TIME(attotime::from_hz(60))
469   MCFG_QUANTUM_TIME(attotime::from_hz(50))
520470
521471   MCFG_MACHINE_START(sms)
522472   MCFG_MACHINE_RESET(sms)
r13435r13436
537487   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS - 2, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256 + 10, SMS_PAL_Y_PIXELS, SMS_TBORDER_START + SMS_PAL_240_TBORDER_Y_PIXELS, SMS_TBORDER_START + SMS_PAL_240_TBORDER_Y_PIXELS + 240)
538488   MCFG_SCREEN_UPDATE(sms)
539489
540   MCFG_PALETTE_LENGTH(64+16)
541   MCFG_PALETTE_INIT(sms)
490   MCFG_PALETTE_LENGTH(SEGA315_5124_PALETTE_SIZE)
491   MCFG_PALETTE_INIT(sega315_5124)
542492
543   MCFG_SMSVDP_ADD("sms_vdp", _315_5246_intf)
493   MCFG_SEGA315_5246_ADD("sms_vdp", _315_5124_pal_intf)
544494MACHINE_CONFIG_END
545495
546496static MACHINE_CONFIG_DERIVED( sms1_pal, sms_pal_base )
r13435r13436
565515   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS - 2, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 256 + 10, SMS_PAL_Y_PIXELS, SMS_TBORDER_START + SMS_PAL_240_TBORDER_Y_PIXELS, SMS_TBORDER_START + SMS_PAL_240_TBORDER_Y_PIXELS + 240)
566516   MCFG_SCREEN_UPDATE(sms1)
567517
568   MCFG_PALETTE_LENGTH(64+16)
569   MCFG_PALETTE_INIT(sms)
518   MCFG_PALETTE_LENGTH(SEGA315_5124_PALETTE_SIZE)
519   MCFG_PALETTE_INIT(sega315_5124)
570520
571521   MCFG_DEFAULT_LAYOUT(layout_sms1)
572522
573   MCFG_SMSVDP_ADD("sms_vdp", _315_5124_intf)
523   MCFG_SEGA315_5124_ADD("sms_vdp", _315_5124_pal_intf)
574524MACHINE_CONFIG_END
575525
576526static MACHINE_CONFIG_DERIVED( sms_fm, sms1_ntsc )
r13435r13436
611561   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 6*8, SMS_LBORDER_START + SMS_LBORDER_X_PIXELS + 26*8, SMS_NTSC_Y_PIXELS, SMS_TBORDER_START + SMS_NTSC_192_TBORDER_Y_PIXELS + 3*8, SMS_TBORDER_START + SMS_NTSC_192_TBORDER_Y_PIXELS + 21*8 )
612562   MCFG_SCREEN_UPDATE(gamegear)
613563
614   MCFG_PALETTE_LENGTH(4096)
615   MCFG_PALETTE_INIT(gamegear)
564   MCFG_PALETTE_LENGTH(SEGA315_5378_PALETTE_SIZE)
565   MCFG_PALETTE_INIT(sega315_5378)
616566
617567   MCFG_VIDEO_START(gamegear)
618568
619   MCFG_SMSVDP_ADD("sms_vdp", _315_5378_intf)
569   MCFG_SEGA315_5378_ADD("sms_vdp", _315_5124_ntsc_intf)
620570
621571   /* sound hardware */
622572   MCFG_SPEAKER_STANDARD_STEREO("lspeaker","rspeaker")

Previous 15994 Revisions Next


© 1998-2018 The MESS Team