Previous 15994 Revisions Next

r6079 Wednesday 14th October, 2009 at 20:39:36 UTC by Fabio Priuli
[SMS] Added Light Phaser support [Enik Land]
[SMS] Converted SMS VDP to be a device
[docs]messnew.txt
[src/mess/drivers]sms.c
[src/mess/includes]sms.h
[src/mess/machine]sms.c
[src/mess/video]smsvdp.c smsvdp.h

src/mess/video/smsvdp.c
r6078r6079
1/*********************************************************************
2
3    smsvdp.c
4
5    Implementation of video hardware chip used by Sega Master System
6
7**********************************************************************/
8
19/*
210    For more information, please see:
311    - http://cgfm2.emuviews.com/txt/msvdp.txt
r6078r6079
3442  - bottom border      48  C0-EF       32  E0-FF      24  F0-07
3543  - bottom blanking     3  F0-F2        3  00-02       3  08-0A
3644
45  TODO:
46    - implement differences between SMS1_VDP & SMS_VDP/GG_VDP
47
3748*/
3849
3950#include "driver.h"
4051#include "video/smsvdp.h"
41#include "includes/sms.h"
4252
53#define IS_SMS1_VDP           (smsvdp->features & MODEL_315_5124)
54#define IS_SMS2_VDP           (smsvdp->features & MODEL_315_5246)
55#define IS_GAMEGEAR_VDP       (smsvdp->features & MODEL_315_5378)
4356
44#define IS_SMS1_VDP         ( smsvdp.features & MODEL_315_5124 )
45#define IS_SMS2_VDP         ( smsvdp.features & MODEL_315_5246 )
46#define IS_GAMEGEAR_VDP      ( smsvdp.features & MODEL_315_5378 )
57#define STATUS_VINT           0x80   /* Pending vertical interrupt flag */
58#define STATUS_SPROVR         0x40   /* Sprite overflow flag */
59#define STATUS_SPRCOL         0x20   /* Object collision flag */
60#define STATUS_HINT           0x02   /* Pending horizontal interrupt flag */
4761
48#define STATUS_VINT         (0x80)   /* Pending vertical interrupt flag */
49#define STATUS_SPROVR      (0x40)   /* Sprite overflow flag */
50#define STATUS_SPRCOL      (0x20)   /* Object collision flag */
51#define STATUS_HINT         (0x02)   /* Pending horizontal interrupt flag */
62#define GG_CRAM_SIZE          0x40   /* 32 colors x 2 bytes per color = 64 bytes */
63#define SMS_CRAM_SIZE         0x20   /* 32 colors x 1 bytes per color = 32 bytes */
64#define MAX_CRAM_SIZE         0x40
5265
53#define GG_CRAM_SIZE      (0x40)   /* 32 colors x 2 bytes per color = 64 bytes */
54#define SMS_CRAM_SIZE      (0x20)   /* 32 colors x 1 bytes per color = 32 bytes */
55#define MAX_CRAM_SIZE      0x40
66#define VRAM_SIZE             0x4000
5667
57#define VRAM_SIZE         (0x4000)
68#define PRIORITY_BIT          0x1000
69#define BACKDROP_COLOR        ((smsvdp->vdp_mode == 4 ? 0x10 : 0x00) + (smsvdp->reg[0x07] & 0x0f))
5870
59#define PRIORITY_BIT      0x1000
60#define BACKDROP_COLOR      ( ( smsvdp.vdp_mode == 4 ? 0x10 : 0x00 ) + (smsvdp.reg[0x07] & 0x0F))
71#define NUM_OF_REGISTER       0x10  /* 16 registers */
6172
62#define NUM_OF_REGISTER      (0x10)  /* 16 registers */
73#define INIT_VCOUNT           0
74#define VERTICAL_BLANKING     1
75#define TOP_BLANKING          2
76#define TOP_BORDER            3
77#define ACTIVE_DISPLAY_V      4
78#define BOTTOM_BORDER         5
79#define BOTTOM_BLANKING       6
6380
64#define INIT_VCOUNT         0
65#define VERTICAL_BLANKING   1
66#define TOP_BLANKING      2
67#define TOP_BORDER         3
68#define ACTIVE_DISPLAY_V   4
69#define BOTTOM_BORDER      5
70#define BOTTOM_BLANKING      6
81static const UINT8 sms_ntsc_192[7] = { 0xd5, 3, 13, 27, 192, 24, 3 };
82static const UINT8 sms_ntsc_224[7] = { 0xe5, 3, 13, 11, 224,  8, 3 };
83static const UINT8 sms_ntsc_240[7] = { 0xee, 3, 13,  3, 240,  0, 3 };
84static const UINT8 sms_pal_192[7]  = { 0xba, 3, 13, 54, 192, 48, 3 };
85static const UINT8 sms_pal_224[7]  = { 0xca, 3, 13, 38, 224, 32, 3 };
86static const UINT8 sms_pal_240[7]  = { 0xd2, 3, 13, 30, 240, 24, 3 };
7187
72static const UINT8 sms_ntsc_192[7] = { 0xD5, 3, 13, 27, 192, 24, 3 };
73static const UINT8 sms_ntsc_224[7] = { 0xE5, 3, 13, 11, 224,  8, 3 };
74static const UINT8 sms_ntsc_240[7] = { 0xEE, 3, 13,  3, 240,  0, 3 };
75static const UINT8 sms_pal_192[7]  = { 0xBA, 3, 13, 54, 192, 48, 3 };
76static const UINT8 sms_pal_224[7]  = { 0xCA, 3, 13, 38, 224, 32, 3 };
77static const UINT8 sms_pal_240[7]  = { 0xD2, 3, 13, 30, 240, 24, 3 };
7888
79static struct _smsvdp
89typedef struct _smsvdp_t smsvdp_t;
90struct _smsvdp_t
8091{
81   UINT32      features;
82   UINT8      reg[NUM_OF_REGISTER];      /* All the registers */
83   UINT8      status;                  /* Status register */
84   UINT8      reg9copy;               /* Internal copy of register 9 */
85   UINT8      addrmode;               /* Type of VDP action */
86   UINT16      addr;                  /* Contents of internal VDP address register */
87   UINT8      cram_mask;               /* Mask to switch between SMS and GG CRAM sizes */
88   int         cram_dirty;               /* Have there been any changes to the CRAM area */
89   int         pending;
90   UINT8      buffer;
91   int         gg_sms_mode;            /* Shrunk SMS screen on GG lcd mode flag */
92   int         irq_state;               /* The status of the IRQ line of the VDP */
93   int         vdp_mode;               /* Current mode of the VDP: 0,1,2,3,4 */
94   int         y_pixels;               /* 192, 224, 240 */
95   int         line_counter;
96   UINT8      *VRAM;                  /* Pointer to VRAM */
97   UINT8      *CRAM;                  /* Pointer to CRAM */
98   const UINT8   *sms_frame_timing;
99   bitmap_t   *prev_bitmap;
100   int         prev_bitmap_saved;
101   UINT8      *collision_buffer;
92   UINT32           features;
93   UINT8            reg[NUM_OF_REGISTER];     /* All the registers */
94   UINT8            status;                   /* Status register */
95   UINT8            reg9copy;                 /* Internal copy of register 9 */
96   UINT8            addrmode;                 /* Type of VDP action */
97   UINT16           addr;                     /* Contents of internal VDP address register */
98   UINT8            cram_mask;                /* Mask to switch between SMS and GG CRAM sizes */
99   int              cram_dirty;               /* Have there been any changes to the CRAM area */
100   int              pending;
101   UINT8            buffer;
102   int              gg_sms_mode;              /* Shrunk SMS screen on GG lcd mode flag */
103   int              irq_state;                /* The status of the IRQ line of the VDP */
104   int              vdp_mode;                 /* Current mode of the VDP: 0,1,2,3,4 */
105   int              y_pixels;                 /* 192, 224, 240 */
106   int              line_counter;
107   UINT8            hcounter;
108   UINT8            *VRAM;                    /* Pointer to VRAM */
109   UINT8            *CRAM;                    /* Pointer to CRAM */
110   const UINT8      *sms_frame_timing;
111   bitmap_t         *prev_bitmap;
112   bitmap_t         *tmpbitmap;
113   int              prev_bitmap_saved;
114   UINT8            *collision_buffer;
102115
103   /* lineBuffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
116   /* line_buffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
104117       Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
105       sms compatibility mode.
106     */
107   int         *line_buffer;
108   int         current_palette[32];
109   void (*int_callback)(running_machine*,int);
110   emu_timer   *smsvdp_display_timer;
111} smsvdp;
118       sms compatibility mode. */
119   int              *line_buffer;
120   int              current_palette[32];
121   smsvdp_int_cb    int_callback;
122   smsvdp_pause_cb  pause_callback;
123   emu_timer        *smsvdp_display_timer;
124};
112125
113static TIMER_CALLBACK(smsvdp_display_callback);
114static void sms_refresh_line(running_machine *machine, bitmap_t *bitmap, int offsetx, int offsety, int line);
115static void sms_update_palette(void);
126static TIMER_CALLBACK( smsvdp_display_callback );
127static void sms_refresh_line( running_machine *machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int offsetx, int offsety, int line );
128static void sms_update_palette( smsvdp_t *smsvdp );
116129
117static void set_display_settings( running_machine *machine )
130
131/*****************************************************************************
132    INLINE FUNCTIONS
133*****************************************************************************/
134
135INLINE smsvdp_t *get_safe_token(const device_config *device)
118136{
119   const device_config *screen = video_screen_first(machine->config);
137   assert(device != NULL);
138   assert(device->token != NULL);
139   assert(device->type == SMSVDP);
140
141   return (smsvdp_t *)device->token;
142}
143
144INLINE const smsvdp_interface *get_interface(const device_config *device)
145{
146   assert(device != NULL);
147   assert((device->type == SMSVDP));
148   return (const smsvdp_interface *) device->static_config;
149}
150
151/*************************************
152 *
153 *  Utilities
154 *
155 *************************************/
156
157static void set_display_settings( const device_config *device )
158{
159   smsvdp_t *smsvdp = get_safe_token(device);
160
161   const device_config *screen = video_screen_first(device->machine->config);
120162   int height = video_screen_get_height(screen);
121163   int M1, M2, M3, M4;
122   M1 = smsvdp.reg[0x01] & 0x10;
123   M2 = smsvdp.reg[0x00] & 0x02;
124   M3 = smsvdp.reg[0x01] & 0x08;
125   M4 = smsvdp.reg[0x00] & 0x04;
126   smsvdp.y_pixels = 192;
127   if ( M4 )
164   M1 = smsvdp->reg[0x01] & 0x10;
165   M2 = smsvdp->reg[0x00] & 0x02;
166   M3 = smsvdp->reg[0x01] & 0x08;
167   M4 = smsvdp->reg[0x00] & 0x04;
168   smsvdp->y_pixels = 192;
169   if (M4)
128170   {
129171      /* mode 4 */
130      smsvdp.vdp_mode = 4;
131      if ( M2 && ( IS_SMS2_VDP || IS_GAMEGEAR_VDP ) )
172      smsvdp->vdp_mode = 4;
173      if (M2 && (IS_SMS2_VDP || IS_GAMEGEAR_VDP))
132174      {
133         /* Is it 224-line display */
134         if ( M1 && ! M3 )
135         {
136            smsvdp.y_pixels = 224;
137         }
138         else if ( ! M1 && M3 )
139         {
140            /* 240-line display */
141            smsvdp.y_pixels = 240;
142         }
175         if (M1 && !M3)
176            smsvdp->y_pixels = 224;   /* 224-line display */
177         else if (!M1 && M3)
178            smsvdp->y_pixels = 240;   /* 240-line display */
143179      }
144180   }
145181   else
146182   {
147183      /* original TMS9918 mode */
148      if ( ! M1 && ! M2 && ! M3 )
184      if (!M1 && !M2 && !M3)
149185      {
150         smsvdp.vdp_mode = 0;
186         smsvdp->vdp_mode = 0;
151187      }
152188      else
153//      if ( M1 && ! M2 && ! M3 )
189//      if (M1 && !M2 && !M3)
154190//      {
155//          smsvdp.vdp_mode = 1;
191//          smsvdp->vdp_mode = 1;
156192//      }
157193//      else
158      if ( ! M1 && M2 && ! M3 )
194      if (!M1 && M2 && !M3)
159195      {
160         smsvdp.vdp_mode = 2;
196         smsvdp->vdp_mode = 2;
161197//      }
162198//      else
163//      if ( ! M1 && ! M2 && M3 )
199//      if (!M1 && !M2 && M3)
164200//      {
165//          smsvdp.vdp_mode = 3;
201//          smsvdp->vdp_mode = 3;
166202      }
167203      else
168204      {
169         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');
205         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');
170206      }
171207   }
172208
173   switch( smsvdp.y_pixels )
209   switch (smsvdp->y_pixels)
174210   {
175211   case 192:
176      smsvdp.sms_frame_timing = ( height == PAL_Y_PIXELS ) ? sms_pal_192 : sms_ntsc_192;
212      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_192 : sms_ntsc_192;
177213      break;
178214
179215   case 224:
180      smsvdp.sms_frame_timing = ( height == PAL_Y_PIXELS ) ? sms_pal_224 : sms_ntsc_224;
216      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_224 : sms_ntsc_224;
181217      break;
182218
183219   case 240:
184      smsvdp.sms_frame_timing = ( height == PAL_Y_PIXELS ) ? sms_pal_240 : sms_ntsc_240;
220      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_240 : sms_ntsc_240;
185221      break;
186222   }
187   smsvdp.cram_dirty = 1;
223   smsvdp->cram_dirty = 1;
188224}
189225
190226
191 READ8_HANDLER(sms_vdp_vcount_r)
227READ8_DEVICE_HANDLER( sms_vdp_vcount_r )
192228{
193   return ( smsvdp.sms_frame_timing[ INIT_VCOUNT ] + video_screen_get_vpos(space->machine->primary_screen) ) & 0xFF;
229   smsvdp_t *smsvdp = get_safe_token(device);
230
231   return (smsvdp->sms_frame_timing[INIT_VCOUNT] + video_screen_get_vpos(device->machine->primary_screen)) & 0xff;
194232}
195233
196234
197READ8_HANDLER(sms_vdp_hcount_r)
235READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r )
198236{
199   return video_screen_get_hpos(space->machine->primary_screen) >> 1;
237   smsvdp_t *smsvdp = get_safe_token(device);
238   
239   return smsvdp->hcounter;
200240}
201241
202
203void sms_set_ggsmsmode( int mode )
242WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w )
204243{
205   smsvdp.gg_sms_mode = mode;
244   smsvdp_t *smsvdp = get_safe_token(device);
245   
246   smsvdp->hcounter = data;
206247}
207248
208249
209int smsvdp_video_init( running_machine *machine, const smsvdp_configuration *config )
250void sms_set_ggsmsmode( const device_config *device, int mode )
210251{
211   const device_config *screen = video_screen_first(machine->config);
212   int width = video_screen_get_width(screen);
213   int height = video_screen_get_height(screen);
252   smsvdp_t *smsvdp = get_safe_token(device);
214253
215   memset( &smsvdp, 0, sizeof smsvdp );
254   smsvdp->gg_sms_mode = mode;
255   smsvdp->cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
256}
216257
217   smsvdp.features = config->model;
218   smsvdp.int_callback = config->int_callback;
219258
220   /* Allocate video RAM
221       In theory the driver could have a "gfx1" and/or "gfx2" memory region
222       of it's own. So this code could potentially cause a clash.
223    */
224   smsvdp.VRAM = memory_region_alloc( machine, "gfx1", VRAM_SIZE, ROM_REQUIRED );
225   smsvdp.CRAM = memory_region_alloc( machine, "gfx2", MAX_CRAM_SIZE, ROM_REQUIRED );
226   smsvdp.line_buffer = auto_alloc_array(machine, int, 256 * 5);
227   memset( smsvdp.line_buffer, 0, 256 * 5 * sizeof(int) );
259static TIMER_CALLBACK( smsvdp_set_irq )
260{
261   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
228262
229   /* Clear RAM */
230   smsvdp.cram_dirty = 1;
231   memset(smsvdp.VRAM, 0, VRAM_SIZE);
232   memset(smsvdp.CRAM, 0, MAX_CRAM_SIZE);
233   smsvdp.reg[0x02] = 0x0E;         /* power up default */
234   smsvdp.reg[0x0a] = 0xff;
263   smsvdp->irq_state = 1;
235264
236   smsvdp.cram_mask = ( IS_GAMEGEAR_VDP && ! smsvdp.gg_sms_mode ) ? ( GG_CRAM_SIZE - 1 ) : ( SMS_CRAM_SIZE - 1 );
237
238   smsvdp.collision_buffer = auto_alloc_array(machine, UINT8, SMS_X_PIXELS);
239
240   /* Make temp bitmap for rendering */
241   tmpbitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
242
243   smsvdp.prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
244
245   set_display_settings( machine );
246
247   smsvdp.smsvdp_display_timer = timer_alloc(machine,  smsvdp_display_callback , NULL);
248   timer_adjust_periodic(smsvdp.smsvdp_display_timer, video_screen_get_time_until_pos(machine->primary_screen, 0, 0 ), 0, video_screen_get_scan_period( machine->primary_screen ));
249   return 0;
265   if (smsvdp->int_callback)
266      smsvdp->int_callback(machine, ASSERT_LINE);
250267}
251268
252269
253static TIMER_CALLBACK(smsvdp_set_irq)
270static TIMER_CALLBACK( smsvdp_display_callback )
254271{
255   smsvdp.irq_state = 1;
256   if ( smsvdp.int_callback )
257   {
258      smsvdp.int_callback( machine, ASSERT_LINE );
259   }
260}
272   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
261273
262
263static TIMER_CALLBACK(smsvdp_display_callback)
264{
265274   rectangle rec;
266275   int vpos = video_screen_get_vpos(machine->primary_screen);
267   int vpos_limit = smsvdp.sms_frame_timing[VERTICAL_BLANKING] + smsvdp.sms_frame_timing[TOP_BLANKING]
268                  + smsvdp.sms_frame_timing[TOP_BORDER] + smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V]
269                  + smsvdp.sms_frame_timing[BOTTOM_BORDER] + smsvdp.sms_frame_timing[BOTTOM_BLANKING];
276   int vpos_limit = smsvdp->sms_frame_timing[VERTICAL_BLANKING] + smsvdp->sms_frame_timing[TOP_BLANKING]
277                  + smsvdp->sms_frame_timing[TOP_BORDER] + smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V]
278                  + smsvdp->sms_frame_timing[BOTTOM_BORDER] + smsvdp->sms_frame_timing[BOTTOM_BLANKING];
270279
271280   rec.min_y = rec.max_y = vpos;
272281
273282   /* Check if we're on the last line of a frame */
274   if ( vpos == vpos_limit - 1 )
283   if (vpos == vpos_limit - 1)
275284   {
276      sms_check_pause_button( cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM) );
285      if (smsvdp->pause_callback)
286         smsvdp->pause_callback(machine);
287//      sms_check_pause_button(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM));
277288      return;
278289   }
279290
280   vpos_limit -= smsvdp.sms_frame_timing[BOTTOM_BLANKING];
291   vpos_limit -= smsvdp->sms_frame_timing[BOTTOM_BLANKING];
281292
282293   /* Check if we're below the bottom border */
283   if ( vpos >= vpos_limit )
294   if (vpos >= vpos_limit)
284295   {
285296      return;
286297   }
287298
288   vpos_limit -= smsvdp.sms_frame_timing[BOTTOM_BORDER];
299   vpos_limit -= smsvdp->sms_frame_timing[BOTTOM_BORDER];
289300
290301   /* Check if we're in the bottom border area */
291   if ( vpos >= vpos_limit )
302   if (vpos >= vpos_limit)
292303   {
293      if ( vpos == vpos_limit )
304      if (vpos == vpos_limit)
294305      {
295         if ( smsvdp.line_counter == 0x00 )
306         if (smsvdp->line_counter == 0x00)
296307         {
297            smsvdp.line_counter = smsvdp.reg[0x0A];
298            smsvdp.status |= STATUS_HINT;
308            smsvdp->line_counter = smsvdp->reg[0x0a];
309            smsvdp->status |= STATUS_HINT;
299310
300            if ( smsvdp.reg[0x00] & 0x10 )
311            if (smsvdp->reg[0x00] & 0x10)
301312            {
302313               /* Delay triggering of interrupt to allow software to read the status bit before the irq */
303               timer_set(machine, video_screen_get_time_until_pos(machine->primary_screen, video_screen_get_vpos(machine->primary_screen), video_screen_get_hpos(machine->primary_screen) + 1 ), NULL, 0, smsvdp_set_irq );
314               timer_set(machine, video_screen_get_time_until_pos(machine->primary_screen, video_screen_get_vpos(machine->primary_screen), video_screen_get_hpos(machine->primary_screen) + 1), smsvdp, 0, smsvdp_set_irq);
304315            }
305316         }
306317
307318      }
308319
309      if ( vpos == vpos_limit + 1 )
320      if (vpos == vpos_limit + 1)
310321      {
311         smsvdp.status |= STATUS_VINT;
322         smsvdp->status |= STATUS_VINT;
312323
313         if ( smsvdp.reg[0x01] & 0x20 )
324         if (smsvdp->reg[0x01] & 0x20)
314325         {
315326            /* Delay triggering of interrupt to allow software to read the status bit before the irq */
316            timer_set(machine, video_screen_get_time_until_pos(machine->primary_screen, video_screen_get_vpos(machine->primary_screen), video_screen_get_hpos(machine->primary_screen) + 1 ), NULL, 0, smsvdp_set_irq );
327            timer_set(machine, video_screen_get_time_until_pos(machine->primary_screen, video_screen_get_vpos(machine->primary_screen), video_screen_get_hpos(machine->primary_screen) + 1), smsvdp, 0, smsvdp_set_irq);
317328         }
318329      }
319330
320      sms_update_palette();
331      sms_update_palette(smsvdp);
321332
322333      /* Draw left border */
323334      rec.min_x = LBORDER_START;
324335      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
325      bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
336      bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
326337
327338      /* Draw right border */
328339      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
329340      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
330      bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
341      bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
331342
332343      /* Draw middle of the border */
333344      /* We need to do this through the regular drawing function so it will */
334345      /* be included in the gamegear scaling functions */
335      sms_refresh_line( machine, tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit - smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V], vpos - ( vpos_limit - smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V] ) );
346      sms_refresh_line(machine, smsvdp, smsvdp->tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit - smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V], vpos - (vpos_limit - smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V]));
336347      return;
337348   }
338349
339   vpos_limit -= smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V];
350   vpos_limit -= smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V];
340351
341352   /* Check if we're in the active display area */
342   if ( vpos >= vpos_limit )
353   if (vpos >= vpos_limit)
343354   {
344      if ( vpos == vpos_limit )
355      if (vpos == vpos_limit)
345356      {
346         smsvdp.line_counter = smsvdp.reg[0x0A];
347         smsvdp.reg9copy = smsvdp.reg[0x09];
357         smsvdp->line_counter = smsvdp->reg[0x0a];
358         smsvdp->reg9copy = smsvdp->reg[0x09];
348359      }
349360
350      if ( smsvdp.line_counter == 0x00 )
361      if (smsvdp->line_counter == 0x00)
351362      {
352         smsvdp.line_counter = smsvdp.reg[0x0A];
353         smsvdp.status |= STATUS_HINT;
363         smsvdp->line_counter = smsvdp->reg[0x0a];
364         smsvdp->status |= STATUS_HINT;
354365
355         if ( smsvdp.reg[0x00] & 0x10 )
366         if (smsvdp->reg[0x00] & 0x10)
356367         {
357368            /* Delay triggering of interrupt to allow software to read the status bit before the irq */
358            timer_set(machine, video_screen_get_time_until_pos(machine->primary_screen, video_screen_get_vpos(machine->primary_screen), video_screen_get_hpos(machine->primary_screen) + 1 ), NULL, 0, smsvdp_set_irq );
369            timer_set(machine, video_screen_get_time_until_pos(machine->primary_screen, video_screen_get_vpos(machine->primary_screen), video_screen_get_hpos(machine->primary_screen) + 1), smsvdp, 0, smsvdp_set_irq);
359370         }
360371      }
361372      else
362373      {
363         smsvdp.line_counter -= 1;
374         smsvdp->line_counter -= 1;
364375      }
365376
366      sms_update_palette();
377      sms_update_palette(smsvdp);
367378
368379      /* Check if display is disabled */
369      if ( ! ( smsvdp.reg[0x01] & 0x40 ) )
380      if (!(smsvdp->reg[0x01] & 0x40))
370381      {
371382         /* set whole line to backdrop color */
372383         rec.min_x = LBORDER_START;
373384         rec.max_x = LBORDER_START + LBORDER_X_PIXELS + 255 + RBORDER_X_PIXELS;
374         bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
385         bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
375386      }
376387      else
377388      {
378389         /* Draw left border */
379390         rec.min_x = LBORDER_START;
380391         rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
381         bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
392         bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
382393
383394         /* Draw right border */
384395         rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
385396         rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
386         bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
387         sms_refresh_line( machine, tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit );
397         bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
398         sms_refresh_line(machine, smsvdp, smsvdp->tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit);
388399      }
389400      return;
390401   }
391402
392   vpos_limit -= smsvdp.sms_frame_timing[TOP_BORDER];
403   vpos_limit -= smsvdp->sms_frame_timing[TOP_BORDER];
393404
394405   /* Check if we're in the top border area */
395   if ( vpos >= vpos_limit )
406   if (vpos >= vpos_limit)
396407   {
397      sms_update_palette();
408      sms_update_palette(smsvdp);
398409
399410      /* Draw left border */
400411      rec.min_x = LBORDER_START;
401412      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
402      bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
413      bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
403414
404415      /* Draw right border */
405416      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
406417      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
407      bitmap_fill( tmpbitmap, &rec , machine->pens[smsvdp.current_palette[BACKDROP_COLOR]]);
418      bitmap_fill(smsvdp->tmpbitmap, &rec, machine->pens[smsvdp->current_palette[BACKDROP_COLOR]]);
408419
409420      /* Draw middle of the border */
410421      /* We need to do this through the regular drawing function so it will */
411422      /* be included in the gamegear scaling functions */
412      sms_refresh_line( machine, tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit + smsvdp.sms_frame_timing[TOP_BORDER], vpos - ( vpos_limit + smsvdp.sms_frame_timing[TOP_BORDER] ) );
423      sms_refresh_line(machine, smsvdp, smsvdp->tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit + smsvdp->sms_frame_timing[TOP_BORDER], vpos - (vpos_limit + smsvdp->sms_frame_timing[TOP_BORDER]));
413424      return;
414425   }
415426}
416427
417428
418 READ8_HANDLER(sms_vdp_data_r)
429READ8_DEVICE_HANDLER( sms_vdp_data_r )
419430{
431   smsvdp_t *smsvdp = get_safe_token(device);
420432   UINT8 temp;
421433
422434   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
423435   /* to the address register when in the middle of doing a command.               */
424436   /* Cosmic Spacehead needs this, among others                                    */
425437   /* Clear pending write flag */
426   smsvdp.pending = 0;
438   smsvdp->pending = 0;
427439
428440   /* Return read buffer contents */
429   temp = smsvdp.buffer;
441   temp = smsvdp->buffer;
430442
431443   /* Load read buffer */
432   smsvdp.buffer = smsvdp.VRAM[(smsvdp.addr & 0x3FFF)];
444   smsvdp->buffer = smsvdp->VRAM[(smsvdp->addr & 0x3fff)];
433445
434446   /* Bump internal address register */
435   smsvdp.addr += 1;
436   return (temp);
447   smsvdp->addr += 1;
448   return temp;
437449}
438450
439451
440 READ8_HANDLER(sms_vdp_ctrl_r)
452READ8_DEVICE_HANDLER( sms_vdp_ctrl_r )
441453{
442   UINT8 temp = smsvdp.status;
454   smsvdp_t *smsvdp = get_safe_token(device);
443455
456   UINT8 temp = smsvdp->status;
457
444458   /* Clear pending write flag */
445   smsvdp.pending = 0;
459   smsvdp->pending = 0;
446460
447   smsvdp.status &= ~( STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT );
461   smsvdp->status &= ~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT);
448462
449   if (smsvdp.irq_state == 1)
463   if (smsvdp->irq_state == 1)
450464   {
451      smsvdp.irq_state = 0;
465      smsvdp->irq_state = 0;
452466
453      if ( smsvdp.int_callback )
454      {
455         smsvdp.int_callback( space->machine, CLEAR_LINE );
456      }
467      if (smsvdp->int_callback)
468         smsvdp->int_callback(device->machine, CLEAR_LINE);
457469   }
458
459   return (temp);
470   return temp;
460471}
461472
462473
463WRITE8_HANDLER(sms_vdp_data_w)
474WRITE8_DEVICE_HANDLER( sms_vdp_data_w )
464475{
476   smsvdp_t *smsvdp = get_safe_token(device);
477   int address;
478
465479   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
466480   /* to the address register when in the middle of doing a command.               */
467481   /* Cosmic Spacehead needs this, among others                                    */
468482   /* Clear pending write flag */
469   smsvdp.pending = 0;
483   smsvdp->pending = 0;
470484
471   switch(smsvdp.addrmode)
485   switch(smsvdp->addrmode)
472486   {
473487      case 0x00:
474488      case 0x01:
475489      case 0x02:
476         {
477            int address = (smsvdp.addr & 0x3FFF);
478
479            smsvdp.VRAM[address] = data;
480         }
490         address = (smsvdp->addr & 0x3fff);
491         smsvdp->VRAM[address] = data;
481492         break;
482493
483494      case 0x03:
495         address = smsvdp->addr & smsvdp->cram_mask;
496         if (data != smsvdp->CRAM[address])
484497         {
485            int address = smsvdp.addr & smsvdp.cram_mask;
486
487            if (data != smsvdp.CRAM[address])
488            {
489               smsvdp.CRAM[address] = data;
490               smsvdp.cram_dirty = 1;
491            }
498            smsvdp->CRAM[address] = data;
499            smsvdp->cram_dirty = 1;
492500         }
493501         break;
494502   }
495503
496   smsvdp.buffer = data;
497   smsvdp.addr += 1;
504   smsvdp->buffer = data;
505   smsvdp->addr += 1;
498506}
499507
500508
501WRITE8_HANDLER(sms_vdp_ctrl_w)
509WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w )
502510{
503   int regNum;
511   smsvdp_t *smsvdp = get_safe_token(device);
504512
505   if (smsvdp.pending == 0)
513   int reg_num;
514
515   if (smsvdp->pending == 0)
506516   {
507      smsvdp.addr = ( smsvdp.addr & 0xff00 ) | data;
508      smsvdp.pending = 1;
517      smsvdp->addr = (smsvdp->addr & 0xff00) | data;
518      smsvdp->pending = 1;
509519   }
510520   else
511521   {
512522      /* Clear pending write flag */
513      smsvdp.pending = 0;
523      smsvdp->pending = 0;
514524
515      smsvdp.addrmode = (data >> 6) & 0x03;
516      smsvdp.addr = ( data << 8 ) | ( smsvdp.addr & 0xff );
517      switch( smsvdp.addrmode )
525      smsvdp->addrmode = (data >> 6) & 0x03;
526      smsvdp->addr = (data << 8) | (smsvdp->addr & 0xff);
527      switch (smsvdp->addrmode)
518528      {
519529      case 0:      /* VRAM reading mode */
520         smsvdp.buffer = smsvdp.VRAM[ smsvdp.addr & 0x3FFF ];
521         smsvdp.addr += 1;
530         smsvdp->buffer = smsvdp->VRAM[smsvdp->addr & 0x3fff];
531         smsvdp->addr += 1;
522532         break;
523533
524534      case 1:      /* VRAM writing mode */
525535         break;
526536
527537      case 2:      /* VDP register write */
528         regNum = data & 0x0F;
529         smsvdp.reg[regNum] = smsvdp.addr & 0xff;
530         if (regNum == 0 && smsvdp.addr & 0x02)
531         {
538         reg_num = data & 0x0f;
539         smsvdp->reg[reg_num] = smsvdp->addr & 0xff;
540         if (reg_num == 0 && smsvdp->addr & 0x02)
532541            logerror("overscan enabled.\n");
533         }
534         if ( regNum == 0 || regNum == 1 )
542
543         if (reg_num == 0 || reg_num == 1)
544            set_display_settings(device);
545
546         if ((reg_num == 1) && (smsvdp->reg[0x01] & 0x20) && (smsvdp->status & STATUS_VINT))
535547         {
536            set_display_settings( space->machine );
548            smsvdp->irq_state = 1;
549
550            if (smsvdp->int_callback)
551               smsvdp->int_callback(device->machine, ASSERT_LINE);
537552         }
538         if ( ( regNum == 1 ) && ( smsvdp.reg[0x01] & 0x20 ) && ( smsvdp.status & STATUS_VINT ) )
539         {
540            smsvdp.irq_state = 1;
541            smsvdp.int_callback( space->machine, ASSERT_LINE );
542         }
543         smsvdp.addrmode = 0;
553         smsvdp->addrmode = 0;
544554         break;
545555
546556      case 3:      /* CRAM writing mode */
r6078r6079
550560}
551561
552562
553static void sms_refresh_line_mode4(int *lineBuffer, int line)
563static void sms_refresh_line_mode4( smsvdp_t *smsvdp, int *line_buffer, int line )
554564{
555   int tileColumn;
556   int xScroll, yScroll, xScrollStartColumn;
557   int spriteIndex;
558   int pixelX, pixelPlotX, prioritySelected[256];
559   int spriteX, spriteY, spriteLine, spriteTileSelected, spriteHeight, spriteZoom;
560   int spriteBuffer[8], spriteBufferCount, spriteBufferIndex;
561   int bitPlane0, bitPlane1, bitPlane2, bitPlane3;
565   int tile_column;
566   int x_scroll, y_scroll, x_scroll_start_column;
567   int sprite_index;
568   int pixel_x, pixel_plot_x, priority_selected[256];
569   int sprite_x, sprite_y, sprite_line, sprite_tile_selected, sprite_height, sprite_zoom;
570   int sprite_buffer[8], sprite_buffer_count, sprite_buffer_index;
571   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
562572   int scroll_mod;
563   UINT16 nameTableAddress;
564   UINT8 *nameTable;
565   UINT8 *spriteTable = smsvdp.VRAM + ( (smsvdp.reg[0x05] << 7) & 0x3F00 );
573   UINT16 name_table_address;
574   UINT8 *name_table;
575   UINT8 *sprite_table = smsvdp->VRAM + ((smsvdp->reg[0x05] << 7) & 0x3f00);
566576
567   if ( smsvdp.y_pixels != 192 )
577   if (smsvdp->y_pixels != 192)
568578   {
569      nameTableAddress = ((smsvdp.reg[0x02] & 0x0C) << 10) | 0x0700;
579      name_table_address = ((smsvdp->reg[0x02] & 0x0c) << 10) | 0x0700;
570580      scroll_mod = 256;
571581   }
572582   else
573583   {
574      nameTableAddress = (smsvdp.reg[0x02] << 10) & 0x3800;
584      name_table_address = (smsvdp->reg[0x02] << 10) & 0x3800;
575585      scroll_mod = 224;
576586   }
577587
578   if ( IS_SMS1_VDP )
579   {
580      nameTableAddress = nameTableAddress & (((smsvdp.reg[0x02] & 0x01) << 10) | 0x3BFF);
581   }
588   if (IS_SMS1_VDP)
589      name_table_address = name_table_address & (((smsvdp->reg[0x02] & 0x01) << 10) | 0x3bff);
582590
583   /* if top 2 rows of screen not affected by horizontal scrolling, then xScroll = 0 */
584   /* else xScroll = reg[0x08]                                                       */
585   xScroll = (((smsvdp.reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - smsvdp.reg[0x08]);
591   /* if top 2 rows of screen not affected by horizontal scrolling, then x_scroll = 0 */
592   /* else x_scroll = reg[0x08]                                                       */
593   x_scroll = (((smsvdp->reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - smsvdp->reg[0x08]);
586594
587   xScrollStartColumn = (xScroll >> 3);          /* x starting column tile */
595   x_scroll_start_column = (x_scroll >> 3);          /* x starting column tile */
588596
589597   /* Draw background layer */
590   for (tileColumn = 0; tileColumn < 33; tileColumn++)
598   for (tile_column = 0; tile_column < 33; tile_column++)
591599   {
592      UINT16 tileData;
593      int tileSelected, palletteSelected, horizSelected, vertSelected, prioritySelect;
594      int tileLine;
600      UINT16 tile_data;
601      int tile_selected, palette_selected, horiz_selected, vert_selected, priority_select;
602      int tile_line;
595603
596604      /* Rightmost 8 columns for SMS (or 2 columns for GG) not affected by */
597605      /* vertical scrolling when bit 7 of reg[0x00] is set */
598      yScroll = ((smsvdp.reg[0x00] & 0x80) && (tileColumn > 23)) ? 0 : smsvdp.reg9copy;
606      y_scroll = ((smsvdp->reg[0x00] & 0x80) && (tile_column > 23)) ? 0 : smsvdp->reg9copy;
599607
600      nameTable = smsvdp.VRAM + nameTableAddress + ((((line + yScroll) % scroll_mod) >> 3) << 6);
608      name_table = smsvdp->VRAM + name_table_address + ((((line + y_scroll) % scroll_mod) >> 3) << 6);
601609
602      tileLine = ( ( tileColumn + xScrollStartColumn ) & 0x1F ) * 2;
603      tileData = nameTable[ tileLine ] | ( nameTable[ tileLine + 1 ] << 8 );
610      tile_line = ((tile_column + x_scroll_start_column) & 0x1f) * 2;
611      tile_data = name_table[tile_line] | (name_table[tile_line + 1] << 8);
604612
605      tileSelected = (tileData & 0x01FF);
606      prioritySelect = tileData & PRIORITY_BIT;
607      palletteSelected = (tileData >> 11) & 0x01;
608      vertSelected = (tileData >> 10) & 0x01;
609      horizSelected = (tileData >> 9) & 0x01;
613      tile_selected = (tile_data & 0x01ff);
614      priority_select = tile_data & PRIORITY_BIT;
615      palette_selected = (tile_data >> 11) & 0x01;
616      vert_selected = (tile_data >> 10) & 0x01;
617      horiz_selected = (tile_data >> 9) & 0x01;
610618
611      tileLine = line - ((0x07 - (yScroll & 0x07)) + 1);
612      if (vertSelected)
613      {
614         tileLine = 0x07 - tileLine;
615      }
616      bitPlane0 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x00];
617      bitPlane1 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x01];
618      bitPlane2 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x02];
619      bitPlane3 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x03];
619      tile_line = line - ((0x07 - (y_scroll & 0x07)) + 1);
620      if (vert_selected)
621         tile_line = 0x07 - tile_line;
620622
621      for (pixelX = 0; pixelX < 8 ; pixelX++)
623      bit_plane_0 = smsvdp->VRAM[((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x00];
624      bit_plane_1 = smsvdp->VRAM[((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x01];
625      bit_plane_2 = smsvdp->VRAM[((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x02];
626      bit_plane_3 = smsvdp->VRAM[((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x03];
627
628      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
622629      {
623         UINT8 penBit0, penBit1, penBit2, penBit3;
624         UINT8 penSelected;
630         UINT8 pen_bit_0, pen_bit_1, pen_bit_2, pen_bit_3;
631         UINT8 pen_selected;
625632
626         penBit0 = (bitPlane0 >> (7 - pixelX)) & 0x01;
627         penBit1 = (bitPlane1 >> (7 - pixelX)) & 0x01;
628         penBit2 = (bitPlane2 >> (7 - pixelX)) & 0x01;
629         penBit3 = (bitPlane3 >> (7 - pixelX)) & 0x01;
633         pen_bit_0 = (bit_plane_0 >> (7 - pixel_x)) & 0x01;
634         pen_bit_1 = (bit_plane_1 >> (7 - pixel_x)) & 0x01;
635         pen_bit_2 = (bit_plane_2 >> (7 - pixel_x)) & 0x01;
636         pen_bit_3 = (bit_plane_3 >> (7 - pixel_x)) & 0x01;
630637
631         penSelected = (penBit3 << 3 | penBit2 << 2 | penBit1 << 1 | penBit0);
632         if (palletteSelected)
633         {
634            penSelected |= 0x10;
635         }
638         pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0);
639         if (palette_selected)
640            pen_selected |= 0x10;
636641
637         if (!horizSelected)
642         
643         if (!horiz_selected)
638644         {
639            pixelPlotX = pixelX;
645            pixel_plot_x = pixel_x;
640646         }
641647         else
642648         {
643            pixelPlotX = 7 - pixelX;
649            pixel_plot_x = 7 - pixel_x;
644650         }
645         pixelPlotX = (0 - (xScroll & 0x07) + (tileColumn << 3) + pixelPlotX);
646         if (pixelPlotX >= 0 && pixelPlotX < 256)
651         pixel_plot_x = (0 - (x_scroll & 0x07) + (tile_column << 3) + pixel_plot_x);
652         if (pixel_plot_x >= 0 && pixel_plot_x < 256)
647653         {
648//              logerror("%x %x\n", pixelPlotX + pixelOffsetX, pixelPlotY);
649            lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
650            prioritySelected[pixelPlotX] = prioritySelect | ( penSelected & 0x0F );
654//              logerror("%x %x\n", pixel_plot_x + pixel_offset_x, pixel_plot_y);
655            line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
656            priority_selected[pixel_plot_x] = priority_select | (pen_selected & 0x0f);
651657         }
652658      }
653659   }
654660
655661   /* Draw sprite layer */
656   spriteHeight = (smsvdp.reg[0x01] & 0x02 ? 16 : 8);
657   spriteZoom = 1;
658   if (smsvdp.reg[0x01] & 0x01)
659   {
662   sprite_height = (smsvdp->reg[0x01] & 0x02 ? 16 : 8);
663   sprite_zoom = 1;
664
665   if (smsvdp->reg[0x01] & 0x01)
660666      /* sprite doubling */
661      spriteZoom = 2;
662   }
663   spriteBufferCount = 0;
664   for (spriteIndex = 0; (spriteIndex < 64) && (spriteTable[spriteIndex] != 0xD0 || smsvdp.y_pixels != 192) && (spriteBufferCount < 9); spriteIndex++)
667      sprite_zoom = 2;
668
669   sprite_buffer_count = 0;
670   for (sprite_index = 0; (sprite_index < 64) && (sprite_table[sprite_index] != 0xd0 || smsvdp->y_pixels != 192) && (sprite_buffer_count < 9); sprite_index++)
665671   {
666      spriteY = spriteTable[spriteIndex] + 1; /* sprite y position starts at line 1 */
667      if (spriteY > 240)
672      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
673
674      if (sprite_y > 240)
675         sprite_y -= 256; /* wrap from top if y position is > 240 */
676
677      if ((line >= sprite_y) && (line < (sprite_y + sprite_height * sprite_zoom)))
668678      {
669         spriteY -= 256; /* wrap from top if y position is > 240 */
670      }
671      if ((line >= spriteY) && (line < (spriteY + spriteHeight * spriteZoom)))
672      {
673         if (spriteBufferCount < 8)
679         if (sprite_buffer_count < 8)
674680         {
675            spriteBuffer[spriteBufferCount] = spriteIndex;
681            sprite_buffer[sprite_buffer_count] = sprite_index;
676682         }
677683         else
678684         {
679685            /* Too many sprites per line */
680            smsvdp.status |= STATUS_SPROVR;
686            smsvdp->status |= STATUS_SPROVR;
681687         }
682         spriteBufferCount++;
688         sprite_buffer_count++;
683689      }
684690   }
685   if ( spriteBufferCount > 8 )
691
692   if (sprite_buffer_count > 8)
693      sprite_buffer_count = 8;
694
695   memset(smsvdp->collision_buffer, 0, SMS_X_PIXELS);
696   sprite_buffer_count--;
697
698   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
686699   {
687      spriteBufferCount = 8;
688   }
689   memset(smsvdp.collision_buffer, 0, SMS_X_PIXELS);
690   spriteBufferCount--;
691   for (spriteBufferIndex = spriteBufferCount; spriteBufferIndex >= 0; spriteBufferIndex--)
692   {
693      spriteIndex = spriteBuffer[spriteBufferIndex];
694      spriteY = spriteTable[spriteIndex] + 1; /* sprite y position starts at line 1 */
695      if (spriteY > 240)
696      {
697         spriteY -= 256; /* wrap from top if y position is > 240 */
698      }
700      sprite_index = sprite_buffer[sprite_buffer_index];
701      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
699702
700      spriteX = spriteTable[0x80 + (spriteIndex << 1)];
701      if (smsvdp.reg[0x00] & 0x08)
702      {
703         spriteX -= 0x08;    /* sprite shift */
704      }
705      spriteTileSelected = spriteTable[0x81 + (spriteIndex << 1)];
706      if (smsvdp.reg[0x06] & 0x04)
707      {
708         spriteTileSelected += 256; /* pattern table select */
709      }
710      if (smsvdp.reg[0x01] & 0x02)
711      {
712         spriteTileSelected &= 0x01FE; /* force even index */
713      }
714      spriteLine = ( line - spriteY ) / spriteZoom;
703      if (sprite_y > 240)
704         sprite_y -= 256; /* wrap from top if y position is > 240 */
715705
716      if (spriteLine > 0x07)
717      {
718         spriteTileSelected += 1;
719      }
706      sprite_x = sprite_table[0x80 + (sprite_index << 1)];
720707
721      bitPlane0 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x00];
722      bitPlane1 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x01];
723      bitPlane2 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x02];
724      bitPlane3 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x03];
708      if (smsvdp->reg[0x00] & 0x08)
709         sprite_x -= 0x08;    /* sprite shift */
725710
726      for (pixelX = 0; pixelX < 8 ; pixelX++)
711      sprite_tile_selected = sprite_table[0x81 + (sprite_index << 1)];
712
713      if (smsvdp->reg[0x06] & 0x04)
714         sprite_tile_selected += 256; /* pattern table select */
715
716      if (smsvdp->reg[0x01] & 0x02)
717         sprite_tile_selected &= 0x01fe; /* force even index */
718
719      sprite_line = (line - sprite_y) / sprite_zoom;
720
721      if (sprite_line > 0x07)
722         sprite_tile_selected += 1;
723
724      bit_plane_0 = smsvdp->VRAM[((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x00];
725      bit_plane_1 = smsvdp->VRAM[((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x01];
726      bit_plane_2 = smsvdp->VRAM[((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x02];
727      bit_plane_3 = smsvdp->VRAM[((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x03];
728
729      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
727730      {
728         UINT8 penBit0, penBit1, penBit2, penBit3;
729         int penSelected;
731         UINT8 pen_bit_0, pen_bit_1, pen_bit_2, pen_bit_3;
732         int pen_selected;
730733
731         penBit0 = (bitPlane0 >> (7 - pixelX)) & 0x01;
732         penBit1 = (bitPlane1 >> (7 - pixelX)) & 0x01;
733         penBit2 = (bitPlane2 >> (7 - pixelX)) & 0x01;
734         penBit3 = (bitPlane3 >> (7 - pixelX)) & 0x01;
734         pen_bit_0 = (bit_plane_0 >> (7 - pixel_x)) & 0x01;
735         pen_bit_1 = (bit_plane_1 >> (7 - pixel_x)) & 0x01;
736         pen_bit_2 = (bit_plane_2 >> (7 - pixel_x)) & 0x01;
737         pen_bit_3 = (bit_plane_3 >> (7 - pixel_x)) & 0x01;
735738
736         penSelected = (penBit3 << 3 | penBit2 << 2 | penBit1 << 1 | penBit0) | 0x10;
739         pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0) | 0x10;
737740
738         if ( penSelected == 0x10 )      /* Transparent palette so skip draw */
741         if (pen_selected == 0x10)      /* Transparent palette so skip draw */
739742         {
740743            continue;
741744         }
742745
743         if (smsvdp.reg[0x01] & 0x01)
746         if (smsvdp->reg[0x01] & 0x01)
744747         {
745748            /* sprite doubling is enabled */
746            pixelPlotX = spriteX + (pixelX << 1);
749            pixel_plot_x = sprite_x + (pixel_x << 1);
747750
748751            /* check to prevent going outside of active display area */
749            if ( pixelPlotX < 0 || pixelPlotX > 255 )
752            if (pixel_plot_x < 0 || pixel_plot_x > 255)
750753            {
751754               continue;
752755            }
753756
754            if ( ! ( prioritySelected[pixelPlotX] & PRIORITY_BIT ) )
757            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
755758            {
756               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
757               lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
759               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
760               line_buffer[pixel_plot_x + 1] = smsvdp->current_palette[pen_selected];
758761            }
759762            else
760763            {
761               if ( prioritySelected[pixelPlotX] == PRIORITY_BIT )
764               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
762765               {
763                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
766                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
764767               }
765               if ( prioritySelected[pixelPlotX + 1] == PRIORITY_BIT )
768               if (priority_selected[pixel_plot_x + 1] == PRIORITY_BIT)
766769               {
767                  lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
770                  line_buffer[pixel_plot_x + 1] = smsvdp->current_palette[pen_selected];
768771               }
769772            }
770            if (smsvdp.collision_buffer[pixelPlotX] != 1)
773            if (smsvdp->collision_buffer[pixel_plot_x] != 1)
771774            {
772               smsvdp.collision_buffer[pixelPlotX] = 1;
775               smsvdp->collision_buffer[pixel_plot_x] = 1;
773776            }
774777            else
775778            {
776779               /* sprite collision occurred */
777               smsvdp.status |= STATUS_SPRCOL;
780               smsvdp->status |= STATUS_SPRCOL;
778781            }
779            if (smsvdp.collision_buffer[pixelPlotX + 1] != 1)
782            if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
780783            {
781               smsvdp.collision_buffer[pixelPlotX + 1] = 1;
784               smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
782785            }
783786            else
784787            {
785788               /* sprite collision occurred */
786               smsvdp.status |= STATUS_SPRCOL;
789               smsvdp->status |= STATUS_SPRCOL;
787790            }
788791         }
789792         else
790793         {
791            pixelPlotX = spriteX + pixelX;
794            pixel_plot_x = sprite_x + pixel_x;
792795
793796            /* check to prevent going outside of active display area */
794            if ( pixelPlotX < 0 || pixelPlotX > 255 )
797            if (pixel_plot_x < 0 || pixel_plot_x > 255)
795798            {
796799               continue;
797800            }
798801
799            if ( ! ( prioritySelected[pixelPlotX] & PRIORITY_BIT ) )
802            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
800803            {
801               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
804               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
802805            }
803806            else
804807            {
805               if ( prioritySelected[pixelPlotX] == PRIORITY_BIT )
808               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
806809               {
807                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
810                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
808811               }
809812            }
810            if (smsvdp.collision_buffer[pixelPlotX] != 1)
813            if (smsvdp->collision_buffer[pixel_plot_x] != 1)
811814            {
812               smsvdp.collision_buffer[pixelPlotX] = 1;
815               smsvdp->collision_buffer[pixel_plot_x] = 1;
813816            }
814817            else
815818            {
816819               /* sprite collision occurred */
817               smsvdp.status |= STATUS_SPRCOL;
820               smsvdp->status |= STATUS_SPRCOL;
818821            }
819822         }
820823      }
821824   }
822825
823826   /* Fill column 0 with overscan color from reg[0x07] */
824   if (smsvdp.reg[0x00] & 0x20)
827   if (smsvdp->reg[0x00] & 0x20)
825828   {
826      lineBuffer[0] = smsvdp.current_palette[BACKDROP_COLOR];
827      lineBuffer[1] = smsvdp.current_palette[BACKDROP_COLOR];
828      lineBuffer[2] = smsvdp.current_palette[BACKDROP_COLOR];
829      lineBuffer[3] = smsvdp.current_palette[BACKDROP_COLOR];
830      lineBuffer[4] = smsvdp.current_palette[BACKDROP_COLOR];
831      lineBuffer[5] = smsvdp.current_palette[BACKDROP_COLOR];
832      lineBuffer[6] = smsvdp.current_palette[BACKDROP_COLOR];
833      lineBuffer[7] = smsvdp.current_palette[BACKDROP_COLOR];
829      line_buffer[0] = smsvdp->current_palette[BACKDROP_COLOR];
830      line_buffer[1] = smsvdp->current_palette[BACKDROP_COLOR];
831      line_buffer[2] = smsvdp->current_palette[BACKDROP_COLOR];
832      line_buffer[3] = smsvdp->current_palette[BACKDROP_COLOR];
833      line_buffer[4] = smsvdp->current_palette[BACKDROP_COLOR];
834      line_buffer[5] = smsvdp->current_palette[BACKDROP_COLOR];
835      line_buffer[6] = smsvdp->current_palette[BACKDROP_COLOR];
836      line_buffer[7] = smsvdp->current_palette[BACKDROP_COLOR];
834837   }
835838}
836839
837840
838static void sms_refresh_tms9918_sprites(int *lineBuffer, int line)
841static void sms_refresh_tms9918_sprites( smsvdp_t *smsvdp, int *line_buffer, int line )
839842{
840   int pixelPlotX;
841   int spriteHeight, spriteBufferCount, spriteIndex, spriteBuffer[4], spriteBufferIndex;
842   UINT8 *spriteTable, *spritePatternTable;
843   int pixel_plot_x;
844   int sprite_height, sprite_buffer_count, sprite_index, sprite_buffer[4], sprite_buffer_index;
845   UINT8 *sprite_table, *sprite_pattern_table;
843846
844847   /* Draw sprite layer */
845   spriteTable = smsvdp.VRAM + ( ( smsvdp.reg[0x05] & 0x7F ) << 7 );
846   spritePatternTable = smsvdp.VRAM + ( ( smsvdp.reg[0x06] & 0x07 ) << 11 );
847   spriteHeight = 8;
848   sprite_table = smsvdp->VRAM + ((smsvdp->reg[0x05] & 0x7f) << 7);
849   sprite_pattern_table = smsvdp->VRAM + ((smsvdp->reg[0x06] & 0x07) << 11);
850   sprite_height = 8;
848851
849   if ( smsvdp.reg[0x01] & 0x02 )                         /* Check if SI is set */
850      spriteHeight = spriteHeight * 2;
851   if ( smsvdp.reg[0x01] & 0x01 )                         /* Check if MAG is set */
852      spriteHeight = spriteHeight * 2;
852   if (smsvdp->reg[0x01] & 0x02)                         /* Check if SI is set */
853      sprite_height = sprite_height * 2;
854   if (smsvdp->reg[0x01] & 0x01)                         /* Check if MAG is set */
855      sprite_height = sprite_height * 2;
853856
854   spriteBufferCount = 0;
855   for ( spriteIndex = 0; (spriteIndex < 32*4 ) && ( spriteTable[spriteIndex] != 0xD0 ) && ( spriteBufferCount < 5); spriteIndex+= 4 )
857   sprite_buffer_count = 0;
858   for (sprite_index = 0; (sprite_index < 32 * 4) && (sprite_table[sprite_index] != 0xd0) && (sprite_buffer_count < 5); sprite_index += 4)
856859   {
857      int spriteY = spriteTable[spriteIndex] + 1;
860      int sprite_y = sprite_table[sprite_index] + 1;
858861
859      if ( spriteY > 240 )
860      {
861         spriteY -= 256;
862      }
862      if (sprite_y > 240)
863         sprite_y -= 256;
863864
864      if ( ( line >= spriteY ) && ( line < ( spriteY + spriteHeight ) ) )
865      if ((line >= sprite_y) && (line < (sprite_y + sprite_height)))
865866      {
866         if ( spriteBufferCount < 5 )
867         if (sprite_buffer_count < 5)
867868         {
868            spriteBuffer[spriteBufferCount] = spriteIndex;
869            sprite_buffer[sprite_buffer_count] = sprite_index;
869870         }
870871         else
871872         {
872873            /* Too many sprites per line */
873            smsvdp.status |= STATUS_SPROVR;
874            smsvdp->status |= STATUS_SPROVR;
874875         }
875         spriteBufferCount++;
876         sprite_buffer_count++;
876877      }
877878   }
878879
879   if ( spriteBufferCount > 4 )
880   {
881      spriteBufferCount = 4;
882   }
880   if (sprite_buffer_count > 4)
881      sprite_buffer_count = 4;
883882
884   memset( smsvdp.collision_buffer, 0, SMS_X_PIXELS );
885   spriteBufferCount--;
883   memset(smsvdp->collision_buffer, 0, SMS_X_PIXELS);
884   sprite_buffer_count--;
886885
887   for ( spriteBufferIndex = spriteBufferCount; spriteBufferIndex >= 0; spriteBufferIndex-- )
886   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
888887   {
889      int penSelected;
890      int spriteLine, pixelX, spriteX, spriteTileSelected;
891      int spriteY;
888      int pen_selected;
889      int sprite_line, pixel_x, sprite_x, sprite_tile_selected;
890      int sprite_y;
892891      UINT8 pattern;
893892
894      spriteIndex = spriteBuffer[ spriteBufferIndex ];
895      spriteY = spriteTable[ spriteIndex ] + 1;
893      sprite_index = sprite_buffer[sprite_buffer_index];
894      sprite_y = sprite_table[sprite_index] + 1;
896895
897      if ( spriteY > 240 )
898      {
899         spriteY -= 256;
900      }
896      if (sprite_y > 240)
897         sprite_y -= 256;
901898
902      spriteX = spriteTable[ spriteIndex + 1 ];
903      penSelected = spriteTable[ spriteIndex + 3 ] & 0x0F;
899      sprite_x = sprite_table[sprite_index + 1];
900      pen_selected = sprite_table[sprite_index + 3] & 0x0f;
904901
905      if ( IS_GAMEGEAR_VDP )
906      {
907         penSelected |= 0x10;
908      }
902      if (IS_GAMEGEAR_VDP)
903         pen_selected |= 0x10;
909904
910      if ( spriteTable[ spriteIndex + 3 ] & 0x80 )
911      {
912         spriteX -= 32;
913      }
905      if (sprite_table[sprite_index + 3] & 0x80)
906         sprite_x -= 32;
914907
915      spriteTileSelected = spriteTable[ spriteIndex + 2 ];
916      spriteLine = line - spriteY;
908      sprite_tile_selected = sprite_table[sprite_index + 2];
909      sprite_line = line - sprite_y;
917910
918      if ( smsvdp.reg[0x01] & 0x01 )
919      {
920         spriteLine >>= 1;
921      }
911      if (smsvdp->reg[0x01] & 0x01)
912         sprite_line >>= 1;
922913
923      if ( smsvdp.reg[0x01] & 0x02 )
914      if (smsvdp->reg[0x01] & 0x02)
924915      {
925         spriteTileSelected &= 0xFC;
916         sprite_tile_selected &= 0xfc;
926917
927         if ( spriteLine > 0x07 )
918         if (sprite_line > 0x07)
928919         {
929            spriteTileSelected += 1;
930            spriteLine -= 8;
920            sprite_tile_selected += 1;
921            sprite_line -= 8;
931922         }
932923      }
933924
934      pattern = spritePatternTable[ spriteTileSelected * 8 + spriteLine ];
925      pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
935926
936      for ( pixelX = 0; pixelX < 8; pixelX++ )
927      for (pixel_x = 0; pixel_x < 8; pixel_x++)
937928      {
938         if ( smsvdp.reg[0x01] & 0x01 )
929         if (smsvdp->reg[0x01] & 0x01)
939930         {
940            pixelPlotX = spriteX + pixelX * 2;
941            if ( pixelPlotX < 0 || pixelPlotX > 255 )
931            pixel_plot_x = sprite_x + pixel_x * 2;
932            if (pixel_plot_x < 0 || pixel_plot_x > 255)
942933            {
943934               continue;
944935            }
945936
946            if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) )
937            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
947938            {
948               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
939               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
949940
950               if ( smsvdp.collision_buffer[pixelPlotX] != 1 )
941               if (smsvdp->collision_buffer[pixel_plot_x] != 1)
951942               {
952                  smsvdp.collision_buffer[pixelPlotX] = 1;
943                  smsvdp->collision_buffer[pixel_plot_x] = 1;
953944               }
954945               else
955946               {
956                  smsvdp.status |= STATUS_SPRCOL;
947                  smsvdp->status |= STATUS_SPRCOL;
957948               }
958949
959               lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
950               line_buffer[pixel_plot_x+1] = smsvdp->current_palette[pen_selected];
960951
961               if ( smsvdp.collision_buffer[pixelPlotX + 1] != 1 )
952               if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
962953               {
963                  smsvdp.collision_buffer[pixelPlotX + 1] = 1;
954                  smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
964955               }
965956               else
966957               {
967                  smsvdp.status |= STATUS_SPRCOL;
958                  smsvdp->status |= STATUS_SPRCOL;
968959               }
969960            }
970961         }
971962         else
972963         {
973            pixelPlotX = spriteX + pixelX;
964            pixel_plot_x = sprite_x + pixel_x;
974965
975            if ( pixelPlotX < 0 || pixelPlotX > 255 )
966            if (pixel_plot_x < 0 || pixel_plot_x > 255)
976967            {
977968               continue;
978969            }
979970
980            if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) )
971            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
981972            {
982               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
973               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
983974
984               if ( smsvdp.collision_buffer[pixelPlotX] != 1 )
975               if (smsvdp->collision_buffer[pixel_plot_x] != 1)
985976               {
986                  smsvdp.collision_buffer[pixelPlotX] = 1;
977                  smsvdp->collision_buffer[pixel_plot_x] = 1;
987978               }
988979               else
989980               {
990                  smsvdp.status |= STATUS_SPRCOL;
981                  smsvdp->status |= STATUS_SPRCOL;
991982               }
992983            }
993984         }
994985      }
995986
996      if ( smsvdp.reg[0x01] & 0x02 )
987      if (smsvdp->reg[0x01] & 0x02)
997988      {
998         spriteTileSelected += 2;
999         pattern = spritePatternTable[ spriteTileSelected * 8 + spriteLine ];
1000         spriteX += ( smsvdp.reg[0x01] & 0x01 ? 16 : 8 );
989         sprite_tile_selected += 2;
990         pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
991         sprite_x += (smsvdp->reg[0x01] & 0x01 ? 16 : 8);
1001992
1002         for ( pixelX = 0; pixelX < 8; pixelX++ )
993         for (pixel_x = 0; pixel_x < 8; pixel_x++)
1003994         {
1004            if ( smsvdp.reg[0x01] & 0x01 )
995            if (smsvdp->reg[0x01] & 0x01)
1005996            {
1006               pixelPlotX = spriteX + pixelX * 2;
997               pixel_plot_x = sprite_x + pixel_x * 2;
1007998
1008               if ( pixelPlotX < 0 || pixelPlotX > 255 )
999               if (pixel_plot_x < 0 || pixel_plot_x > 255)
10091000               {
10101001                  continue;
10111002               }
10121003
1013               if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) )
1004               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
10141005               {
1015                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
1006                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
10161007
1017                  if ( smsvdp.collision_buffer[pixelPlotX] != 1 )
1008                  if (smsvdp->collision_buffer[pixel_plot_x] != 1)
10181009                  {
1019                     smsvdp.collision_buffer[pixelPlotX] = 1;
1010                     smsvdp->collision_buffer[pixel_plot_x] = 1;
10201011                  }
10211012                  else
10221013                  {
1023                     smsvdp.status |= STATUS_SPRCOL;
1014                     smsvdp->status |= STATUS_SPRCOL;
10241015                  }
10251016
1026                  lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
1017                  line_buffer[pixel_plot_x+1] = smsvdp->current_palette[pen_selected];
10271018
1028                  if ( smsvdp.collision_buffer[pixelPlotX + 1] != 1 )
1019                  if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
10291020                  {
1030                     smsvdp.collision_buffer[pixelPlotX + 1] = 1;
1021                     smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
10311022                  }
10321023                  else
10331024                  {
1034                     smsvdp.status |= STATUS_SPRCOL;
1025                     smsvdp->status |= STATUS_SPRCOL;
10351026                  }
10361027               }
10371028            }
10381029            else
10391030            {
1040               pixelPlotX = spriteX + pixelX;
1031               pixel_plot_x = sprite_x + pixel_x;
10411032
1042               if ( pixelPlotX < 0 || pixelPlotX > 255 )
1033               if (pixel_plot_x < 0 || pixel_plot_x > 255)
10431034               {
10441035                  continue;
10451036               }
10461037
1047               if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) )
1038               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
10481039               {
1049                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
1040                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
10501041
1051                  if ( smsvdp.collision_buffer[pixelPlotX] != 1 )
1042                  if (smsvdp->collision_buffer[pixel_plot_x] != 1)
10521043                  {
1053                     smsvdp.collision_buffer[pixelPlotX] = 1;
1044                     smsvdp->collision_buffer[pixel_plot_x] = 1;
10541045                  }
10551046                  else
10561047                  {
1057                     smsvdp.status |= STATUS_SPRCOL;
1048                     smsvdp->status |= STATUS_SPRCOL;
10581049                  }
10591050               }
10601051            }
r6078r6079
10641055}
10651056
10661057
1067static void sms_refresh_line_mode2(int *lineBuffer, int line)
1058static void sms_refresh_line_mode2( smsvdp_t *smsvdp, int *line_buffer, int line )
10681059{
1069   int tileColumn;
1070   int pixelX, pixelPlotX;
1071   UINT8 *nameTable, *colorTable, *patternTable;
1072   int patternMask, colorMask, patternOffset;
1060   int tile_column;
1061   int pixel_x, pixel_plot_x;
1062   UINT8 *name_table, *color_table, *pattern_table;
1063   int pattern_mask, color_mask, pattern_offset;
10731064
10741065   /* Draw background layer */
1075   nameTable = smsvdp.VRAM + ( ( smsvdp.reg[0x02] & 0x0F ) << 10 ) + ( ( line >> 3 ) * 32 );
1076   colorTable = smsvdp.VRAM + ( ( smsvdp.reg[0x03] & 0x80 ) << 6 );
1077   colorMask = ( ( smsvdp.reg[0x03] & 0x7F ) << 3 ) | 0x07;
1078   patternTable = smsvdp.VRAM + ( ( smsvdp.reg[0x04] & 0x04 ) << 11 );
1079   patternMask = ( ( smsvdp.reg[0x04] & 0x03 ) << 8 ) | 0xFF;
1080   patternOffset = ( line & 0xC0 ) << 2;
1066   name_table = smsvdp->VRAM + ((smsvdp->reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1067   color_table = smsvdp->VRAM + ((smsvdp->reg[0x03] & 0x80) << 6);
1068   color_mask = ((smsvdp->reg[0x03] & 0x7f) << 3) | 0x07;
1069   pattern_table = smsvdp->VRAM + ((smsvdp->reg[0x04] & 0x04) << 11);
1070   pattern_mask = ((smsvdp->reg[0x04] & 0x03) << 8) | 0xff;
1071   pattern_offset = (line & 0xc0) << 2;
10811072
1082   for ( tileColumn = 0; tileColumn < 32; tileColumn++ )
1073   for (tile_column = 0; tile_column < 32; tile_column++)
10831074   {
10841075      UINT8 pattern;
10851076      UINT8 colors;
10861077
1087      pattern = patternTable[ ( ( ( patternOffset + nameTable[tileColumn] ) & patternMask ) * 8 ) + ( line & 0x07 ) ];
1088      colors = colorTable[ ( ( ( patternOffset + nameTable[tileColumn] ) & colorMask ) * 8 ) + ( line & 0x07 ) ];
1078      pattern = pattern_table[(((pattern_offset + name_table[tile_column]) & pattern_mask) * 8) + (line & 0x07)];
1079      colors = color_table[(((pattern_offset + name_table[tile_column]) & color_mask) * 8) + (line & 0x07)];
10891080
1090      for ( pixelX = 0; pixelX < 8; pixelX++ )
1081      for (pixel_x = 0; pixel_x < 8; pixel_x++)
10911082      {
1092         UINT8 penSelected;
1083         UINT8 pen_selected;
10931084
1094         if ( pattern & ( 1 << ( 7 - pixelX ) ) )
1085         if (pattern & (1 << (7 - pixel_x)))
10951086         {
1096            penSelected = colors >> 4;
1087            pen_selected = colors >> 4;
10971088         }
10981089         else
10991090         {
1100            penSelected = colors & 0x0F;
1091            pen_selected = colors & 0x0f;
11011092         }
11021093
1103         if ( ! penSelected )
1104         {
1105            penSelected = BACKDROP_COLOR;
1106         }
1094         if (!pen_selected)
1095            pen_selected = BACKDROP_COLOR;
11071096
1108         pixelPlotX = ( tileColumn << 3 ) + pixelX;
1097         pixel_plot_x = (tile_column << 3) + pixel_x;
11091098
1110         if ( IS_GAMEGEAR_VDP )
1111         {
1112            penSelected |= 0x10;
1113         }
1099         if (IS_GAMEGEAR_VDP)
1100            pen_selected |= 0x10;
11141101
1115         lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
1102         line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
11161103      }
11171104   }
11181105
11191106   /* Draw sprite layer */
1120   sms_refresh_tms9918_sprites( lineBuffer, line );
1107   sms_refresh_tms9918_sprites(smsvdp, line_buffer, line);
11211108}
11221109
11231110
1124static void sms_refresh_line_mode0(int *lineBuffer, int line)
1111static void sms_refresh_line_mode0( smsvdp_t *smsvdp, int *line_buffer, int line)
11251112{
1126   int tileColumn;
1127   int pixelX, pixelPlotX;
1128   UINT8 *nameTable, *colorTable, *patternTable;
1113   int tile_column;
1114   int pixel_x, pixel_plot_x;
1115   UINT8 *name_table, *color_table, *pattern_table;
11291116
11301117   /* Draw background layer */
1131   nameTable = smsvdp.VRAM + ( ( smsvdp.reg[0x02] & 0x0F ) << 10 ) + ( ( line >> 3 ) * 32 );
1132   colorTable = smsvdp.VRAM + ( ( smsvdp.reg[0x03] << 6 ) & ( VRAM_SIZE - 1 ) );
1133   patternTable = smsvdp.VRAM + ( ( smsvdp.reg[0x04] << 11 ) & ( VRAM_SIZE - 1 ) );
1118   name_table = smsvdp->VRAM + ((smsvdp->reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1119   color_table = smsvdp->VRAM + ((smsvdp->reg[0x03] << 6) & (VRAM_SIZE - 1));
1120   pattern_table = smsvdp->VRAM + ((smsvdp->reg[0x04] << 11) & (VRAM_SIZE - 1));
11341121
1135   for ( tileColumn = 0; tileColumn < 32; tileColumn++ )
1122   for (tile_column = 0; tile_column < 32; tile_column++)
11361123   {
11371124      UINT8 pattern;
11381125      UINT8 colors;
11391126
1140      pattern = patternTable[ ( nameTable[tileColumn] * 8 ) + ( line & 0x07 ) ];
1141      colors = colorTable[ nameTable[tileColumn] >> 3 ];
1127      pattern = pattern_table[(name_table[tile_column] * 8) + (line & 0x07)];
1128      colors = color_table[name_table[tile_column] >> 3];
11421129
1143      for ( pixelX = 0; pixelX < 8; pixelX++ )
1130      for (pixel_x = 0; pixel_x < 8; pixel_x++)
11441131      {
1145         int penSelected;
1132         int pen_selected;
11461133
1147         if ( pattern & ( 1 << ( 7 - pixelX ) ) )
1148         {
1149            penSelected = colors >> 4;
1150         }
1134         if (pattern & (1 << (7 - pixel_x)))
1135            pen_selected = colors >> 4;
11511136         else
1152         {
1153            penSelected = colors & 0x0F;
1154         }
1137            pen_selected = colors & 0x0f;
11551138
1156         if ( IS_GAMEGEAR_VDP )
1157         {
1158            penSelected |= 0x10;
1159         }
1139         if (IS_GAMEGEAR_VDP)
1140            pen_selected |= 0x10;
11601141
1161         pixelPlotX = ( tileColumn << 3 ) + pixelX;
1162         lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
1142         pixel_plot_x = (tile_column << 3) + pixel_x;
1143         line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
11631144      }
11641145   }
11651146
11661147   /* Draw sprite layer */
1167   sms_refresh_tms9918_sprites( lineBuffer, line );
1148   sms_refresh_tms9918_sprites(smsvdp, line_buffer, line);
11681149}
11691150
11701151
1171static void sms_refresh_line( running_machine *machine, bitmap_t *bitmap, int pixelOffsetX, int pixelPlotY, int line )
1152static void sms_refresh_line( running_machine *machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int pixel_offset_x, int pixel_plot_y, int line )
11721153{
11731154   int x;
1174   int *blitLineBuffer = smsvdp.line_buffer;
1155   int *blitline_buffer = smsvdp->line_buffer;
11751156
1176   if ( line >= 0 && line < smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V] )
1157   if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
11771158   {
1178      switch( smsvdp.vdp_mode )
1159      switch( smsvdp->vdp_mode )
11791160      {
11801161      case 0:
1181         sms_refresh_line_mode0( blitLineBuffer, line );
1162         sms_refresh_line_mode0(smsvdp, blitline_buffer, line);
11821163         break;
11831164
11841165      case 2:
1185         sms_refresh_line_mode2( blitLineBuffer, line );
1166         sms_refresh_line_mode2(smsvdp, blitline_buffer, line);
11861167         break;
11871168
11881169      case 4:
11891170      default:
1190         sms_refresh_line_mode4( blitLineBuffer, line );
1171         sms_refresh_line_mode4(smsvdp, blitline_buffer, line);
11911172         break;
11921173      }
11931174   }
11941175   else
11951176   {
1196      for ( x = 0; x < 256; x++ )
1177      for (x = 0; x < 256; x++)
11971178      {
1198         blitLineBuffer[x] = smsvdp.current_palette[BACKDROP_COLOR];
1179         blitline_buffer[x] = smsvdp->current_palette[BACKDROP_COLOR];
11991180      }
12001181   }
12011182
1202   if ( IS_GAMEGEAR_VDP && smsvdp.gg_sms_mode )
1183   if (IS_GAMEGEAR_VDP && smsvdp->gg_sms_mode)
12031184   {
1204      int *combineLineBuffer = smsvdp.line_buffer + ( ( line & 0x03 ) + 1 ) * 256;
1205      int plotX = 48;
1185      int *combineline_buffer = smsvdp->line_buffer + ((line & 0x03) + 1) * 256;
1186      int plot_x = 48;
12061187
12071188      /* Do horizontal scaling */
1208      for( x = 8; x < 248; )
1189      for (x = 8; x < 248;)
12091190      {
1210         int   combined;
1191         int combined;
12111192
12121193         /* Take red and green from first pixel, and blue from second pixel */
1213         combined = ( blitLineBuffer[x] & 0x00FF ) | ( blitLineBuffer[x+1] & 0x0F00 );
1214         combineLineBuffer[plotX] = combined;
1194         combined = (blitline_buffer[x] & 0x00ff) | (blitline_buffer[x + 1] & 0x0f00);
1195         combineline_buffer[plot_x] = combined;
12151196
12161197         /* Take red from second pixel, and green and blue from third pixel */
1217         combined = ( blitLineBuffer[x+1] & 0x000F ) | ( blitLineBuffer[x+2] & 0x0FF0 );
1218         combineLineBuffer[plotX+1] = combined;
1198         combined = (blitline_buffer[x + 1] & 0x000f) | (blitline_buffer[x + 2] & 0x0ff0);
1199         combineline_buffer[plot_x + 1] = combined;
12191200         x += 3;
1220         plotX += 2;
1201         plot_x += 2;
12211202      }
12221203
12231204      /* Do vertical scaling for a screen with 192 or 224 lines
r6078r6079
12331214           GG_143 = 1/6 * SMS_217 + 1/3 * SMS_218 + 1/3 * SMS_219 + 1/6 * SMS_220
12341215        */
12351216      {
1236         int   ggLine;
1237         int   myLine = pixelPlotY + line - ( TBORDER_START + NTSC_224_TBORDER_Y_PIXELS );
1238         int   *line1, *line2, *line3, *line4;
1217         int gg_line;
1218         int my_line = pixel_plot_y + line - (TBORDER_START + NTSC_224_TBORDER_Y_PIXELS);
1219         int *line1, *line2, *line3, *line4;
12391220
12401221         /* First make sure there's enough data to draw anything */
12411222         /* We need one more line of data if we're on line 8, 11, 14, 17, etc */
1242         if ( myLine < 6 || myLine > 220 || ( ( myLine - 8 ) % 3 == 0 ) )
1223         if (my_line < 6 || my_line > 220 || ((my_line - 8) % 3 == 0))
12431224         {
12441225            return;
12451226         }
12461227
1247         ggLine = ( ( myLine - 6 ) / 3 ) * 2;
1228         gg_line = ((my_line - 6) / 3) * 2;
12481229
12491230         /* If we're on SMS line 7, 10, 13, etc we're on an odd GG line */
1250         if ( myLine % 3 )
1231         if (my_line % 3)
12511232         {
1252            ggLine++;
1233            gg_line++;
12531234         }
12541235
12551236         /* Calculate the line we will be drawing on */
1256         pixelPlotY = TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 24 + ggLine;
1237         pixel_plot_y = TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 24 + gg_line;
12571238
12581239         /* Setup our source lines */
1259         line1 = smsvdp.line_buffer + ( ( ( myLine - 3 ) & 0x03 ) + 1 ) * 256;
1260         line2 = smsvdp.line_buffer + ( ( ( myLine - 2 ) & 0x03 ) + 1 ) * 256;
1261         line3 = smsvdp.line_buffer + ( ( ( myLine - 1 ) & 0x03 ) + 1 ) * 256;
1262         line4 = smsvdp.line_buffer + ( ( ( myLine - 0 ) & 0x03 ) + 1 ) * 256;
1240         line1 = smsvdp->line_buffer + (((my_line - 3) & 0x03) + 1) * 256;
1241         line2 = smsvdp->line_buffer + (((my_line - 2) & 0x03) + 1) * 256;
1242         line3 = smsvdp->line_buffer + (((my_line - 1) & 0x03) + 1) * 256;
1243         line4 = smsvdp->line_buffer + (((my_line - 0) & 0x03) + 1) * 256;
12631244
1264         for( x = 0+48; x < 160+48; x++ )
1245         for (x = 0 + 48; x < 160 + 48; x++)
12651246         {
12661247            rgb_t   c1 = machine->pens[line1[x]];
12671248            rgb_t   c2 = machine->pens[line2[x]];
12681249            rgb_t   c3 = machine->pens[line3[x]];
12691250            rgb_t   c4 = machine->pens[line4[x]];
1270            *BITMAP_ADDR32( bitmap, pixelPlotY, pixelOffsetX + x) =
1271               MAKE_RGB( ( RGB_RED(c1)/6 + RGB_RED(c2)/3 + RGB_RED(c3)/3 + RGB_RED(c4)/6 ),
1272                  ( RGB_GREEN(c1)/6 + RGB_GREEN(c2)/3 + RGB_GREEN(c3)/3 + RGB_GREEN(c4)/6 ),
1273                  ( RGB_BLUE(c1)/6 + RGB_BLUE(c2)/3 + RGB_BLUE(c3)/3 + RGB_BLUE(c4)/6 ) );
1251            *BITMAP_ADDR32(bitmap, pixel_plot_y, pixel_offset_x + x) =
1252               MAKE_RGB((RGB_RED(c1) / 6 + RGB_RED(c2) / 3 + RGB_RED(c3) / 3 + RGB_RED(c4) / 6 ),
1253                  (RGB_GREEN(c1) / 6 + RGB_GREEN(c2) / 3 + RGB_GREEN(c3) / 3 + RGB_GREEN(c4) / 6 ),
1254                  (RGB_BLUE(c1) / 6 + RGB_BLUE(c2) / 3 + RGB_BLUE(c3) / 3 + RGB_BLUE(c4) / 6 ) );
12741255         }
12751256         return;
12761257      }
1277      blitLineBuffer = combineLineBuffer;
1258      blitline_buffer = combineline_buffer;
12781259   }
12791260
1280   for( x = 0; x < 256; x++ )
1261   for (x = 0; x < 256; x++)
12811262   {
1282      *BITMAP_ADDR32( bitmap, pixelPlotY + line, pixelOffsetX + x) = machine->pens[blitLineBuffer[x]];
1263      *BITMAP_ADDR32(bitmap, pixel_plot_y + line, pixel_offset_x + x) = machine->pens[blitline_buffer[x]];
12831264   }
12841265}
12851266
12861267
1287static void sms_update_palette(void)
1268static void sms_update_palette( smsvdp_t *smsvdp )
12881269{
12891270   int i;
12901271
12911272   /* Exit if palette is has no changes */
1292   if (smsvdp.cram_dirty == 0)
1273   if (smsvdp->cram_dirty == 0)
12931274   {
12941275      return;
12951276   }
1296   smsvdp.cram_dirty = 0;
1277   smsvdp->cram_dirty = 0;
12971278
1298   if ( smsvdp.vdp_mode != 4 && ! IS_GAMEGEAR_VDP )
1279   if (smsvdp->vdp_mode != 4 && ! IS_GAMEGEAR_VDP)
12991280   {
1300      for( i = 0; i < 16; i++ )
1281      for(i = 0; i < 16; i++)
13011282      {
1302         smsvdp.current_palette[i] = 64+i;
1283         smsvdp->current_palette[i] = 64 + i;
13031284      }
13041285      return;
13051286   }
13061287
1307   if ( IS_GAMEGEAR_VDP )
1288   if (IS_GAMEGEAR_VDP)
13081289   {
1309      if ( smsvdp.gg_sms_mode )
1290      if (smsvdp->gg_sms_mode)
13101291      {
1311         for ( i = 0; i < 32; i++ )
1292         for (i = 0; i < 32; i++)
13121293         {
1313            smsvdp.current_palette[i] = ( ( smsvdp.CRAM[i] & 0x30 ) << 6 ) | ( ( smsvdp.CRAM[i] & 0x0C ) << 4 ) | ( ( smsvdp.CRAM[i] & 0x03 ) << 2 );
1294            smsvdp->current_palette[i] = ((smsvdp->CRAM[i] & 0x30) << 6) | ((smsvdp->CRAM[i] & 0x0c ) << 4) | ((smsvdp->CRAM[i] & 0x03) << 2);
13141295         }
13151296      }
13161297      else
13171298      {
1318         for ( i = 0; i < 32; i++ )
1299         for (i = 0; i < 32; i++)
13191300         {
1320            smsvdp.current_palette[i] = ( ( smsvdp.CRAM[i*2+1] << 8 ) | smsvdp.CRAM[i*2] ) & 0x0FFF;
1301            smsvdp->current_palette[i] = ((smsvdp->CRAM[i * 2 + 1] << 8) | smsvdp->CRAM[i * 2]) & 0x0fff;
13211302         }
13221303      }
13231304   }
13241305   else
13251306   {
1326      for ( i = 0; i < 32; i++ )
1307      for (i = 0; i < 32; i++)
13271308      {
1328         smsvdp.current_palette[i] = smsvdp.CRAM[i] & 0x3F;
1309         smsvdp->current_palette[i] = smsvdp->CRAM[i] & 0x3f;
13291310      }
13301311   }
13311312}
13321313
13331314
1334VIDEO_UPDATE(sms)
1315UINT32 smsvdp_update( const device_config *device, bitmap_t *bitmap, const rectangle *cliprect )
13351316{
1317   smsvdp_t *smsvdp = get_safe_token(device);
1318   const device_config *screen = video_screen_first(device->machine->config);
13361319   int width = video_screen_get_width(screen);
13371320   int height = video_screen_get_height(screen);
13381321   int x, y;
13391322
1340   if (smsvdp.prev_bitmap_saved)
1323   if (smsvdp->prev_bitmap_saved)
13411324   {
13421325      for (y = 0; y < height; y++)
13431326      {
13441327         for (x = 0; x < width; x++)
13451328         {
1346            *BITMAP_ADDR32(bitmap, y, x) = (*BITMAP_ADDR32(tmpbitmap, y, x) + *BITMAP_ADDR32(smsvdp.prev_bitmap, y, x)) >> 2;
1347            logerror("%x %x %x\n", *BITMAP_ADDR32(tmpbitmap, y, x), *BITMAP_ADDR32(smsvdp.prev_bitmap, y, x), (*BITMAP_ADDR32(tmpbitmap, y, x) + *BITMAP_ADDR32(smsvdp.prev_bitmap, y, x)) >> 2);
1329            *BITMAP_ADDR32(bitmap, y, x) = (*BITMAP_ADDR32(smsvdp->tmpbitmap, y, x) + *BITMAP_ADDR32(smsvdp->prev_bitmap, y, x)) >> 2;
1330            logerror("%x %x %x\n", *BITMAP_ADDR32(smsvdp->tmpbitmap, y, x), *BITMAP_ADDR32(smsvdp->prev_bitmap, y, x), (*BITMAP_ADDR32(smsvdp->tmpbitmap, y, x) + *BITMAP_ADDR32(smsvdp->prev_bitmap, y, x)) >> 2);
13481331         }
13491332      }
13501333   }
13511334   else
13521335   {
1353      copybitmap(bitmap, tmpbitmap, 0, 0, 0, 0, cliprect);
1336      copybitmap(bitmap, smsvdp->tmpbitmap, 0, 0, 0, 0, cliprect);
13541337   }
13551338
1356   if (!smsvdp.prev_bitmap_saved)
1339   if (!smsvdp->prev_bitmap_saved)
13571340   {
1358      copybitmap(smsvdp.prev_bitmap, tmpbitmap, 0, 0, 0, 0, cliprect);
1359   //smsvdp.prev_bitmap_saved = 1;
1341      copybitmap(smsvdp->prev_bitmap, smsvdp->tmpbitmap, 0, 0, 0, 0, cliprect);
1342   //smsvdp->prev_bitmap_saved = 1;
13601343   }
13611344
13621345   return 0;
13631346}
13641347
13651348
1349/*****************************************************************************
1350    DEVICE INTERFACE
1351*****************************************************************************/
1352
1353static DEVICE_START( smsvdp )
1354{
1355   smsvdp_t *smsvdp = get_safe_token(device);
1356   const smsvdp_interface *intf = get_interface(device);
1357
1358   const device_config *screen = video_screen_first(device->machine->config);
1359   int width = video_screen_get_width(screen);
1360   int height = video_screen_get_height(screen);
1361
1362   smsvdp->features = intf->model;
1363   smsvdp->int_callback = intf->int_callback;
1364   smsvdp->pause_callback = intf->pause_callback;
1365
1366   /* Allocate video RAM */
1367   smsvdp->VRAM = memory_region_alloc(device->machine, "vdp_vram", VRAM_SIZE, ROM_REQUIRED);
1368   smsvdp->CRAM = memory_region_alloc(device->machine, "vdp_cram", MAX_CRAM_SIZE, ROM_REQUIRED);
1369   smsvdp->line_buffer = auto_alloc_array(device->machine, int, 256 * 5);
1370
1371   smsvdp->collision_buffer = auto_alloc_array(device->machine, UINT8, SMS_X_PIXELS);
1372   smsvdp->sms_frame_timing = auto_alloc_array(device->machine, UINT8, 7);
1373
1374   /* Make temp bitmap for rendering */
1375   smsvdp->tmpbitmap = auto_bitmap_alloc(device->machine, width, height, BITMAP_FORMAT_INDEXED32);
1376
1377   smsvdp->prev_bitmap = auto_bitmap_alloc(device->machine, width, height, BITMAP_FORMAT_INDEXED32);
1378
1379
1380   smsvdp->smsvdp_display_timer = timer_alloc(device->machine, smsvdp_display_callback, smsvdp);
1381   timer_adjust_periodic(smsvdp->smsvdp_display_timer, video_screen_get_time_until_pos(screen, 0, 0), 0, video_screen_get_scan_period(screen));
1382
1383   state_save_register_device_item(device, 0, smsvdp->status);
1384   state_save_register_device_item(device, 0, smsvdp->reg9copy);
1385   state_save_register_device_item(device, 0, smsvdp->addrmode);
1386   state_save_register_device_item(device, 0, smsvdp->addr);
1387   state_save_register_device_item(device, 0, smsvdp->cram_mask);
1388   state_save_register_device_item(device, 0, smsvdp->cram_dirty);
1389   state_save_register_device_item(device, 0, smsvdp->pending);
1390   state_save_register_device_item(device, 0, smsvdp->buffer);
1391   state_save_register_device_item(device, 0, smsvdp->gg_sms_mode);
1392   state_save_register_device_item(device, 0, smsvdp->irq_state);
1393   state_save_register_device_item(device, 0, smsvdp->vdp_mode);
1394   state_save_register_device_item(device, 0, smsvdp->y_pixels);
1395   state_save_register_device_item(device, 0, smsvdp->line_counter);
1396   state_save_register_device_item(device, 0, smsvdp->hcounter);
1397   state_save_register_device_item(device, 0, smsvdp->prev_bitmap_saved);
1398   state_save_register_device_item_array(device, 0, smsvdp->reg);
1399   state_save_register_device_item_array(device, 0, smsvdp->current_palette);
1400}
1401
1402static DEVICE_RESET( smsvdp )
1403{
1404   smsvdp_t *smsvdp = get_safe_token(device);
1405   int i;
1406
1407   /* Most register are 0x00 at power-up */
1408   for (i = 0; i < NUM_OF_REGISTER; i++)
1409      smsvdp->reg[i] = 0x00;
1410
1411   smsvdp->reg[0x02] = 0x0e;
1412   smsvdp->reg[0x0a] = 0xff;
1413
1414   smsvdp->status = 0;
1415   smsvdp->reg9copy = 0;
1416   smsvdp->addrmode = 0;
1417   smsvdp->addr = 0;
1418   smsvdp->gg_sms_mode = 0;
1419   smsvdp->cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
1420   smsvdp->cram_dirty = 1;
1421   smsvdp->pending = 0;
1422   smsvdp->buffer = 0;
1423   smsvdp->irq_state = 0;
1424   smsvdp->line_counter = 0;
1425   smsvdp->prev_bitmap_saved = 0;
1426
1427   /* for light gun input with "Hang-On & Safari Hunt", hcounter seems to
1428    need initialization to some value */
1429   smsvdp->hcounter = 0x40;
1430   
1431   for (i = 0; i < 0x20; i++)
1432      smsvdp->current_palette[i] = 0;
1433
1434   set_display_settings(device);
1435
1436   /* Clear RAM */
1437   memset(smsvdp->VRAM, 0, VRAM_SIZE);
1438   memset(smsvdp->CRAM, 0, MAX_CRAM_SIZE);
1439   memset(smsvdp->line_buffer, 0, 256 * 5 * sizeof(int));
1440}
1441
1442DEVICE_GET_INFO( smsvdp )
1443{
1444   switch (state)
1445   {
1446      /* --- the following bits of info are returned as 64-bit signed integers --- */
1447      case DEVINFO_INT_TOKEN_BYTES:         info->i = sizeof(smsvdp_t);               break;
1448      case DEVINFO_INT_CLASS:               info->i = DEVICE_CLASS_VIDEO;               break;
1449
1450      /* --- the following bits of info are returned as pointers to data or functions --- */
1451      case DEVINFO_FCT_START:               info->start = DEVICE_START_NAME(smsvdp);      break;
1452      case DEVINFO_FCT_STOP:               /* Nothing */                           break;
1453      case DEVINFO_FCT_RESET:               info->reset = DEVICE_RESET_NAME(smsvdp);      break;
1454
1455      /* --- the following bits of info are returned as NULL-terminated strings --- */
1456      case DEVINFO_STR_NAME:               strcpy(info->s, "Sega Master SYstem / Game Gear VDP");            break;
1457      case DEVINFO_STR_FAMILY:            strcpy(info->s, "Sega MS VDP");               break;
1458      case DEVINFO_STR_VERSION:            strcpy(info->s, "1.0");                     break;
1459      case DEVINFO_STR_SOURCE_FILE:         strcpy(info->s, __FILE__);                  break;
1460      case DEVINFO_STR_CREDITS:            strcpy(info->s, "Copyright MAME / MESS Team");         break;
1461   }
1462}
src/mess/video/smsvdp.h
r6078r6079
1#ifndef _VIDEO_SMSVDP_H_
2#define _VIDEO_SMSVDP_H_
1/*************************************************************************
32
3    smsvdp.h
4
5    Implementation of Sega VDP chip used in Master System and Game Gear
6
7**************************************************************************/
8
9#ifndef __SMSVDP_H__
10#define __SMSVDP_H__
11
12#include "devcb.h"
13
14
15/***************************************************************************
16    CONSTANTS
17***************************************************************************/
18
19
420#define MODEL_315_5124         0x0001
521#define MODEL_315_5246         0x0002
622#define MODEL_315_5378         0x0004
r6078r6079
824#define SMS_X_PIXELS         342      /* 342 pixels */
925#define NTSC_Y_PIXELS         262      /* 262 lines */
1026#define PAL_Y_PIXELS         313      /* 313 lines */
11#define LBORDER_START         ( 1 + 2 + 14 + 8 )
12#define LBORDER_X_PIXELS      (0x0D)      /* 13 pixels */
13#define RBORDER_X_PIXELS      (0x0F)      /* 15 pixels */
14#define TBORDER_START         ( 3 + 13 )
15#define NTSC_192_TBORDER_Y_PIXELS   (0x1B)      /* 27 lines */
27#define LBORDER_START         (1 + 2 + 14 + 8)
28#define LBORDER_X_PIXELS      (0x0d)      /* 13 pixels */
29#define RBORDER_X_PIXELS      (0x0f)      /* 15 pixels */
30#define TBORDER_START         (3 + 13)
31#define NTSC_192_TBORDER_Y_PIXELS   (0x1b)      /* 27 lines */
1632#define NTSC_192_BBORDER_Y_PIXELS   (0x18)      /* 24 lines */
17#define NTSC_224_TBORDER_Y_PIXELS   (0x0B)      /* 11 lines */
33#define NTSC_224_TBORDER_Y_PIXELS   (0x0b)      /* 11 lines */
1834#define NTSC_224_BBORDER_Y_PIXELS   (0x08)      /* 8 lines */
1935#define PAL_192_TBORDER_Y_PIXELS   (0x36)      /* 54 lines */
2036#define PAL_192_BBORDER_Y_PIXELS   (0x30)      /* 48 lines */
2137#define PAL_224_TBORDER_Y_PIXELS   (0x26)      /* 38 lines */
2238#define PAL_224_BBORDER_Y_PIXELS   (0x20)      /* 32 lines */
23#define PAL_240_TBORDER_Y_PIXELS   (0x1E)      /* 30 lines */
39#define PAL_240_TBORDER_Y_PIXELS   (0x1e)      /* 30 lines */
2440#define PAL_240_BBORDER_Y_PIXELS   (0x18)      /* 24 lines */
2541
2642
27typedef struct smsvdp_configuration {
28   UINT32   model;         /* Select model/features for the emulation */
29   void   (*int_callback)(running_machine*,int);   /* interupt callback function */
30} smsvdp_configuration;
43/***************************************************************************
44    TYPE DEFINITIONS
45***************************************************************************/
3146
47typedef void (*smsvdp_int_cb)( running_machine *machine, int state );
48typedef void (*smsvdp_pause_cb)( running_machine *machine );
3249
50typedef struct _smsvdp_interface smsvdp_interface;
51struct _smsvdp_interface
52{
53   UINT32             model;                /* Select model/features for the emulation */
54   smsvdp_int_cb      int_callback;         /* Interrupt callback function */
55   smsvdp_pause_cb    pause_callback;       /* Pause callback function */
56};
57
58/***************************************************************************
59    FUNCTION PROTOTYPES
60***************************************************************************/
61
62DEVICE_GET_INFO( smsvdp );
63
64
65/***************************************************************************
66    DEVICE CONFIGURATION MACROS
67***************************************************************************/
68
69#define SMSVDP DEVICE_GET_INFO_NAME( smsvdp )
70
71#define MDRV_SMSVDP_ADD(_tag, _interface) \
72   MDRV_DEVICE_ADD(_tag, SMSVDP, 0) \
73   MDRV_DEVICE_CONFIG(_interface)
74
75
76/***************************************************************************
77    DEVICE I/O FUNCTIONS
78***************************************************************************/
79
3380/* prototypes */
3481
35int smsvdp_video_init( running_machine *machine, const smsvdp_configuration *config );
36VIDEO_UPDATE(sms);
37 READ8_HANDLER(sms_vdp_vcount_r);
38 READ8_HANDLER(sms_vdp_hcount_r);
39 READ8_HANDLER(sms_vdp_data_r);
40WRITE8_HANDLER(sms_vdp_data_w);
41 READ8_HANDLER(sms_vdp_ctrl_r);
42WRITE8_HANDLER(sms_vdp_ctrl_w);
43void sms_set_ggsmsmode(int mode);
82UINT32 smsvdp_update( const device_config *device, bitmap_t *bitmap, const rectangle *cliprect );
83READ8_DEVICE_HANDLER( sms_vdp_vcount_r );
84READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r );
85WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w );
86READ8_DEVICE_HANDLER( sms_vdp_data_r );
87WRITE8_DEVICE_HANDLER( sms_vdp_data_w );
88READ8_DEVICE_HANDLER( sms_vdp_ctrl_r );
89WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w );
90void sms_set_ggsmsmode( const device_config *device, int mode );
4491
45#endif /* _VIDEO_SMSVDP_H_ */
92#endif /* __SMSVDP_H__ */
src/mess/includes/sms.h
r6078r6079
99
1010#define LOG_REG
1111#define LOG_PAGING
12//#define LOG_CURLINE
1312#define LOG_COLOR
1413
15#define NVRAM_SIZE                        (0x8000)
16#define CPU_ADDRESSABLE_SIZE         (0x10000)
14#define NVRAM_SIZE             (0x08000)
15#define CPU_ADDRESSABLE_SIZE   (0x10000)
1716
1817/*----------- defined in machine/sms.c -----------*/
1918
2019/* Function prototypes */
21WRITE8_HANDLER(sms_cartram_w);
22WRITE8_HANDLER(sms_cartram2_w);
23WRITE8_HANDLER(sms_fm_detect_w);
24 READ8_HANDLER(sms_fm_detect_r);
25 READ8_HANDLER(sms_input_port_0_r);
26WRITE8_HANDLER(sms_ym2413_register_port_0_w);
27WRITE8_HANDLER(sms_ym2413_data_port_0_w);
28WRITE8_HANDLER(sms_version_w);
29 READ8_HANDLER(sms_version_r);
30 READ8_HANDLER(sms_count_r);
31WRITE8_HANDLER(sms_mapper_w);
32 READ8_HANDLER(sms_mapper_r);
33WRITE8_HANDLER(sms_bios_w);
34WRITE8_HANDLER(gg_sio_w);
35 READ8_HANDLER(gg_sio_r);
36 READ8_HANDLER(gg_psg_r);
37WRITE8_HANDLER(gg_psg_w);
38 READ8_HANDLER(gg_input_port_2_r);
20WRITE8_HANDLER( sms_cartram_w );
21WRITE8_HANDLER( sms_cartram2_w );
22WRITE8_HANDLER( sms_fm_detect_w );
23READ8_HANDLER( sms_fm_detect_r );
24READ8_HANDLER( sms_input_port_0_r );
25READ8_HANDLER( sms_input_port_1_r );
26WRITE8_HANDLER( sms_ym2413_register_port_0_w );
27WRITE8_HANDLER( sms_ym2413_data_port_0_w );
28WRITE8_HANDLER( sms_io_control_w );
29READ8_HANDLER( sms_count_r );
30WRITE8_HANDLER( sms_mapper_w );
31READ8_HANDLER( sms_mapper_r );
32WRITE8_HANDLER( sms_bios_w );
33WRITE8_HANDLER( gg_sio_w );
34READ8_HANDLER( gg_sio_r );
35READ8_HANDLER( gg_psg_r );
36WRITE8_HANDLER( gg_psg_w );
37READ8_HANDLER( gg_input_port_2_r );
3938
40void sms_check_pause_button(const address_space *space);
39void sms_pause_callback( running_machine *machine );
40void sms_store_int_callback( running_machine *machine, int state );
4141
4242DEVICE_START( sms_cart );
4343DEVICE_IMAGE_LOAD( sms_cart );
4444
45MACHINE_START(sms);
46MACHINE_RESET(sms);
45MACHINE_START( sms );
46MACHINE_RESET( sms );
4747
48READ8_HANDLER(sms_store_cart_select_r);
49WRITE8_HANDLER(sms_store_cart_select_w);
50READ8_HANDLER(sms_store_select1);
51READ8_HANDLER(sms_store_select2);
52READ8_HANDLER(sms_store_control_r);
53WRITE8_HANDLER(sms_store_control_w);
54void sms_int_callback( running_machine *machine, int state );
55void sms_store_int_callback( running_machine *machine, int state );
48READ8_HANDLER( sms_store_cart_select_r );
49WRITE8_HANDLER( sms_store_cart_select_w );
50READ8_HANDLER( sms_store_select1 );
51READ8_HANDLER( sms_store_select2 );
52READ8_HANDLER( sms_store_control_r );
53WRITE8_HANDLER( sms_store_control_w );
5654
57#define IO_EXPANSION            (0x80)   /* Expansion slot enable (1= disabled, 0= enabled) */
58#define IO_CARTRIDGE            (0x40)   /* Cartridge slot enable (1= disabled, 0= enabled) */
59#define IO_CARD                     (0x20)   /* Card slot disabled (1= disabled, 0= enabled) */
60#define IO_WORK_RAM               (0x10)   /* Work RAM disabled (1= disabled, 0= enabled) */
61#define IO_BIOS_ROM               (0x08)   /* BIOS ROM disabled (1= disabled, 0= enabled) */
62#define IO_CHIP                     (0x04)   /* I/O chip disabled (1= disabled, 0= enabled) */
55#define IO_EXPANSION    (0x80)   /* Expansion slot enable (1= disabled, 0= enabled) */
56#define IO_CARTRIDGE    (0x40)   /* Cartridge slot enable (1= disabled, 0= enabled) */
57#define IO_CARD         (0x20)   /* Card slot disabled (1= disabled, 0= enabled) */
58#define IO_WORK_RAM     (0x10)   /* Work RAM disabled (1= disabled, 0= enabled) */
59#define IO_BIOS_ROM     (0x08)   /* BIOS ROM disabled (1= disabled, 0= enabled) */
60#define IO_CHIP         (0x04)   /* I/O chip disabled (1= disabled, 0= enabled) */
6361
6462
6563DRIVER_INIT( sg1000m3 );
src/mess/machine/sms.c
r6078r6079
99#define VERBOSE 0
1010#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
1111
12#define FLAG_GAMEGEAR      0x00020000
13#define FLAG_BIOS_0400      0x00040000
14#define FLAG_BIOS_2000      0x00080000
15#define FLAG_BIOS_FULL      0x00100000
16#define FLAG_FM            0x00200000
17#define FLAG_REGION_JAPAN   0x00400000
12#define CF_CODEMASTERS_MAPPER 0x01
13#define CF_KOREAN_MAPPER      0x02
14#define CF_93C46_EEPROM       0x04
15#define CF_ONCART_RAM         0x08
16#define CF_GG_SMS_MODE        0x10
1817
18#define MAX_CARTRIDGES        16
1919
20#define IS_GAMEGEAR         ( sms_state.flags & FLAG_GAMEGEAR )
21#define HAS_BIOS_0400      ( sms_state.flags & FLAG_BIOS_0400 )
22#define HAS_BIOS_2000      ( sms_state.flags & FLAG_BIOS_2000 )
23#define HAS_BIOS_FULL      ( sms_state.flags & FLAG_BIOS_FULL )
24#define HAS_BIOS         ( sms_state.flags & ( FLAG_BIOS_0400 | FLAG_BIOS_2000 | FLAG_BIOS_FULL ) )
25#define HAS_FM            ( sms_state.flags & FLAG_FM )
26#define IS_REGION_JAPAN      ( sms_state.flags & FLAG_REGION_JAPAN )
27
28
29#define CF_CODEMASTERS_MAPPER   0x01
30#define CF_KOREAN_MAPPER   0x02
31#define CF_93C46_EEPROM      0x04
32#define CF_ONCART_RAM      0x08
33#define CF_GG_SMS_MODE      0x10
34
35#define MAX_CARTRIDGES      16
36
37
3820typedef struct _sms_driver_data sms_driver_data;
3921struct _sms_driver_data {
40   UINT8   BiosPageCount;
41   UINT8   FMDetect;
42   UINT8   Version;
43   int      Paused;
44   UINT8   biosPort;
45   UINT32   flags;
46   UINT8   *BIOS;
47   UINT8   *mapper_ram;
48   UINT8   mapper[4];
49   UINT8   *banking_bios[5];   /* we are going to use 1-4, same as bank numbers */
50   UINT8   *banking_cart[5];   /* we are going to use 1-4, same as bank numbers */
51   UINT8   *banking_none[5];   /* we are going to use 1-4, same as bank numbers */
52   UINT8   ggSIO[5];
53   UINT8   store_control;
54   UINT8   input_port0;
55   UINT8   input_port1;
22   UINT8 bios_page_count;
23   UINT8 fm_detect;
24   UINT8 ctrl_reg;
25   int paused;
26   UINT8 bios_port;
27   UINT8 *BIOS;
28   UINT8 *mapper_ram;
29   UINT8 mapper[4];
30   UINT8 *banking_bios[5]; /* we are going to use 1-4, same as bank numbers */
31   UINT8 *banking_cart[5]; /* we are going to use 1-4, same as bank numbers */
32   UINT8 *banking_none[5]; /* we are going to use 1-4, same as bank numbers */
33   UINT8 gg_sio[5];
34   UINT8 store_control;
35   UINT8 input_port0;
36   UINT8 input_port1;
5637
38   /* Model identifiers */
39   UINT8 is_gamegear;
40   UINT8 is_region_japan;
41   UINT8 has_bios_0400;
42   UINT8 has_bios_2000;
43   UINT8 has_bios_full;
44   UINT8 has_bios;
45   UINT8 has_fm;
46
5747   /* Data needed for Rapid Fire Unit support */
58   emu_timer   *rapid_fire_timer;
59   UINT8   rapid_fire_state_1;
60   UINT8   rapid_fire_state_2;
61
48   emu_timer *rapid_fire_timer;
49   UINT8 rapid_fire_state_1;
50   UINT8 rapid_fire_state_2;
51   
6252   /* Data needed for Paddle Control controller */
63   UINT32   last_paddle_read_time;
64   UINT8   paddle_read_state;
65
53   UINT32 last_paddle_read_time;
54   UINT8 paddle_read_state;
55   
6656   /* Data needed for Sports Pad controller */
67   UINT32   last_sports_pad_time_1;
68   UINT32   last_sports_pad_time_2;
69   UINT8   sports_pad_state_1;
70   UINT8   sports_pad_state_2;
71   UINT8   sports_pad_last_data_1;
72   UINT8   sports_pad_last_data_2;
73   UINT8   sports_pad_1_x;
74   UINT8   sports_pad_1_y;
75   UINT8   sports_pad_2_x;
76   UINT8   sports_pad_2_y;
57   UINT32 last_sports_pad_time_1;
58   UINT32 last_sports_pad_time_2;
59   UINT8 sports_pad_state_1;
60   UINT8 sports_pad_state_2;
61   UINT8 sports_pad_last_data_1;
62   UINT8 sports_pad_last_data_2;
63   UINT8 sports_pad_1_x;
64   UINT8 sports_pad_1_y;
65   UINT8 sports_pad_2_x;
66   UINT8 sports_pad_2_y;
67   
68   /* Data needed for Light Phaser */
69   UINT8 lphaser_latch;
70   UINT8 lphaser_1_x;
71   UINT8 lphaser_1_y;
72   UINT8 lphaser_2_x;
73   UINT8 lphaser_2_y;
74   UINT8 lphaser_x_offs;   /* Needed to 'calibrate' lphaser; set at cart loading */
7775
7876   /* Cartridge slot info */
79   UINT8   current_cartridge;
77   UINT8 current_cartridge;
8078   struct
8179   {
82      UINT8   *ROM;         /* Pointer to ROM image data */
83      UINT32   size;         /* Size of the ROM image */
84      UINT8   features;      /* on-cartridge special hardware */
85      UINT8   *cartSRAM;      /* on-cartridge SRAM */
86      UINT8   sram_save;      /* should be the contents of the on-cartridge SRAM be saved */
87      UINT8   *cartRAM;      /* additional on-cartridge RAM (64KB for Ernie Els Golf) */
88      UINT32   ram_size;      /* size of the on-cartridge RAM */
89      UINT8   ram_page;      /* currently swapped in cartridge RAM */
80      UINT8 *ROM;        /* Pointer to ROM image data */
81      UINT32 size;       /* Size of the ROM image */
82      UINT8 features;    /* on-cartridge special hardware */
83      UINT8 *cartSRAM;   /* on-cartridge SRAM */
84      UINT8 sram_save;   /* should be the contents of the on-cartridge SRAM be saved */
85      UINT8 *cartRAM;    /* additional on-cartridge RAM (64KB for Ernie Els Golf) */
86      UINT32 ram_size;   /* size of the on-cartridge RAM */
87      UINT8 ram_page;    /* currently swapped in cartridge RAM */
9088   } cartridge[MAX_CARTRIDGES];
9189};
9290
9391static sms_driver_data sms_state;
9492
93
9594static void setup_rom(const address_space *space);
9695
9796
9897static TIMER_CALLBACK( rapid_fire_callback )
9998{
100   sms_state.rapid_fire_state_1 ^= 0xFF;
101   sms_state.rapid_fire_state_2 ^= 0xFF;
99   sms_state.rapid_fire_state_1 ^= 0xff;
100   sms_state.rapid_fire_state_2 ^= 0xff;
102101}
103102
104103
105104static WRITE8_HANDLER( sms_input_write )
106105{
107   switch( offset )
106   switch (offset)
108107   {
109108   case 0:
110      switch( input_port_read_safe(space->machine, "CTRLSEL", 0x00) & 0x0F )
109      switch (input_port_read_safe(space->machine, "CTRLSEL", 0x00) & 0x0f)
111110      {
112111      case 0x03:   /* Sports Pad */
113         if ( data != sms_state.sports_pad_last_data_1 )
112         if (data != sms_state.sports_pad_last_data_1)
114113         {
115114            UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu);
116115
117116            sms_state.sports_pad_last_data_1 = data;
118            if ( cpu_cycles - sms_state.last_sports_pad_time_1 > 512 )
117            if (cpu_cycles - sms_state.last_sports_pad_time_1 > 512)
119118            {
120119               sms_state.sports_pad_state_1 = 3;
121120               sms_state.sports_pad_1_x = input_port_read(space->machine, "SPORT0");
122121               sms_state.sports_pad_1_y = input_port_read(space->machine, "SPORT1");
123122            }
124123            sms_state.last_sports_pad_time_1 = cpu_cycles;
125            sms_state.sports_pad_state_1 = ( sms_state.sports_pad_state_1 + 1 ) & 3;
124            sms_state.sports_pad_state_1 = (sms_state.sports_pad_state_1 + 1) & 3;
126125         }
127126         break;
128127      }
129128      break;
130129
131130   case 1:
132      switch( input_port_read_safe(space->machine, "CTRLSEL", 0x00) >> 4 )
131      switch (input_port_read_safe(space->machine, "CTRLSEL", 0x00) >> 4)
133132      {
134133      case 0x03:   /* Sports Pad */
135         if ( data != sms_state.sports_pad_last_data_2 )
134         if (data != sms_state.sports_pad_last_data_2)
136135         {
137136            UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu);
138137
139138            sms_state.sports_pad_last_data_2 = data;
140            if ( cpu_cycles - sms_state.last_sports_pad_time_2 > 2048 )
139            if (cpu_cycles - sms_state.last_sports_pad_time_2 > 2048)
141140            {
142141               sms_state.sports_pad_state_2 = 3;
143142               sms_state.sports_pad_2_x = input_port_read(space->machine, "SPORT2");
144143               sms_state.sports_pad_2_y = input_port_read(space->machine, "SPORT3");
145144            }
146145            sms_state.last_sports_pad_time_2 = cpu_cycles;
147            sms_state.sports_pad_state_2 = ( sms_state.sports_pad_state_2 + 1 ) & 3;
146            sms_state.sports_pad_state_2 = (sms_state.sports_pad_state_2 + 1) & 3;
148147         }
149148         break;
150149      }
r6078r6079
152151   }
153152}
154153
155static void sms_get_inputs(const address_space *space)
154static TIMER_CALLBACK( lightgun_tick )
156155{
156   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f) == 0x01)
157   {
158         /* enable crosshair */
159         crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
160   }
161   else
162   {
163         /* disable crosshair */
164         crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
165   }
166   
167   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0) == 0x10)
168   {
169         /* enable crosshair */
170         crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_ALL);
171   }
172   else
173   {
174         /* disable crosshair */
175         crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_NONE);
176   }
177}
178
179
180/* FIXME: this function is a hack. For Light Phaser X pos sms_vdp_hcount_latch
181 function should be used instead, but emulation (timming?) needs to improve. */
182void sms_vdp_hcount_lphaser( running_machine *machine, int hpos)
183{
184   UINT8 tmp = ((hpos - 46) >> 1) & 0xff;
185   const device_config *smsvdp = devtag_get_device(machine, "sms_vdp");
186   
187   //printf ("sms_vdp_hcount_lphaser: hpos %3d => hcount %2X\n", hpos, tmp);
188   sms_vdp_hcount_latch_w(smsvdp, 0, tmp);
189}
190
191
192static UINT8 sms_vdp_hcount( running_machine *machine )
193{
194   UINT8 tmp;
195   UINT64 calc_cycles;
196   attotime time_end;
197   const device_config *screen = video_screen_first(machine->config);
198   int vpos = video_screen_get_vpos(screen);
199   int width = video_screen_get_width(screen);
200   
201   /* this is simpler and independent of other clocks */
202   //tmp = ( (hpos - 46) >> 1 ) & 0xFF;
203   
204   /* ... but this is closer to the behavior described by Flubba */
205   /* and makes use of his equation posted at SMSPower forum.    */
206   time_end = video_screen_get_time_until_pos(screen, vpos, width - 1);
207   calc_cycles = cpu_attotime_to_clocks(cputag_get_cpu(machine, "maincpu"), time_end);
208   tmp = ((590 - (calc_cycles * 3)) / 4) & 0xff;
209   
210   //printf ("sms_vdp_hcount: hpos %3d => hcount %2X\n", hpos, tmp);
211   return tmp;
212}
213
214
215static void sms_vdp_hcount_latch( running_machine *machine )
216{
217   UINT8 value = sms_vdp_hcount(machine);
218   const device_config *smsvdp = devtag_get_device(machine, "sms_vdp");
219   
220   sms_vdp_hcount_latch_w(smsvdp, 0, value);
221}
222
223static int lphaser_sensor_is_on( running_machine *machine, int x, int y )
224{
225   const device_config *screen = video_screen_first(machine->config);
226   int dx, dy;
227   //const int r_x_r = 20 * 20; /* radius^2 */
228   int hpos, vpos;
229   
230   if (x < LBORDER_START + LBORDER_X_PIXELS || x >= LBORDER_START + LBORDER_X_PIXELS + 256)
231   {
232      sms_state.lphaser_latch = 0;
233      return 0;
234   }
235   
236   hpos = video_screen_get_hpos(screen);
237   vpos = video_screen_get_vpos(screen);
238   
239   /* some offset seems needed for sms_vdp_hcount_latch() */
240   //hpos -= 44;
241   
242   dx = x - hpos;
243   dy = y - vpos;
244   
245   //printf ("Light Phaser polling at %3d x %3d for shot %3d x %3d\n", hpos, vpos, x, y);
246   
247   /* SMSPower Wiki: the Light Phaser sensor "sees" a circular portion of the screen */
248   //if ( (dx * dx) + (dy * dy) - (r_x_r) <= 0 )
249   if (abs(dy) <= 5 && abs(dx) <= 60) /* simpler method used by SMSPlus-GX */
250   {
251      /* avoid latching hcount more than once in a line */
252      if (sms_state.lphaser_latch == 0)
253      {
254         sms_state.lphaser_latch = 1;
255         //sms_vdp_hcount_latch(screen);
256         /* FIXME: sms_vdp_hcount_lphaser() is a hack necessary while normal latch gives too inconstant shot positions */
257         x += sms_state.lphaser_x_offs;
258         sms_vdp_hcount_lphaser(machine, x);
259      }
260
261      return 1;
262   }
263   else
264   {
265      sms_state.lphaser_latch = 0;
266      return 0;
267   }
268}
269
270
271static int screen_hpos_nonscaled( const device_config *screen, int scaled_hpos )
272{
273   const rectangle *visarea = video_screen_get_visible_area(screen);
274   int offset_x = (scaled_hpos * (visarea->max_x - visarea->min_x)) / 255;
275   return visarea->min_x + offset_x;
276}
277
278
279static int screen_vpos_nonscaled( const device_config *screen, int scaled_vpos )
280{
281   const rectangle *visarea = video_screen_get_visible_area(screen);
282   int offset_y = (scaled_vpos * (visarea->max_y - visarea->min_y)) / 255;
283   return visarea->min_y + offset_y;
284}
285
286
287static void sms_get_inputs( const address_space *space )
288{
157289   UINT8 data = 0x00;
290   UINT8 data_x = 0x00;
291   UINT8 data_y = 0x00;
158292   UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu);
159293   running_machine *machine = space->machine;
160294
161   sms_state.input_port0 = 0xFF;
162   sms_state.input_port1 = 0xFF;
295   sms_state.input_port0 = 0xff;
296   sms_state.input_port1 = 0xff;
163297
164   if ( cpu_cycles - sms_state.last_paddle_read_time > 256 ) {
165      sms_state.paddle_read_state ^= 0xFF;
298   if (cpu_cycles - sms_state.last_paddle_read_time > 256)
299   {
300      sms_state.paddle_read_state ^= 0xff;
166301      sms_state.last_paddle_read_time = cpu_cycles;
167302   }
168303
304   /* Check if lightgun has been chosen as input: if so, enable crosshair */
305   timer_set(machine, attotime_zero, NULL, 0, lightgun_tick);
306
169307   /* Player 1 */
170   switch( input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0F )
308   switch (input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f)
171309   {
172310   case 0x00:  /* Joystick */
173311      data = input_port_read(machine, "PORT_DC");
174      /* Rapid Fire setting for Button A */
175      if ( !(data & 0x10 ) && ( input_port_read(machine, "RFU") & 0x01) )
176      {
312      /* Check Rapid Fire setting for Button A */
313      if (!(data & 0x10) && (input_port_read(machine, "RFU") & 0x01))
177314         data |= sms_state.rapid_fire_state_1 & 0x10;
178      }
315
179316      /* Check Rapid Fire setting for Button B */
180      if ( !(data & 0x20) && (input_port_read(machine, "RFU") & 0x02) )
181      {
317      if (!(data & 0x20) && (input_port_read(machine, "RFU") & 0x02))
182318         data |= sms_state.rapid_fire_state_1 & 0x20;
183      }
184      sms_state.input_port0 = ( sms_state.input_port0 & 0xC0 ) | ( data & 0x3F );
319
320      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | (data & 0x3f);
185321      break;
186322
187323   case 0x01:  /* Light Phaser */
324      data = (input_port_read(machine, "CTRLIPT") & 0x01) << 4;
325      if (!(data & 0x10))
326      {
327         if (input_port_read(machine, "RFU") & 0x01)
328            data |= sms_state.rapid_fire_state_1 & 0x10;
329
330         data_x = input_port_read(machine, "LPHASER0");
331         data_y = input_port_read(machine, "LPHASER1");
332
333         sms_state.lphaser_1_x = screen_hpos_nonscaled(machine->primary_screen, data_x);
334         sms_state.lphaser_1_y = screen_vpos_nonscaled(machine->primary_screen, data_y);
335      }
336      /* just consider the button (trigger) bit */
337      data |= ~0x10;
338      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | (data & 0x3f);
188339      break;
189340
190341   case 0x02:  /* Paddle Control */
191342      /* Get button A state */
192343      data = input_port_read(machine, "PADDLE0");
193      if ( sms_state.paddle_read_state )
194      {
344
345      if (sms_state.paddle_read_state)
195346         data = data >> 4;
196      }
197      sms_state.input_port0 = ( sms_state.input_port0 & 0xC0 ) | ( data & 0x0F ) | ( sms_state.paddle_read_state & 0x20 )
198                      | ( ( input_port_read(machine, "CTRLIPT") & 0x02 ) << 3 );
347
348      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | (data & 0x0f) | (sms_state.paddle_read_state & 0x20)
349                      | ((input_port_read(machine, "CTRLIPT") & 0x02) << 3);
199350      break;
200351
201352   case 0x03:   /* Sega Sports Pad */
202      switch( sms_state.sports_pad_state_1 )
353      switch (sms_state.sports_pad_state_1)
203354      {
204355      case 0:
205         data = ( sms_state.sports_pad_1_x >> 4 ) & 0x0F;
356         data = (sms_state.sports_pad_1_x >> 4) & 0x0f;
206357         break;
207358      case 1:
208         data = sms_state.sports_pad_1_x & 0x0F;
359         data = sms_state.sports_pad_1_x & 0x0f;
209360         break;
210361      case 2:
211         data = ( sms_state.sports_pad_1_y >> 4 ) & 0x0F;
362         data = (sms_state.sports_pad_1_y >> 4) & 0x0f;
212363         break;
213364      case 3:
214         data = sms_state.sports_pad_1_y & 0x0F;
365         data = sms_state.sports_pad_1_y & 0x0f;
215366         break;
216367      }
217      sms_state.input_port0 = ( sms_state.input_port0 & 0xC0 ) | data | ( ( input_port_read(machine, "CTRLIPT") & 0x0C ) << 2 );
368      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
218369      break;
219370   }
220371
221372   /* Player 2 */
222   switch( input_port_read_safe(machine, "CTRLSEL", 0x00) >> 4 )
373   switch (input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0)
223374   {
224375   case 0x00:   /* Joystick */
225376      data = input_port_read(machine, "PORT_DC");
226      sms_state.input_port0 = ( sms_state.input_port0 & 0x3F ) | ( data & 0xC0 );
377      sms_state.input_port0 = (sms_state.input_port0 & 0x3f) | (data & 0xc0);
378
227379      data = input_port_read(machine, "PORT_DD");
228      if ( !(data & 0x04) && (input_port_read(machine, "RFU") & 0x04) )
229      {
380      /* Check Rapid Fire setting for Button A */
381      if (!(data & 0x04) && (input_port_read(machine, "RFU") & 0x04))
230382         data |= sms_state.rapid_fire_state_2 & 0x04;
231      }
232      if ( !(data & 0x08) && (input_port_read(machine, "RFU") & 0x08) )
233      {
383
384      /* Check Rapid Fire setting for Button B */
385      if (!(data & 0x08) && (input_port_read(machine, "RFU") & 0x08))
234386         data |= sms_state.rapid_fire_state_2 & 0x08;
235      }
236      sms_state.input_port1 = ( sms_state.input_port1 & 0xF0 ) | ( data & 0x0F );
387
388      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | (data & 0x0f);
237389      break;
238390
239   case 0x01:   /* Light Phaser */
391   case 0x10:   /* Light Phaser */
392      data = (input_port_read(machine, "CTRLIPT") & 0x10) >> 2;
393      if (!(data & 0x04))
394      {
395         if (input_port_read(machine, "RFU") & 0x04)
396            data |= sms_state.rapid_fire_state_2 & 0x04;
397
398         data_x = input_port_read(machine, "LPHASER2");
399         data_y = input_port_read(machine, "LPHASER3");
400
401         sms_state.lphaser_2_x = screen_hpos_nonscaled (machine->primary_screen, data_x);
402         sms_state.lphaser_2_y = screen_vpos_nonscaled (machine->primary_screen, data_y);
403      }
404      /* just consider the button (trigger) bit */
405      data |= ~0x04;
406      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | (data & 0x0f);
240407      break;
241408
242   case 0x02:   /* Paddle Control */
409   case 0x20:   /* Paddle Control */
243410      /* Get button A state */
244411      data = input_port_read(machine, "PADDLE1");
245      if ( sms_state.paddle_read_state )
246      {
412      if (sms_state.paddle_read_state)
247413         data = data >> 4;
248      }
249      sms_state.input_port0 = ( sms_state.input_port0 & 0x3F ) | ( ( data & 0x03 ) << 6 );
250      sms_state.input_port1 = ( sms_state.input_port1 & 0xF0 ) | ( ( data & 0x0C ) >> 2 ) | ( sms_state.paddle_read_state & 0x08 )
251                      | ( ( input_port_read(machine, "CTRLIPT") & 0x20 ) >> 3 );
414
415      sms_state.input_port0 = (sms_state.input_port0 & 0x3f) | ((data & 0x03) << 6);
416      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (sms_state.paddle_read_state & 0x08)
417                      | ((input_port_read(machine, "CTRLIPT") & 0x20) >> 3);
252418      break;
253419
254   case 0x03:   /* Sega Sports Pad */
255      switch( sms_state.sports_pad_state_2 )
420   case 0x30:   /* Sega Sports Pad */
421      switch (sms_state.sports_pad_state_2)
256422      {
257423      case 0:
258         data = sms_state.sports_pad_2_x & 0x0F;
424         data = sms_state.sports_pad_2_x & 0x0f;
259425         break;
260426      case 1:
261         data = ( sms_state.sports_pad_2_x >> 4 ) & 0x0F;
427         data = (sms_state.sports_pad_2_x >> 4) & 0x0f;
428         break;
262429      case 2:
263         data = sms_state.sports_pad_2_y & 0x0F;
430         data = sms_state.sports_pad_2_y & 0x0f;
264431         break;
265432      case 3:
266         data = ( sms_state.sports_pad_2_y >> 4 ) & 0x0F;
433         data = (sms_state.sports_pad_2_y >> 4) & 0x0f;
267434         break;
268435      }
269      sms_state.input_port0 = ( sms_state.input_port0 & 0x3F ) | ( ( data & 0x03 ) << 6 );
270      sms_state.input_port1 = ( sms_state.input_port1 & 0xF0 ) | ( data >> 2 ) | ( ( input_port_read(machine, "CTRLIPT") & 0xC0 ) >> 4 );
436      sms_state.input_port0 = (sms_state.input_port0 & 0x3f) | ((data & 0x03) << 6);
437      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
271438      break;
272439   }
273440}
274441
275442
276WRITE8_HANDLER(sms_fm_detect_w)
443WRITE8_HANDLER( sms_fm_detect_w )
277444{
278   if ( HAS_FM )
279   {
280      sms_state.FMDetect = (data & 0x01);
281   }
445   if (sms_state.has_fm)
446      sms_state.fm_detect = (data & 0x01);
282447}
283448
284449
285READ8_HANDLER(sms_fm_detect_r)
450READ8_HANDLER( sms_fm_detect_r )
286451{
287   if ( HAS_FM )
452   if (sms_state.has_fm)
288453   {
289      return sms_state.FMDetect;
454      return sms_state.fm_detect;
290455   }
291456   else
292457   {
293      if ( sms_state.biosPort & IO_CHIP )
458      if (sms_state.bios_port & IO_CHIP)
294459      {
295         return 0xFF;
460         return 0xff;
296461      }
297462      else
298463      {
r6078r6079
302467   }
303468}
304469
305
306WRITE8_HANDLER(sms_version_w)
470WRITE8_HANDLER( sms_io_control_w )
307471{
308   if ((data & 0x01) && (data & 0x04))
472   if (data & 0x08)
309473   {
310      sms_state.Version = (data & 0xA0);
474      /* check if TH pin level is high (1) and was low last time */
475      if (data & 0x80 && !(sms_state.ctrl_reg & 0x80))
476      {
477         sms_vdp_hcount_latch(space->machine);
478      }
479      sms_input_write(space, 0, (data & 0x20) >> 5);
311480   }
312   if ( data & 0x08 )
313   {
314      sms_input_write( space, 0, ( data & 0x20 ) >> 5 );
315   }
316   if ( data & 0x02 )
317   {
318      sms_input_write( space, 1, ( data & 0x80 ) >> 7 );
319   }
320}
321481
322
323READ8_HANDLER(sms_version_r)
324{
325   UINT8 temp;
326
327   if (sms_state.biosPort & IO_CHIP)
482   if (data & 0x02)
328483   {
329      return (0xFF);
484      if (data & 0x20 && !(sms_state.ctrl_reg & 0x20))
485      {
486         sms_vdp_hcount_latch(space->machine);
487      }
488      sms_input_write(space, 1, (data & 0x80) >> 7);
330489   }
331490
332   /* Move bits 7,5 of port 3F into bits 7, 6 */
333   temp = (sms_state.Version & 0x80) | (sms_state.Version & 0x20) << 1;
334
335   /* Inverse version detect value for Japanese machines */
336   if ( IS_REGION_JAPAN )
337   {
338      temp ^= 0xC0;
339   }
340
341   /* Merge version data with input port #2 data */
342   sms_get_inputs(space);
343   temp = (temp & 0xC0) | (sms_state.input_port1 & 0x3F);
344
345   return (temp);
491   sms_state.ctrl_reg = data;
346492}
347493
348494
349READ8_HANDLER(sms_count_r)
495READ8_HANDLER( sms_count_r )
350496{
351   if ( offset & 0x01 )
352   {
353      return sms_vdp_hcount_r(space, offset);
354   }
497   const device_config *smsvdp = devtag_get_device(space->machine, "sms_vdp");
498
499   if (offset & 0x01)
500      return sms_vdp_hcount_latch_r(smsvdp, offset);
355501   else
356   {
357      /* VCount read */
358      return sms_vdp_vcount_r(space, offset);
359   }
502      return sms_vdp_vcount_r(smsvdp, offset);
360503}
361504
362505
363506/*
364  Check if the pause button is pressed.
365  If the gamegear is in sms mode, check if the start button is pressed.
507 Check if the pause button is pressed.
508 If the gamegear is in sms mode, check if the start button is pressed.
366509 */
367void sms_check_pause_button(const address_space *space)
510void sms_pause_callback( running_machine *machine )
368511{
369   if ( IS_GAMEGEAR && ! ( sms_state.cartridge[sms_state.current_cartridge].features & CF_GG_SMS_MODE ) )
512   if (sms_state.is_gamegear && !(sms_state.cartridge[sms_state.current_cartridge].features & CF_GG_SMS_MODE))
513      return;
514   
515   if (!(input_port_read(machine, sms_state.is_gamegear ? "START" : "PAUSE") & 0x80))
370516   {
371      return;
372   }
373   if ( ! (input_port_read(space->machine, IS_GAMEGEAR ? "START" : "PAUSE") & 0x80) ) {
374      if ( ! sms_state.Paused ) {
375         cputag_set_input_line(space->machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE );
517      if (!sms_state.paused)
518      {
519         cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE);
376520      }
377      sms_state.Paused = 1;
521      sms_state.paused = 1;
378522   }
379523   else
380524   {
381      sms_state.Paused = 0;
525      sms_state.paused = 0;
382526   }
383527}
384528
385
386READ8_HANDLER(sms_input_port_0_r)
529READ8_HANDLER( sms_input_port_0_r )
387530{
388   if (sms_state.biosPort & IO_CHIP)
531   if (sms_state.bios_port & IO_CHIP)
389532   {
390      return (0xFF);
533      return 0xff;
391534   }
392535   else
393536   {
r6078r6079
397540}
398541
399542
400WRITE8_HANDLER(sms_ym2413_register_port_0_w)
543READ8_HANDLER( sms_input_port_1_r )
401544{
402   if ( HAS_FM )
545   if (sms_state.bios_port & IO_CHIP)
546      return 0xff;
547   
548   sms_get_inputs(space);
549   
550   /* Do region detection if TH of ports A and B are set to output (0) */
551   if (!(sms_state.ctrl_reg & 0x0a))
552    {
553      /* Move bits 7,5 of IO control port into bits 7, 6 */
554      sms_state.input_port1 = (sms_state.input_port1 & 0x3f) | (sms_state.ctrl_reg & 0x80) | (sms_state.ctrl_reg & 0x20) << 1;
555     
556      /* Inverse region detect value for Japanese machines */
557      if (sms_state.is_region_japan)
558         sms_state.input_port1 ^= 0xc0;
559   }
560   else if (sms_state.ctrl_reg & 0x02 && lphaser_sensor_is_on(space->machine, sms_state.lphaser_1_x, sms_state.lphaser_1_y))
403561   {
562      sms_state.input_port1 &= ~0x40;
563   }
564   else if (sms_state.ctrl_reg & 0x08 && lphaser_sensor_is_on(space->machine, sms_state.lphaser_2_x, sms_state.lphaser_2_y))
565   {   
566      sms_state.input_port1 &= ~0x80;
567   }   
568     
569   return sms_state.input_port1;
570}
571   
572
573   
574WRITE8_HANDLER( sms_ym2413_register_port_0_w )
575{
576   if (sms_state.has_fm)
577   {
404578      const device_config *ym = devtag_get_device(space->machine, "ym2413");
405      ym2413_w(ym, 0, (data & 0x3F));
579      ym2413_w(ym, 0, (data & 0x3f));
406580   }
407581}
408582
409583
410WRITE8_HANDLER(sms_ym2413_data_port_0_w)
584WRITE8_HANDLER( sms_ym2413_data_port_0_w )
411585{
412   if ( HAS_FM )
586   if (sms_state.has_fm)
413587   {
414588      const device_config *ym = devtag_get_device(space->machine, "ym2413");
415589      logerror("data_port_0_w %x %x\n", offset, data);
r6078r6079
418592}
419593
420594
421 READ8_HANDLER(gg_input_port_2_r)
595READ8_HANDLER( gg_input_port_2_r )
422596{
423   //logerror("joy 2 read, val: %02x, pc: %04x\n", (( IS_REGION_JAPAN ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
424   return (( IS_REGION_JAPAN ? 0x00 : 0x40 ) | (input_port_read(space->machine, "START") & 0x80));
597   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((sms_state.is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
598   return ((sms_state.is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine, "START") & 0x80));
425599}
426600
427601
428 READ8_HANDLER(sms_mapper_r)
602READ8_HANDLER( sms_mapper_r )
429603{
430604   return sms_state.mapper[offset];
431605}
432606
433607
434WRITE8_HANDLER(sms_mapper_w)
608WRITE8_HANDLER( sms_mapper_w )
435609{
436610   int page;
437611   UINT8 *SOURCE_BIOS;
438612   UINT8 *SOURCE_CART;
439613   UINT8 *SOURCE;
440   UINT8   rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
614   UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
441615
442616   offset &= 3;
443617
444618   sms_state.mapper[offset] = data;
445619   sms_state.mapper_ram[offset] = data;
446620
447   if ( sms_state.cartridge[sms_state.current_cartridge].ROM )
621   if (sms_state.cartridge[sms_state.current_cartridge].ROM)
448622   {
449      SOURCE_CART = sms_state.cartridge[sms_state.current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000;
623      SOURCE_CART = sms_state.cartridge[sms_state.current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
450624   }
451625   else
452626   {
453627      SOURCE_CART = sms_state.banking_none[1];
454628   }
455   if ( sms_state.BIOS )
629
630   if (sms_state.BIOS)
456631   {
457      SOURCE_BIOS = sms_state.BIOS + ( (sms_state.BiosPageCount > 0) ? data % sms_state.BiosPageCount : 0 ) * 0x4000;
632      SOURCE_BIOS = sms_state.BIOS + ((sms_state.bios_page_count > 0) ? data % sms_state.bios_page_count : 0) * 0x4000;
458633   }
459634   else
460635   {
461636      SOURCE_BIOS = sms_state.banking_none[1];
462637   }
463638
464   if (sms_state.biosPort & IO_BIOS_ROM || ( IS_GAMEGEAR && sms_state.BIOS == NULL ) )
639   if (sms_state.bios_port & IO_BIOS_ROM || (sms_state.is_gamegear && sms_state.BIOS == NULL))
465640   {
466      if (!(sms_state.biosPort & IO_CARTRIDGE) || ( IS_GAMEGEAR && sms_state.BIOS == NULL ) )
641      if (!(sms_state.bios_port & IO_CARTRIDGE) || (sms_state.is_gamegear && sms_state.BIOS == NULL))
467642      {
468         page = ( rom_page_count > 0) ? data % rom_page_count : 0;
469         if ( ! sms_state.cartridge[sms_state.current_cartridge].ROM )
643         page = (rom_page_count > 0) ? data % rom_page_count : 0;
644         if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
470645            return;
471646         SOURCE = SOURCE_CART;
472647      }
r6078r6079
478653   }
479654   else
480655   {
481      page = (sms_state.BiosPageCount > 0) ? data % sms_state.BiosPageCount : 0;
482      if ( ! sms_state.BIOS )
656      page = (sms_state.bios_page_count > 0) ? data % sms_state.bios_page_count : 0;
657      if (!sms_state.BIOS)
483658         return;
484659      SOURCE = SOURCE_BIOS;
485660   }
486661
487   switch(offset)
662   switch (offset)
488663   {
489      case 0: /* Control */
490         /* Is it ram or rom? */
491         if (data & 0x08) /* it's ram */
664   case 0: /* Control */
665      /* Is it ram or rom? */
666      if (data & 0x08) /* it's ram */
667      {
668         sms_state.cartridge[sms_state.current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
669         if (data & 0x04)
492670         {
493            sms_state.cartridge[sms_state.current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
494            if (data & 0x04)
495            {
496               LOG(("ram 1 paged.\n"));
497               SOURCE = sms_state.cartridge[sms_state.current_cartridge].cartSRAM + 0x4000;
498            }
499            else
500            {
501               LOG(("ram 0 paged.\n"));
502               SOURCE = sms_state.cartridge[sms_state.current_cartridge].cartSRAM;
503            }
504            memory_set_bankptr(space->machine,  4, SOURCE );
505            memory_set_bankptr(space->machine,  5, SOURCE + 0x2000 );
671            LOG(("ram 1 paged.\n"));
672            SOURCE = sms_state.cartridge[sms_state.current_cartridge].cartSRAM + 0x4000;
506673         }
507         else /* it's rom */
674         else
508675         {
509            if ( sms_state.biosPort & IO_BIOS_ROM || ! HAS_BIOS )
510            {
511               page = ( rom_page_count > 0) ? sms_state.mapper[3] % rom_page_count : 0;
512               SOURCE = sms_state.banking_cart[4];
513            }
514            else
515            {
516               page = (sms_state.BiosPageCount > 0) ? sms_state.mapper[3] % sms_state.BiosPageCount : 0;
517               SOURCE = sms_state.banking_bios[4];
518            }
519            LOG(("rom 2 paged in %x.\n", page));
520            memory_set_bankptr(space->machine,  4, SOURCE );
521            memory_set_bankptr(space->machine,  5, SOURCE + 0x2000 );
676            LOG(("ram 0 paged.\n"));
677            SOURCE = sms_state.cartridge[sms_state.current_cartridge].cartSRAM;
522678         }
523         break;
524
525      case 1: /* Select 16k ROM bank for 0400-3FFF */
526         LOG(("rom 0 paged in %x.\n", page));
527         sms_state.banking_bios[2] = SOURCE_BIOS + 0x0400;
528         sms_state.banking_cart[2] = SOURCE_CART + 0x0400;
529         if ( IS_GAMEGEAR )
679         memory_set_bankptr(space->machine,  4, SOURCE);
680         memory_set_bankptr(space->machine,  5, SOURCE + 0x2000);
681      }
682      else /* it's rom */
683      {
684         if (sms_state.bios_port & IO_BIOS_ROM || ! sms_state.has_bios)
530685         {
531            SOURCE = SOURCE_CART;
686            page = (rom_page_count > 0) ? sms_state.mapper[3] % rom_page_count : 0;
687            SOURCE = sms_state.banking_cart[4];
532688         }
533         memory_set_bankptr(space->machine,  2, SOURCE + 0x0400 );
534         break;
535
536      case 2: /* Select 16k ROM bank for 4000-7FFF */
537         LOG(("rom 1 paged in %x.\n", page));
538         sms_state.banking_bios[3] = SOURCE_BIOS;
539         sms_state.banking_cart[3] = SOURCE_CART;
540         if ( IS_GAMEGEAR )
689         else
541690         {
542            SOURCE = SOURCE_CART;
691            page = (sms_state.bios_page_count > 0) ? sms_state.mapper[3] % sms_state.bios_page_count : 0;
692            SOURCE = sms_state.banking_bios[4];
543693         }
544         memory_set_bankptr(space->machine,  3, SOURCE );
545         break;
694         LOG(("rom 2 paged in %x.\n", page));
695         memory_set_bankptr(space->machine,  4, SOURCE);
696         memory_set_bankptr(space->machine,  5, SOURCE + 0x2000);
697      }
698      break;
546699
547      case 3: /* Select 16k ROM bank for 8000-BFFF */
548         sms_state.banking_bios[4] = SOURCE_BIOS;
549         if ( IS_GAMEGEAR )
700   case 1: /* Select 16k ROM bank for 0400-3FFF */
701      LOG(("rom 0 paged in %x.\n", page));
702      sms_state.banking_bios[2] = SOURCE_BIOS + 0x0400;
703      sms_state.banking_cart[2] = SOURCE_CART + 0x0400;
704      if (sms_state.is_gamegear)
705         SOURCE = SOURCE_CART;
706
707      memory_set_bankptr(space->machine,  2, SOURCE + 0x0400);
708      break;
709
710   case 2: /* Select 16k ROM bank for 4000-7FFF */
711      LOG(("rom 1 paged in %x.\n", page));
712      sms_state.banking_bios[3] = SOURCE_BIOS;
713      sms_state.banking_cart[3] = SOURCE_CART;
714      if (sms_state.is_gamegear)
715         SOURCE = SOURCE_CART;
716
717      memory_set_bankptr(space->machine,  3, SOURCE);
718      break;
719
720   case 3: /* Select 16k ROM bank for 8000-BFFF */
721      sms_state.banking_bios[4] = SOURCE_BIOS;
722      if (sms_state.is_gamegear)
723         SOURCE = SOURCE_CART;
724
725      if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
726      {
727         if (SOURCE == SOURCE_CART)
550728         {
551            SOURCE = SOURCE_CART;
729            SOURCE = sms_state.banking_cart[4];
552730         }
553         if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER )
554         {
555            if ( SOURCE == SOURCE_CART )
556            {
557               SOURCE = sms_state.banking_cart[4];
558            }
559         }
560         else
561         {
562            sms_state.banking_cart[4] = SOURCE_CART;
563         }
564         if ( ! ( sms_state.mapper[0] & 0x08 ) ) /* is RAM disabled? */
565         {
566            LOG(("rom 2 paged in %x.\n", page));
567            memory_set_bankptr(space->machine,  4, SOURCE );
568            memory_set_bankptr(space->machine,  5, SOURCE + 0x2000 );
569         }
570         break;
731      }
732      else
733      {
734         sms_state.banking_cart[4] = SOURCE_CART;
735      }
736
737      if (!(sms_state.mapper[0] & 0x08)) /* is RAM disabled? */
738      {
739         LOG(("rom 2 paged in %x.\n", page));
740         memory_set_bankptr(space->machine,  4, SOURCE);
741         memory_set_bankptr(space->machine,  5, SOURCE + 0x2000);
742      }
743      break;
571744   }
572745}
573746
574747
575static WRITE8_HANDLER(sms_codemasters_page0_w)
748static WRITE8_HANDLER( sms_codemasters_page0_w )
576749{
577   if ( sms_state.cartridge[sms_state.current_cartridge].ROM && sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER )
750   if (sms_state.cartridge[sms_state.current_cartridge].ROM && sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
578751   {
579752      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
580      sms_state.banking_cart[1] = sms_state.cartridge[sms_state.current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000;
753      sms_state.banking_cart[1] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
581754      sms_state.banking_cart[2] = sms_state.banking_cart[1] + 0x0400;
582      memory_set_bankptr(space->machine, 1, sms_state.banking_cart[1] );
583      memory_set_bankptr(space->machine,  2, sms_state.banking_cart[2] );
755      memory_set_bankptr(space->machine, 1, sms_state.banking_cart[1]);
756      memory_set_bankptr(space->machine, 2, sms_state.banking_cart[2]);
584757   }
585758}
586759
587760
588static WRITE8_HANDLER(sms_codemasters_page1_w)
761static WRITE8_HANDLER( sms_codemasters_page1_w )
589762{
590   if ( sms_state.cartridge[sms_state.current_cartridge].ROM && sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER )
763   if (sms_state.cartridge[sms_state.current_cartridge].ROM && sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
591764   {
592765      /* Check if we need to switch in some RAM */
593      if ( data & 0x80 )
766      if (data & 0x80)
594767      {
595768         sms_state.cartridge[sms_state.current_cartridge].ram_page = data & 0x07;
596         memory_set_bankptr(space->machine, 5, sms_state.cartridge[sms_state.current_cartridge].cartRAM + sms_state.cartridge[sms_state.current_cartridge].ram_page * 0x2000 );
769         memory_set_bankptr(space->machine, 5, sms_state.cartridge[sms_state.current_cartridge].cartRAM + sms_state.cartridge[sms_state.current_cartridge].ram_page * 0x2000);
597770      }
598771      else
599772      {
600773         UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
601         sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000;
602         memory_set_bankptr(space->machine,  3, sms_state.banking_cart[3] );
603         memory_set_bankptr(space->machine,  5, sms_state.banking_cart[4] + 0x2000 );
774         sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
775         memory_set_bankptr(space->machine, 3, sms_state.banking_cart[3]);
776         memory_set_bankptr(space->machine, 5, sms_state.banking_cart[4] + 0x2000);
604777      }
605778   }
606779}
607780
608781
609WRITE8_HANDLER(sms_bios_w)
782WRITE8_HANDLER( sms_bios_w )
610783{
611   sms_state.biosPort = data;
784   sms_state.bios_port = data;
612785
613786   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(space->cpu));
614787
r6078r6079
616789}
617790
618791
619WRITE8_HANDLER(sms_cartram2_w)
792WRITE8_HANDLER( sms_cartram2_w )
620793{
621794   if (sms_state.mapper[0] & 0x08)
622795   {
r6078r6079
630803         sms_state.cartridge[sms_state.current_cartridge].cartSRAM[offset + 0x2000] = data;
631804      }
632805   }
633   if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER )
806   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
634807   {
635808      sms_state.cartridge[sms_state.current_cartridge].cartRAM[sms_state.cartridge[sms_state.current_cartridge].ram_page * 0x2000 + offset] = data;
636809   }
637   if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_KOREAN_MAPPER && offset == 0 ) /* Dodgeball King mapper */
810   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
638811   {
639      UINT8   rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
640      int      page = (rom_page_count > 0) ? data % rom_page_count : 0;
641      if ( ! sms_state.cartridge[sms_state.current_cartridge].ROM )
812      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
813      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
814
815      if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
642816         return;
817
643818      sms_state.banking_cart[4] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x4000;
644      memory_set_bankptr(space->machine,  4, sms_state.banking_cart[4] );
645      memory_set_bankptr(space->machine,  5, sms_state.banking_cart[4] + 0x2000 );
819      memory_set_bankptr(space->machine, 4, sms_state.banking_cart[4]);
820      memory_set_bankptr(space->machine, 5, sms_state.banking_cart[4] + 0x2000);
646821      LOG(("rom 2 paged in %x dodgeball king.\n", page));
647822   }
648823}
649824
650825
651WRITE8_HANDLER(sms_cartram_w)
826WRITE8_HANDLER( sms_cartram_w )
652827{
653828   int page;
654829
r6078r6079
666841   }
667842   else
668843   {
669      if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0 ) /* Codemasters mapper */
844      if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
670845      {
671         UINT8   rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
846         UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
672847         page = (rom_page_count > 0) ? data % rom_page_count : 0;
673         if ( ! sms_state.cartridge[sms_state.current_cartridge].ROM )
848         if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
674849            return;
675850         sms_state.banking_cart[4] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x4000;
676         memory_set_bankptr(space->machine,  4, sms_state.banking_cart[4] );
677         memory_set_bankptr(space->machine,  5, sms_state.banking_cart[4] + 0x2000 );
851         memory_set_bankptr(space->machine, 4, sms_state.banking_cart[4]);
852         memory_set_bankptr(space->machine, 5, sms_state.banking_cart[4] + 0x2000);
678853         LOG(("rom 2 paged in %x codemasters.\n", page));
679854      }
680      else if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_ONCART_RAM )
855      else if (sms_state.cartridge[sms_state.current_cartridge].features & CF_ONCART_RAM)
681856      {
682         sms_state.cartridge[sms_state.current_cartridge].cartRAM[offset & ( sms_state.cartridge[sms_state.current_cartridge].ram_size - 1 ) ] = data;
857         sms_state.cartridge[sms_state.current_cartridge].cartRAM[offset & (sms_state.cartridge[sms_state.current_cartridge].ram_size - 1)] = data;
683858      }
684859      else
685860      {
r6078r6079
689864}
690865
691866
692WRITE8_HANDLER(gg_sio_w)
867WRITE8_HANDLER( gg_sio_w )
693868{
694869   logerror("*** write %02X to SIO register #%d\n", data, offset);
695870
696   sms_state.ggSIO[offset & 0x07] = data;
697   switch(offset & 7)
871   sms_state.gg_sio[offset & 0x07] = data;
872   switch (offset & 7)
698873   {
699874      case 0x00: /* Parallel Data */
700875         break;
r6078r6079
714889}
715890
716891
717 READ8_HANDLER(gg_sio_r)
892READ8_HANDLER( gg_sio_r )
718893{
719894   logerror("*** read SIO register #%d\n", offset);
720895
721   switch(offset & 7)
896   switch (offset & 7)
722897   {
723898      case 0x00: /* Parallel Data */
724899         break;
r6078r6079
736911         break;
737912   }
738913
739   return sms_state.ggSIO[offset];
914   return sms_state.gg_sio[offset];
740915}
741916
742917
743 READ8_HANDLER(gg_psg_r)
918READ8_HANDLER( gg_psg_r )
744919{
745   return 0xFF;
920   return 0xff;
746921}
747922
748923
749WRITE8_HANDLER(gg_psg_w)
924WRITE8_HANDLER( gg_psg_w )
750925{
751926   logerror("write %02X to psg at offset #%d.\n",data , offset);
752927
r6078r6079
762937}
763938
764939
765static void sms_machine_stop(running_machine *machine)
940static void sms_machine_stop( running_machine *machine )
766941{
767942   /* Does the cartridge have SRAM that should be saved? */
768   if ( sms_state.cartridge[sms_state.current_cartridge].sram_save )
943   if (sms_state.cartridge[sms_state.current_cartridge].sram_save)
769944      image_battery_save(devtag_get_device(machine, "cart1"), sms_state.cartridge[sms_state.current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
770945}
771946
772947
773static void setup_rom(const address_space *space)
948static void setup_rom( const address_space *space )
774949{
775950   running_machine *machine = space->machine;
776951
777952   /* 1. set up bank pointers to point to nothing */
778   memory_set_bankptr(machine,  1, sms_state.banking_none[1] );
779   memory_set_bankptr(machine,  2, sms_state.banking_none[2] );
780   memory_set_bankptr(machine,  3, sms_state.banking_none[3] );
781   memory_set_bankptr(machine,  4, sms_state.banking_none[4] );
782   memory_set_bankptr(machine,  5, sms_state.banking_none[4] + 0x2000 );
953   memory_set_bankptr(machine, 1, sms_state.banking_none[1]);
954   memory_set_bankptr(machine, 2, sms_state.banking_none[2]);
955   memory_set_bankptr(machine, 3, sms_state.banking_none[3]);
956   memory_set_bankptr(machine, 4, sms_state.banking_none[4]);
957   memory_set_bankptr(machine, 5, sms_state.banking_none[4] + 0x2000);
783958
784959   /* 2. check and set up expansion port */
785   if (!(sms_state.biosPort & IO_EXPANSION) && (sms_state.biosPort & IO_CARTRIDGE) && (sms_state.biosPort & IO_CARD))
960   if (!(sms_state.bios_port & IO_EXPANSION) && (sms_state.bios_port & IO_CARTRIDGE) && (sms_state.bios_port & IO_CARD))
786961   {
787962      /* TODO: Implement me */
788      logerror( "Switching to unsupported expansion port.\n" );
963      logerror("Switching to unsupported expansion port.\n");
789964   }
790965
791966   /* 3. check and set up card rom */
792   if (!(sms_state.biosPort & IO_CARD) && (sms_state.biosPort & IO_CARTRIDGE) && (sms_state.biosPort & IO_EXPANSION))
967   if (!(sms_state.bios_port & IO_CARD) && (sms_state.bios_port & IO_CARTRIDGE) && (sms_state.bios_port & IO_EXPANSION))
793968   {
794969      /* TODO: Implement me */
795      logerror( "Switching to unsupported card rom port.\n" );
970      logerror("Switching to unsupported card rom port.\n");
796971   }
797972
798973   /* 4. check and set up cartridge rom */
799   /* if ( ( !(sms_state.biosPort & IO_CARTRIDGE) && (sms_state.biosPort & IO_EXPANSION) && (sms_state.biosPort & IO_CARD) ) || IS_GAMEGEAR ) { */
974   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || sms_state.is_gamegear) { */
800975   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
801   if ( ( ! ( sms_state.biosPort & IO_CARTRIDGE ) ) || IS_GAMEGEAR )
976   if ((!(sms_state.bios_port & IO_CARTRIDGE)) || sms_state.is_gamegear)
802977   {
803      memory_set_bankptr(machine,  1, sms_state.banking_cart[1] );
804      memory_set_bankptr(machine,  2, sms_state.banking_cart[2] );
805      memory_set_bankptr(machine,  3, sms_state.banking_cart[3] );
806      memory_set_bankptr(machine,  4, sms_state.banking_cart[4] );
807      memory_set_bankptr(machine,  5, sms_state.banking_cart[4] + 0x2000 );
808      logerror( "Switched in cartridge rom.\n" );
978      memory_set_bankptr(machine, 1, sms_state.banking_cart[1]);
979      memory_set_bankptr(machine, 2, sms_state.banking_cart[2]);
980      memory_set_bankptr(machine, 3, sms_state.banking_cart[3]);
981      memory_set_bankptr(machine, 4, sms_state.banking_cart[4]);
982      memory_set_bankptr(machine, 5, sms_state.banking_cart[4] + 0x2000);
983      logerror("Switched in cartridge rom.\n");
809984   }
810985
811986   /* 5. check and set up bios rom */
812   if ( !(sms_state.biosPort & IO_BIOS_ROM) )
987   if (!(sms_state.bios_port & IO_BIOS_ROM))
813988   {
814989      /* 0x0400 bioses */
815      if ( HAS_BIOS_0400 )
990      if (sms_state.has_bios_0400)
816991      {
817         memory_set_bankptr(machine,  1, sms_state.banking_bios[1] );
818         logerror( "Switched in 0x0400 bios.\n" );
992         memory_set_bankptr(machine, 1, sms_state.banking_bios[1]);
993         logerror("Switched in 0x0400 bios.\n");
819994      }
820995      /* 0x2000 bioses */
821      if ( HAS_BIOS_2000 )
996      if (sms_state.has_bios_2000)
822997      {
823         memory_set_bankptr(machine,  1, sms_state.banking_bios[1] );
824         memory_set_bankptr(machine,  2, sms_state.banking_bios[2] );
825         logerror( "Switched in 0x2000 bios.\n" );
998         memory_set_bankptr(machine, 1, sms_state.banking_bios[1]);
999         memory_set_bankptr(machine, 2, sms_state.banking_bios[2]);
1000         logerror("Switched in 0x2000 bios.\n");
8261001      }
827      if ( HAS_BIOS_FULL )
1002      if (sms_state.has_bios_full)
8281003      {
829         memory_set_bankptr(machine,  1, sms_state.banking_bios[1] );
830         memory_set_bankptr(machine,  2, sms_state.banking_bios[2] );
831         memory_set_bankptr(machine,  3, sms_state.banking_bios[3] );
832         memory_set_bankptr(machine,  4, sms_state.banking_bios[4] );
833         memory_set_bankptr(machine,  5, sms_state.banking_bios[4] + 0x2000 );
834         logerror( "Switched in full bios.\n" );
1004         memory_set_bankptr(machine, 1, sms_state.banking_bios[1]);
1005         memory_set_bankptr(machine, 2, sms_state.banking_bios[2]);
1006         memory_set_bankptr(machine, 3, sms_state.banking_bios[3]);
1007         memory_set_bankptr(machine, 4, sms_state.banking_bios[4]);
1008         memory_set_bankptr(machine, 5, sms_state.banking_bios[4] + 0x2000);
1009         logerror("Switched in full bios.\n");
8351010      }
8361011   }
8371012
838   if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_ONCART_RAM )
1013   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_ONCART_RAM)
8391014   {
840      memory_set_bankptr(machine,  4, sms_state.cartridge[sms_state.current_cartridge].cartRAM );
841      memory_set_bankptr(machine,  5, sms_state.cartridge[sms_state.current_cartridge].cartRAM );
1015      memory_set_bankptr(machine, 4, sms_state.cartridge[sms_state.current_cartridge].cartRAM);
1016      memory_set_bankptr(machine, 5, sms_state.cartridge[sms_state.current_cartridge].cartRAM);
8421017   }
8431018}
8441019
8451020
846static int sms_verify_cart(UINT8 *magic, int size)
1021static int sms_verify_cart( UINT8 *magic, int size )
8471022{
8481023   int retval;
8491024
r6078r6079
8521027   /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */
8531028   if (size >= 0x8000)
8541029   {
855      if (!strncmp((char*)&magic[0x7FF0], "TMR SEGA", 8))
1030      if (!strncmp((char*)&magic[0x7ff0], "TMR SEGA", 8))
8561031      {
8571032         /* Technically, it should be this, but remove for now until verified:
8581033            if (!strcmp(sysname, "gamegear"))
8591034            {
860                if ((unsigned char)magic[0x7FFD] < 0x50)
1035                if ((unsigned char)magic[0x7ffd] < 0x50)
8611036                    retval = IMAGE_VERIFY_PASS;
8621037            }
8631038            if (!strcmp(sysname, "sms"))
8641039            {
865                if ((unsigned char)magic[0x7FFD] >= 0x50)
1040                if ((unsigned char)magic[0x7ffd] >= 0x50)
8661041                    retval = IMAGE_VERIFY_PASS;
8671042            }
8681043            */
r6078r6079
8741049      //if (!retval) {
8751050    //  if (!strncmp(&magic[0x81f0], "TMR SEGA", 8)) {
8761051            /* Technically, it should be this, but remove for now until verified:
877                if (!strcmp(sysname, "gamegear")) {
1052                if (!strcmp(sysname, "gamegear"))
1053         {
8781054                    if ((unsigned char)magic[0x81fd] < 0x50)
8791055                        retval = IMAGE_VERIFY_PASS;
8801056                }
881                if (!strcmp(sysname, "sms")) {
1057                if (!strcmp(sysname, "sms"))
1058         {
8821059                    if ((unsigned char)magic[0x81fd] >= 0x50)
8831060                        retval = IMAGE_VERIFY_PASS;
8841061                }
r6078r6079
9081085 */
9091086static int detect_codemasters_mapper( UINT8 *rom )
9101087{
911   static const UINT8 jang_pung2[16] = { 0x00, 0xBA, 0x38, 0x0D, 0x00, 0xB8, 0x38, 0x0C, 0x00, 0xB6, 0x38, 0x0B, 0x00, 0xB4, 0x38, 0x0A };
1088   static const UINT8 jang_pung2[16] = { 0x00, 0xba, 0x38, 0x0d, 0x00, 0xb8, 0x38, 0x0c, 0x00, 0xb6, 0x38, 0x0b, 0x00, 0xb4, 0x38, 0x0a };
9121089
913   if ( ( ( rom[0x7fe0] & 0x0F ) <= 9 ) &&
914        ( rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95 ) &&
915        rom[0x7fef] == 0x00 )
916   {
1090   if (((rom[0x7fe0] & 0x0f ) <= 9) && (rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95) &&  rom[0x7fef] == 0x00)
9171091      return 1;
918   }
9191092
920   if ( ! memcmp( &rom[0x7ff0], jang_pung2, 16 ) )
921   {
1093   if (!memcmp(&rom[0x7ff0], jang_pung2, 16))
9221094      return 1;
923   }
9241095
9251096   return 0;
9261097}
r6078r6079
9301101{
9311102   static const UINT8 signatures[2][16] =
9321103   {
933      { 0x3E, 0x11, 0x32, 0x00, 0xA0, 0x78, 0xCD, 0x84, 0x85, 0x3E, 0x02, 0x32, 0x00, 0xA0, 0xC9, 0xFF }, /* Dodgeball King */
934      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4E, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
1104      { 0x3e, 0x11, 0x32, 0x00, 0xa0, 0x78, 0xcd, 0x84, 0x85, 0x3e, 0x02, 0x32, 0x00, 0xa0, 0xc9, 0xff }, /* Dodgeball King */
1105      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
9351106   };
9361107   int i;
9371108
938   for( i = 0; i < 2; i++ )
1109   for (i = 0; i < 2; i++)
9391110   {
940      if ( !memcmp( &rom[0x7ff0], signatures[i], 16 ) )
1111      if (!memcmp(&rom[0x7ff0], signatures[i], 16))
9411112      {
9421113         return 1;
9431114      }
r6078r6079
9451116   return 0;
9461117}
9471118
1119static UINT8 detect_lphaser_xoffset( UINT8 *rom )
1120{
1121   static const UINT8 signatures[6][16] =
1122   {
1123      /* Spacegun */
1124      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9d, 0x99, 0x10, 0x90, 0x00, 0x40 },
1125      /* Gangster Town */
1126      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x19, 0x87, 0x1b, 0xc9, 0x74, 0x50, 0x00, 0x4f },
1127      /* Shooting Gallery */
1128      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0x8a, 0x3a, 0x72, 0x50, 0x00, 0x4f },
1129      /* Rescue Mission */
1130      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0xfb, 0xd3, 0x06, 0x51, 0x00, 0x4f },
1131      /* Laser Ghost */
1132      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x00, 0x00, 0xb7, 0x55, 0x74, 0x70, 0x00, 0x40 },
1133      /* Assault City */
1134      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9f, 0x74, 0x34, 0x70, 0x00, 0x40 },
1135   };
1136   
1137   if (!(sms_state.bios_port & IO_CARTRIDGE) && sms_state.cartridge[sms_state.current_cartridge].size >= 0x8000)
1138   {
1139      if (!memcmp(&rom[0x7ff0], signatures[0], 16) || !memcmp(&rom[0x7ff0], signatures[1], 16))
1140         return 40;
1141     
1142      if (!memcmp(&rom[0x7ff0], signatures[2], 16))
1143         return 49;
1144     
1145      if (!memcmp(&rom[0x7ff0], signatures[3], 16))
1146         return 47;
1147     
1148      if (!memcmp(&rom[0x7ff0], signatures[4], 16))
1149         return 44;
1150     
1151      if (!memcmp(&rom[0x7ff0], signatures[5], 16))
1152         return 53;
1153     
1154   }
1155   return 50;   
1156}
9481157
1158
9491159DEVICE_START( sms_cart )
9501160{
9511161   int i;
9521162
953   for ( i = 0; i < MAX_CARTRIDGES; i++ )
1163   for (i = 0; i < MAX_CARTRIDGES; i++)
9541164   {
9551165      sms_state.cartridge[i].ROM = NULL;
9561166      sms_state.cartridge[i].size = 0;
r6078r6079
9631173   }
9641174   sms_state.current_cartridge = 0;
9651175
966   sms_state.biosPort = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
967   if ( ! IS_GAMEGEAR && ! HAS_BIOS )
1176   sms_state.bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1177   if (!sms_state.is_gamegear && ! sms_state.has_bios)
9681178   {
969      sms_state.biosPort &= ~(IO_CARTRIDGE);
970      sms_state.biosPort |= IO_BIOS_ROM;
1179      sms_state.bios_port &= ~(IO_CARTRIDGE);
1180      sms_state.bios_port |= IO_BIOS_ROM;
9711181   }
9721182}
9731183
r6078r6079
9761186{
9771187   int size = image_length(image);
9781188   int index = 0;
979   const char *fname = image_filename( image );
980   int fname_len = fname ? strlen( fname ) : 0;
981   const char *extrainfo = image_extrainfo( image );
1189   const char *fname = image_filename(image);
1190   int fname_len = fname ? strlen(fname) : 0;
1191   const char *extrainfo = image_extrainfo(image);
9821192
983   if (strcmp(image->tag,"cart1")==0)
984   {
1193   if (strcmp(image->tag,"cart1") == 0)
9851194      index = 0;
986   }
987   if (strcmp(image->tag,"cart2")==0)
988   {
1195
1196   if (strcmp(image->tag,"cart2") == 0)
9891197      index = 1;
990   }
991   if (strcmp(image->tag,"cart3")==0)
992   {
1198
1199   if (strcmp(image->tag,"cart3") == 0)
9931200      index = 2;
994   }
995   if (strcmp(image->tag,"cart4")==0)
996   {
1201
1202   if (strcmp(image->tag,"cart4") == 0)
9971203      index = 3;
998   }
999   if (strcmp(image->tag,"cart5")==0)
1000   {
1204
1205   if (strcmp(image->tag,"cart5") == 0)
10011206      index = 4;
1002   }
1207
10031208   /* Check for 512-byte header */
10041209   if ((size / 512) & 1)
10051210   {
r6078r6079
10071212      size -= 512;
10081213   }
10091214
1010   if ( ! size )
1215   if (!size)
10111216   {
1012      image_seterror( image, IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small" );
1217      image_seterror(image, IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small");
10131218      return INIT_FAIL;
10141219   }
10151220
10161221   /* Create a new memory region to hold the ROM. */
10171222   /* Make sure the region holds only complete (0x4000) rom banks */
1018   sms_state.cartridge[index].size = (size&0x3FFF) ? (((size>>14)+1)<<14) : size;
1019   sms_state.cartridge[index].ROM = auto_alloc_array(image->machine, UINT8, sms_state.cartridge[index].size );
1223   sms_state.cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1224   sms_state.cartridge[index].ROM = auto_alloc_array(image->machine, UINT8, sms_state.cartridge[index].size);
10201225
1021   sms_state.cartridge[index].cartSRAM = auto_alloc_array(image->machine, UINT8, NVRAM_SIZE );
1226   sms_state.cartridge[index].cartSRAM = auto_alloc_array(image->machine, UINT8, NVRAM_SIZE);
10221227
10231228   /* Load ROM banks */
10241229   size = image_fread(image, sms_state.cartridge[index].ROM, size);
10251230
10261231   /* check the image */
1027   if ( ! HAS_BIOS )
1232   if (!sms_state.has_bios)
10281233   {
10291234      if (sms_verify_cart(sms_state.cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
10301235      {
r6078r6079
10351240   sms_state.cartridge[index].features = 0;
10361241
10371242   /* Detect special features from the extrainfo field */
1038   if ( extrainfo )
1243   if (extrainfo)
10391244   {
10401245      /* Check for codemasters mapper */
1041      if ( strstr( extrainfo, "CODEMASTERS" ) )
1042      {
1246      if (strstr(extrainfo, "CODEMASTERS"))
10431247         sms_state.cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1044      }
10451248
10461249      /* Check for korean mapper */
1047      if ( strstr( extrainfo, "KOREAN" ) )
1048      {
1250      if (strstr(extrainfo, "KOREAN"))
10491251         sms_state.cartridge[index].features |= CF_KOREAN_MAPPER;
1050      }
10511252
10521253      /* Check for special SMS Compatibility mode gamegear cartridges */
1053      if ( IS_GAMEGEAR && strstr( extrainfo, "GGSMS" ) )
1054      {
1254      if (sms_state.is_gamegear && strstr(extrainfo, "GGSMS"))
10551255         sms_state.cartridge[index].features |= CF_GG_SMS_MODE;
1056      }
10571256
10581257      /* Check for 93C46 eeprom */
1059      if ( strstr( extrainfo, "93C46" ) )
1060      {
1258      if (strstr( extrainfo, "93C46"))
10611259         sms_state.cartridge[index].features |= CF_93C46_EEPROM;
1062      }
10631260
10641261      /* Check for 8KB on-cart RAM */
1065      if ( strstr( extrainfo, "8KB_CART_RAM" ) )
1262      if (strstr(extrainfo, "8KB_CART_RAM"))
10661263       {
10671264         sms_state.cartridge[index].features |= CF_ONCART_RAM;
10681265         sms_state.cartridge[index].ram_size = 0x2000;
1069         sms_state.cartridge[index].cartRAM = auto_alloc_array(image->machine, UINT8, sms_state.cartridge[index].ram_size );
1266         sms_state.cartridge[index].cartRAM = auto_alloc_array(image->machine, UINT8, sms_state.cartridge[index].ram_size);
10701267      }
10711268   }
10721269   else
10731270   {
10741271      /* If no extrainfo information is available try to find special information out on our own */
10751272      /* Check for special cartridge features */
1076      if ( size >= 0x8000 )
1273      if (size >= 0x8000)
10771274      {
10781275         /* Check for special mappers */
1079         if ( detect_codemasters_mapper( sms_state.cartridge[index].ROM ) )
1080         {
1276         if (detect_codemasters_mapper(sms_state.cartridge[index].ROM))
10811277            sms_state.cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1082         }
1083         if ( detect_korean_mapper( sms_state.cartridge[index].ROM ) )
1084         {
1278
1279         if (detect_korean_mapper(sms_state.cartridge[index].ROM))
10851280            sms_state.cartridge[index].features |= CF_KOREAN_MAPPER;
1086         }
1281
10871282      }
10881283
10891284      /* Check for special SMS Compatibility mode gamegear cartridges */
1090      if ( IS_GAMEGEAR )
1285      if (sms_state.is_gamegear)
10911286      {
10921287         /* Just in case someone passes us an sms file */
1093         if ( fname_len > 3 && ! mame_stricmp( fname + fname_len - 3, "sms" ) )
1094         {
1288         if (fname_len > 3 && ! mame_stricmp(fname + fname_len - 3, "sms"))
10951289            sms_state.cartridge[index].features |= CF_GG_SMS_MODE;
1096         }
10971290      }
10981291   }
10991292
1100   if ( sms_state.cartridge[index].features & CF_CODEMASTERS_MAPPER )
1293   if (sms_state.cartridge[index].features & CF_CODEMASTERS_MAPPER)
11011294   {
11021295      sms_state.cartridge[index].ram_size = 0x10000;
1103      sms_state.cartridge[index].cartRAM = auto_alloc_array(image->machine, UINT8, sms_state.cartridge[index].ram_size );
1296      sms_state.cartridge[index].cartRAM = auto_alloc_array(image->machine, UINT8, sms_state.cartridge[index].ram_size);
11041297      sms_state.cartridge[index].ram_page = 0;
11051298   }
1299   
1300   /* For Light Phaser games, we have to detect the x offset */
1301   sms_state.lphaser_x_offs = detect_lphaser_xoffset(sms_state.cartridge[index].ROM);
11061302
11071303   /* Load battery backed RAM, if available */
1108   image_battery_load( image, sms_state.cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
1304   image_battery_load(image, sms_state.cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE);
11091305
11101306   return INIT_PASS;
11111307}
r6078r6079
11131309
11141310static void setup_cart_banks( void )
11151311{
1116   if ( sms_state.cartridge[sms_state.current_cartridge].ROM )
1312   if (sms_state.cartridge[sms_state.current_cartridge].ROM)
11171313   {
1118      UINT8   rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
1314      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
11191315      sms_state.banking_cart[1] = sms_state.cartridge[sms_state.current_cartridge].ROM;
11201316      sms_state.banking_cart[2] = sms_state.cartridge[sms_state.current_cartridge].ROM + 0x0400;
1121      sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + ( ( 1 < rom_page_count ) ? 0x4000 : 0 );
1122      sms_state.banking_cart[4] = sms_state.cartridge[sms_state.current_cartridge].ROM + ( ( 2 < rom_page_count ) ? 0x8000 : 0 );
1317      sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1318      sms_state.banking_cart[4] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
11231319      /* Codemasters mapper points to bank 0 for page 2 */
1124      if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER ) {
1320      if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
1321      {
11251322         sms_state.banking_cart[4] = sms_state.cartridge[sms_state.current_cartridge].ROM;
11261323      }
11271324   }
r6078r6079
11451342
11461343   sms_state.BIOS = memory_region(machine, "user1");
11471344
1148   sms_state.BiosPageCount = ( sms_state.BIOS ? memory_region_length(machine, "user1") / 0x4000 : 0 );
1345   sms_state.bios_page_count = (sms_state.BIOS ? memory_region_length(machine, "user1") / 0x4000 : 0);
11491346
11501347   setup_cart_banks();
11511348
1152   if ( sms_state.BIOS == NULL || sms_state.BIOS[0] == 0x00 )
1349   if (sms_state.BIOS == NULL || sms_state.BIOS[0] == 0x00)
11531350   {
11541351      sms_state.BIOS = NULL;
1155                sms_state.biosPort |= IO_BIOS_ROM;
1352      sms_state.bios_port |= IO_BIOS_ROM;
11561353   }
11571354
1158   if ( sms_state.BIOS )
1355   if (sms_state.BIOS)
11591356   {
11601357      sms_state.banking_bios[1] = sms_state.BIOS;
11611358      sms_state.banking_bios[2] = sms_state.BIOS + 0x0400;
1162      sms_state.banking_bios[3] = sms_state.BIOS + ( ( 1 < sms_state.BiosPageCount) ? 0x4000 : 0 );
1163      sms_state.banking_bios[4] = sms_state.BIOS + ( ( 2 < sms_state.BiosPageCount) ? 0x8000 : 0 );
1359      sms_state.banking_bios[3] = sms_state.BIOS + ((1 < sms_state.bios_page_count) ? 0x4000 : 0);
1360      sms_state.banking_bios[4] = sms_state.BIOS + ((2 < sms_state.bios_page_count) ? 0x8000 : 0);
11641361   }
11651362}
11661363
11671364
1168MACHINE_START(sms)
1365MACHINE_START( sms )
11691366{
11701367   add_exit_callback(machine, sms_machine_stop);
11711368}
11721369
11731370
1174MACHINE_RESET(sms)
1371MACHINE_RESET( sms )
11751372{
11761373   const address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1374   const device_config *smsvdp = devtag_get_device(machine, "sms_vdp");
11771375
1178   sms_state.Version = 0x00;
1179   if ( HAS_FM )
1180   {
1181      sms_state.FMDetect = 0x01;
1182   }
1376   sms_state.ctrl_reg = 0xff;
1377   if (sms_state.has_fm)
1378      sms_state.fm_detect = 0x01;
11831379
1184   sms_state.mapper_ram = memory_get_write_ptr( space, 0xDFFC );
1380   sms_state.mapper_ram = memory_get_write_ptr(space, 0xdffc);
11851381
1186   sms_state.biosPort = 0;
1382   sms_state.bios_port = 0;
11871383
1188   if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER )
1384   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
11891385   {
11901386      /* Install special memory handlers */
1191      memory_install_write8_handler(space, 0x0000, 0x0000, 0, 0, sms_codemasters_page0_w );
1192      memory_install_write8_handler(space, 0x4000, 0x4000, 0, 0, sms_codemasters_page1_w );
1387      memory_install_write8_handler(space, 0x0000, 0x0000, 0, 0, sms_codemasters_page0_w);
1388      memory_install_write8_handler(space, 0x4000, 0x4000, 0, 0, sms_codemasters_page1_w);
11931389   }
11941390
1195   if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_GG_SMS_MODE )
1196   {
1197      sms_set_ggsmsmode( 1 );
1198   }
1391   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_GG_SMS_MODE)
1392      sms_set_ggsmsmode(smsvdp, 1);
11991393
12001394   /* Initialize SIO stuff for GG */
1201   sms_state.ggSIO[0] = 0x7F;
1202   sms_state.ggSIO[1] = 0xFF;
1203   sms_state.ggSIO[2] = 0x00;
1204   sms_state.ggSIO[3] = 0xFF;
1205   sms_state.ggSIO[4] = 0x00;
1395   sms_state.gg_sio[0] = 0x7f;
1396   sms_state.gg_sio[1] = 0xff;
1397   sms_state.gg_sio[2] = 0x00;
1398   sms_state.gg_sio[3] = 0xff;
1399   sms_state.gg_sio[4] = 0x00;
12061400
12071401   sms_state.store_control = 0;
12081402
r6078r6079
12121406
12131407   sms_state.rapid_fire_state_1 = 0;
12141408   sms_state.rapid_fire_state_2 = 0;
1215   sms_state.rapid_fire_timer = timer_alloc(machine, rapid_fire_callback , NULL);
1409   sms_state.rapid_fire_timer = timer_alloc(machine, rapid_fire_callback , NULL);
12161410   timer_adjust_periodic(sms_state.rapid_fire_timer, ATTOTIME_IN_HZ(10), 0, ATTOTIME_IN_HZ(10));
12171411
12181412   sms_state.last_paddle_read_time = 0;
r6078r6079
12281422   sms_state.sports_pad_1_y = 0;
12291423   sms_state.sports_pad_2_x = 0;
12301424   sms_state.sports_pad_2_y = 0;
1425
1426   sms_state.lphaser_latch = 0;
1427   sms_state.lphaser_1_x = 0;
1428   sms_state.lphaser_1_y = 0;
1429   sms_state.lphaser_2_x = 0;
1430   sms_state.lphaser_2_x = 0;
1431
1432   /* Check if lightgun has been chosen as input: if so, enable crosshair */
1433   timer_set(space->machine, attotime_zero, NULL, 0, lightgun_tick);
12311434}
12321435
12331436
1234READ8_HANDLER(sms_store_cart_select_r)
1437READ8_HANDLER( sms_store_cart_select_r )
12351438{
1236   return 0xFF;
1439   return 0xff;
12371440}
12381441
12391442
1240WRITE8_HANDLER(sms_store_cart_select_w)
1443WRITE8_HANDLER( sms_store_cart_select_w )
12411444{
12421445   UINT8 slot = data >> 4;
12431446   UINT8 slottype = data & 0x08;
12441447
12451448   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
12461449   /* cartridge? slot #0 */
1247   if ( slottype == 0 )
1248   {
1450   if (slottype == 0)
12491451      sms_state.current_cartridge = slot;
1250   }
1452
12511453   setup_cart_banks();
1252   memory_set_bankptr(space->machine, 10, sms_state.banking_cart[3] + 0x2000 );
1454   memory_set_bankptr(space->machine, 10, sms_state.banking_cart[3] + 0x2000);
12531455   setup_rom(space);
12541456}
12551457
12561458
1257READ8_HANDLER(sms_store_select1)
1459READ8_HANDLER( sms_store_select1 )
12581460{
1259   return 0xFF;
1461   return 0xff;
12601462}
12611463
12621464
1263READ8_HANDLER(sms_store_select2)
1465READ8_HANDLER( sms_store_select2 )
12641466{
1265   return 0xFF;
1467   return 0xff;
12661468}
12671469
12681470
1269READ8_HANDLER(sms_store_control_r)
1471READ8_HANDLER( sms_store_control_r )
12701472{
12711473   return sms_state.store_control;
12721474}
12731475
12741476
1275WRITE8_HANDLER(sms_store_control_w)
1477WRITE8_HANDLER( sms_store_control_w )
12761478{
1277   logerror( "0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(space->cpu), data );
1278   if ( data & 0x02 )
1479   logerror("0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(space->cpu), data);
1480   if (data & 0x02)
12791481   {
1280      cputag_resume( space->machine, "maincpu", SUSPEND_REASON_HALT );
1482      cputag_resume(space->machine, "maincpu", SUSPEND_REASON_HALT);
12811483   }
12821484   else
12831485   {
12841486      /* Pull reset line of CPU #0 low */
1285      cputag_suspend( space->machine, "maincpu", SUSPEND_REASON_HALT, 1 );
1487      cputag_suspend(space->machine, "maincpu", SUSPEND_REASON_HALT, 1);
12861488      device_reset(cputag_get_cpu(space->machine, "maincpu"));
12871489   }
12881490   sms_state.store_control = data;
12891491}
12901492
1291
1292void sms_int_callback( running_machine *machine, int state )
1493void sms_store_int_callback( running_machine *machine, int state )
12931494{
1294   cputag_set_input_line(machine, "maincpu", 0, state );
1495   cputag_set_input_line(machine, "maincpu", sms_state.store_control & 0x01 ? 1 : 0, state);
12951496}
12961497
12971498
1298void sms_store_int_callback( running_machine *machine, int state )
1499static void sms_set_zero_flag( void )
12991500{
1300   cputag_set_input_line(machine, "maincpu", sms_state.store_control & 0x01 ? 1 : 0, state );
1501sms_state.is_gamegear = 0;
1502   sms_state.is_region_japan = 0;
1503   sms_state.has_bios_0400 = 0;
1504   sms_state.has_bios_2000 = 0;
1505   sms_state.has_bios_full = 0;
1506   sms_state.has_bios = 0;
1507   sms_state.has_fm = 0;   
13011508}
13021509
1303
13041510DRIVER_INIT( sg1000m3 )
13051511{
1306   sms_state.flags = FLAG_REGION_JAPAN | FLAG_FM;
1512   sms_set_zero_flag();
1513   sms_state.is_region_japan = 1;
1514   sms_state.has_fm = 1;
13071515}
13081516
13091517
13101518DRIVER_INIT( sms1 )
13111519{
1312   sms_state.flags = FLAG_BIOS_FULL;
1520   sms_set_zero_flag();
1521   sms_state.has_bios_full = 1;
13131522}
13141523
13151524
13161525DRIVER_INIT( smsj )
13171526{
1318   sms_state.flags = FLAG_REGION_JAPAN | FLAG_BIOS_2000 | FLAG_FM;
1527   sms_set_zero_flag();
1528   sms_state.is_region_japan = 1;
1529   sms_state.has_bios_2000 = 1;
1530   sms_state.has_fm = 1;
13191531}
13201532
13211533
13221534DRIVER_INIT( sms2kr )
13231535{
1324   sms_state.flags = FLAG_REGION_JAPAN | FLAG_BIOS_FULL | FLAG_FM;
1536   sms_set_zero_flag();
1537   sms_state.is_region_japan = 1;
1538   sms_state.has_bios_full = 1;
1539   sms_state.has_fm = 1;
13251540}
13261541
13271542
13281543DRIVER_INIT( smssdisp )
13291544{
1330   sms_state.flags = 0;
1545   sms_set_zero_flag();
13311546}
13321547
13331548
13341549DRIVER_INIT( gamegear )
13351550{
1336   sms_state.flags = FLAG_GAMEGEAR;
1551   sms_set_zero_flag();
1552   sms_state.is_gamegear = 1;
13371553}
13381554
13391555
13401556DRIVER_INIT( gamegeaj )
13411557{
1342   sms_state.flags = FLAG_REGION_JAPAN | FLAG_GAMEGEAR | FLAG_BIOS_0400;
1558   sms_set_zero_flag();
1559   sms_state.is_region_japan = 1;
1560   sms_state.is_gamegear = 1;
1561   sms_state.has_bios_0400 = 1;
13431562}
src/mess/drivers/sms.c
r6078r6079
9595   ADDRESS_MAP_GLOBAL_MASK(0xff)
9696   ADDRESS_MAP_UNMAP_HIGH
9797   AM_RANGE(0x00, 0x00) AM_MIRROR(0x3e) AM_WRITE(sms_bios_w)
98   AM_RANGE(0x01, 0x01) AM_MIRROR(0x3e) AM_WRITE(sms_version_w)
98   AM_RANGE(0x01, 0x01) AM_MIRROR(0x3e) AM_WRITE(sms_io_control_w)
9999   AM_RANGE(0x40, 0x7F)                 AM_READ(sms_count_r)
100100   AM_RANGE(0x40, 0x7F)                 AM_DEVWRITE("smsiii", sn76496_w)
101   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_READWRITE(sms_vdp_data_r, sms_vdp_data_w)
102   AM_RANGE(0x80, 0x81) AM_MIRROR(0x3e) AM_READWRITE(sms_vdp_ctrl_r, sms_vdp_ctrl_w)
101   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
102   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
103103   AM_RANGE(0xC0, 0xC0) AM_MIRROR(0x1e) AM_READ(sms_input_port_0_r)
104   AM_RANGE(0xC1, 0xC1) AM_MIRROR(0x1e) AM_READ(sms_version_r)
104   AM_RANGE(0xC1, 0xC1) AM_MIRROR(0x1e) AM_READ(sms_input_port_1_r)
105105   AM_RANGE(0xE0, 0xE0) AM_MIRROR(0x0e) AM_READ(sms_input_port_0_r)
106   AM_RANGE(0xE1, 0xE1) AM_MIRROR(0x0e) AM_READ(sms_version_r)
107   AM_RANGE(0xF0, 0xF0)             AM_READWRITE(sms_input_port_0_r, sms_ym2413_register_port_0_w)
108   AM_RANGE(0xF1, 0xF1)             AM_READWRITE(sms_version_r, sms_ym2413_data_port_0_w)
109   AM_RANGE(0xF2, 0xF2)             AM_READWRITE(sms_fm_detect_r, sms_fm_detect_w)
110   AM_RANGE(0xF3, 0xF3)             AM_READ(sms_version_r)
106   AM_RANGE(0xE1, 0xE1) AM_MIRROR(0x0e) AM_READ(sms_input_port_1_r)
107   AM_RANGE(0xF0, 0xF0)                 AM_READWRITE(sms_input_port_0_r, sms_ym2413_register_port_0_w)
108   AM_RANGE(0xF1, 0xF1)                 AM_READWRITE(sms_input_port_1_r, sms_ym2413_data_port_0_w)
109   AM_RANGE(0xF2, 0xF2)                 AM_READWRITE(sms_fm_detect_r, sms_fm_detect_w)
110   AM_RANGE(0xF3, 0xF3)                 AM_READ(sms_input_port_1_r)
111111   AM_RANGE(0xF4, 0xF4) AM_MIRROR(0x02) AM_READ(sms_input_port_0_r)
112   AM_RANGE(0xF5, 0xF5) AM_MIRROR(0x02) AM_READ(sms_version_r)
112   AM_RANGE(0xF5, 0xF5) AM_MIRROR(0x02) AM_READ(sms_input_port_1_r)
113113   AM_RANGE(0xF8, 0xF8) AM_MIRROR(0x06) AM_READ(sms_input_port_0_r)
114   AM_RANGE(0xF9, 0xF9) AM_MIRROR(0x06) AM_READ(sms_version_r)
114   AM_RANGE(0xF9, 0xF9) AM_MIRROR(0x06) AM_READ(sms_input_port_1_r)
115115ADDRESS_MAP_END
116116
117117static ADDRESS_MAP_START( gg_io, ADDRESS_SPACE_IO, 8 )
118118   ADDRESS_MAP_GLOBAL_MASK(0xff)
119119   ADDRESS_MAP_UNMAP_HIGH
120   AM_RANGE(0x00, 0x00)             AM_READ(gg_input_port_2_r)
121   AM_RANGE(0x01, 0x05)             AM_READWRITE(gg_sio_r, gg_sio_w)
122   AM_RANGE(0x06, 0x06)             AM_READWRITE(gg_psg_r, gg_psg_w)
123   AM_RANGE(0x07, 0x07)              AM_WRITE(sms_version_w)
120   AM_RANGE(0x00, 0x00)                 AM_READ(gg_input_port_2_r)
121   AM_RANGE(0x01, 0x05)                 AM_READWRITE(gg_sio_r, gg_sio_w)
122   AM_RANGE(0x06, 0x06)                 AM_READWRITE(gg_psg_r, gg_psg_w)
123   AM_RANGE(0x07, 0x07)                 AM_WRITE(sms_io_control_w)
124124   AM_RANGE(0x08, 0x08) AM_MIRROR(0x06) AM_WRITE(sms_bios_w)
125   AM_RANGE(0x09, 0x09) AM_MIRROR(0x06) AM_WRITE(sms_version_w)
125   AM_RANGE(0x09, 0x09) AM_MIRROR(0x06) AM_WRITE(sms_io_control_w)
126126   AM_RANGE(0x10, 0x10) AM_MIRROR(0x0e) AM_WRITE(sms_bios_w)
127   AM_RANGE(0x11, 0x11) AM_MIRROR(0x0e) AM_WRITE(sms_version_w)
127   AM_RANGE(0x11, 0x11) AM_MIRROR(0x0e) AM_WRITE(sms_io_control_w)
128128   AM_RANGE(0x20, 0x20) AM_MIRROR(0x1e) AM_WRITE(sms_bios_w)
129   AM_RANGE(0x21, 0x21) AM_MIRROR(0x1e) AM_WRITE(sms_version_w)
129   AM_RANGE(0x21, 0x21) AM_MIRROR(0x1e) AM_WRITE(sms_io_control_w)
130130   AM_RANGE(0x40, 0x7F)                 AM_READ(sms_count_r)
131131   AM_RANGE(0x40, 0x7F)                 AM_DEVWRITE("gamegear", sn76496_w)
132   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_READWRITE(sms_vdp_data_r, sms_vdp_data_w)
133   AM_RANGE(0x80, 0x81) AM_MIRROR(0x3e) AM_READWRITE(sms_vdp_ctrl_r, sms_vdp_ctrl_w)
134   AM_RANGE(0xC0, 0xC0)             AM_READ_PORT("PORT_DC")
135   AM_RANGE(0xC1, 0xC1)             AM_READ_PORT("PORT_DD")
136   AM_RANGE(0xDC, 0xDC)             AM_READ_PORT("PORT_DC")
137   AM_RANGE(0xDD, 0xDD)             AM_READ_PORT("PORT_DD")
132   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
133   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
134   AM_RANGE(0xC0, 0xC0)                 AM_READ_PORT("PORT_DC")
135   AM_RANGE(0xC1, 0xC1)                 AM_READ_PORT("PORT_DD")
136   AM_RANGE(0xDC, 0xDC)                 AM_READ_PORT("PORT_DC")
137   AM_RANGE(0xDD, 0xDD)                 AM_READ_PORT("PORT_DD")
138138ADDRESS_MAP_END
139139
140140
r6078r6079
164164   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_START ) PORT_NAME(DEF_STR(Pause))
165165
166166   PORT_START("LPHASER0")   /* Light phaser X - player 1 */
167//  PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR( X, 1.0, 0.0, 0 ) PORT_SENSITIVITY(25) PORT_KEYDELTA(15) PORT_CATEGORY(11) PORT_PLAYER(1)
167   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR( X, 1.0, 0.0, 0 ) PORT_SENSITIVITY(50) PORT_KEYDELTA(15) PORT_CATEGORY(11) PORT_PLAYER(1)
168168
169169   PORT_START("LPHASER1")   /* Light phaser Y - player 1 */
170//  PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR( Y, 1.0, 0.0, 0 ) PORT_SENSITIVITY(25) PORT_KEYDELTA(15) PORT_CATEGORY(11) PORT_PLAYER(1)
170   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR( Y, 1.0, 0.0, 0 ) PORT_SENSITIVITY(50) PORT_KEYDELTA(15) PORT_CATEGORY(11) PORT_PLAYER(1)
171171
172   PORT_START("LPHASER2")   /* Light phaser X - player 2 */
173//  PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR( X, 1.0, 0.0, 0 ) PORT_SENSITIVITY(25) PORT_KEUDELTA(15) PORT_CATEGORY(21) PORT_PLAYER(2)
172   PORT_START("LPHASER2")   /* Light phaser X - player 2 */   
173   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR( X, 1.0, 0.0, 0 ) PORT_SENSITIVITY(50) PORT_KEYDELTA(15) PORT_CATEGORY(21) PORT_PLAYER(2)
174174
175175   PORT_START("LPHASER3")   /* Light phaser Y - player 2 */
176//  PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR( Y, 1.0, 0.0, 0 ) PORT_SENSITIVITY(25) PORT_KEYDELTA(25) PORT_CATEGORY(21) PORT_PLAYER(2)
176   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR( Y, 1.0, 0.0, 0 ) PORT_SENSITIVITY(50) PORT_KEYDELTA(25) PORT_CATEGORY(21) PORT_PLAYER(2)
177177
178178   PORT_START("RFU")   /* Rapid Fire Unit */
179179   PORT_DIPNAME( 0x03, 0x00, "Rapid Fire Unit - Player 1" )
r6078r6079
204204   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(23) PORT_PLAYER(2)
205205
206206   PORT_START("CTRLSEL")   /* Controller selection */
207   PORT_CATEGORY_CLASS( 0x0F, 0x00, "Player 1 Controller" )
207   PORT_CATEGORY_CLASS( 0x0f, 0x00, "Player 1 Controller" )
208208   PORT_CATEGORY_ITEM( 0x00, DEF_STR( Joystick ), 10 )
209//  PORT_CATEGORY_ITEM( 0x01, "Light Phaser", 11 )
209   PORT_CATEGORY_ITEM( 0x01, "Light Phaser", 11 )
210210   PORT_CATEGORY_ITEM( 0x02, "Sega Paddle Control", 12 )
211211   PORT_CATEGORY_ITEM( 0x03, "Sega Sports Pad", 13 )
212   PORT_CATEGORY_CLASS( 0xF0, 0x00, "Player 2 Controller" )
212   PORT_CATEGORY_CLASS( 0xf0, 0x00, "Player 2 Controller" )
213213   PORT_CATEGORY_ITEM( 0x00, DEF_STR( Joystick ), 20 )
214//  PORT_CATEGORY_ITEM( 0x10, "Light Phaser", 21 )
214   PORT_CATEGORY_ITEM( 0x10, "Light Phaser", 21 )
215215   PORT_CATEGORY_ITEM( 0x20, "Sega Paddle Control", 22 )
216216   PORT_CATEGORY_ITEM( 0x30, "Sega Sports Pad", 23 )
217217
r6078r6079
249249static PALETTE_INIT( sms )
250250{
251251   int i;
252   for( i = 0; i < 64; i++ )
252   for (i = 0; i < 64; i++)
253253   {
254254      int r = i & 0x03;
255      int g = ( i & 0x0C ) >> 2;
256      int b = ( i & 0x30 ) >> 4;
257      palette_set_color_rgb(machine, i, r << 6, g << 6, b << 6 );
255      int g = (i & 0x0c) >> 2;
256      int b = (i & 0x30) >> 4;
257      palette_set_color_rgb(machine, i, r << 6, g << 6, b << 6);
258258   }
259259   /* TMS9918 palette */
260   palette_set_color_rgb(machine, 64+ 0,   0,   0,   0 );
261   palette_set_color_rgb(machine, 64+ 1,   0,   0,   0 );
262   palette_set_color_rgb(machine, 64+ 2,  33, 200,  66 );
263   palette_set_color_rgb(machine, 64+ 3,  94, 220, 120 );
264   palette_set_color_rgb(machine, 64+ 4,  84,  85, 237 );
265   palette_set_color_rgb(machine, 64+ 5, 125, 118, 252 );
266   palette_set_color_rgb(machine, 64+ 6, 212,  82,  77 );
267   palette_set_color_rgb(machine, 64+ 7,  66, 235, 245 );
268   palette_set_color_rgb(machine, 64+ 8, 252,  85,  84 );
269   palette_set_color_rgb(machine, 64+ 9, 255, 121, 120 );
270   palette_set_color_rgb(machine, 64+10, 212, 193,  84 );
271   palette_set_color_rgb(machine, 64+11, 230, 206, 128 );
272   palette_set_color_rgb(machine, 64+12,  33, 176,  59 );
273   palette_set_color_rgb(machine, 64+13, 201,  91, 186 );
274   palette_set_color_rgb(machine, 64+14, 204, 204, 204 );
275   palette_set_color_rgb(machine, 64+15, 255, 255, 255 );
260   palette_set_color_rgb(machine, 64+ 0,   0,   0,   0);
261   palette_set_color_rgb(machine, 64+ 1,   0,   0,   0);
262   palette_set_color_rgb(machine, 64+ 2,  33, 200,  66);
263   palette_set_color_rgb(machine, 64+ 3,  94, 220, 120);
264   palette_set_color_rgb(machine, 64+ 4,  84,  85, 237);
265   palette_set_color_rgb(machine, 64+ 5, 125, 118, 252);
266   palette_set_color_rgb(machine, 64+ 6, 212,  82,  77);
267   palette_set_color_rgb(machine, 64+ 7,  66, 235, 245);
268   palette_set_color_rgb(machine, 64+ 8, 252,  85,  84);
269   palette_set_color_rgb(machine, 64+ 9, 255, 121, 120);
270   palette_set_color_rgb(machine, 64+10, 212, 193,  84);
271   palette_set_color_rgb(machine, 64+11, 230, 206, 128);
272   palette_set_color_rgb(machine, 64+12,  33, 176,  59);
273   palette_set_color_rgb(machine, 64+13, 201,  91, 186);
274   palette_set_color_rgb(machine, 64+14, 204, 204, 204);
275   palette_set_color_rgb(machine, 64+15, 255, 255, 255);
276276}
277277
278278static PALETTE_INIT( gamegear )
279279{
280280   int i;
281   for( i = 0; i < 4096; i++ )
281   for (i = 0; i < 4096; i++)
282282   {
283      int r = i & 0x000F;
284      int g = ( i & 0x00F0 ) >> 4;
285      int b = ( i & 0x0F00 ) >> 8;
286      palette_set_color_rgb(machine, i, r << 4, g << 4, b << 4 );
283      int r = i & 0x000f;
284      int g = (i & 0x00f0) >> 4;
285      int b = (i & 0x0f00) >> 8;
286      palette_set_color_rgb(machine, i, r << 4, g << 4, b << 4);
287287   }
288288}
289289
290static const smsvdp_configuration config_315_5124 = { MODEL_315_5124, sms_int_callback };
291static const smsvdp_configuration config_315_5246 = { MODEL_315_5246, sms_int_callback };
292static const smsvdp_configuration config_315_5378 = { MODEL_315_5378, sms_int_callback };
293static const smsvdp_configuration config_store = { MODEL_315_5124, sms_store_int_callback };
294290
295static VIDEO_START(sega_315_5124)
291
292static void sms_int_callback( running_machine *machine, int state )
296293{
297   smsvdp_video_init( machine, &config_315_5124 );
294   cputag_set_input_line(machine, "maincpu", 0, state);
298295}
299296
300static VIDEO_START(sega_315_5246)
297static const smsvdp_interface _315_5124_intf =
301298{
302   smsvdp_video_init( machine, &config_315_5246 );
303}
299   MODEL_315_5124,
300   sms_int_callback,
301   sms_pause_callback
302};
304303
305static VIDEO_START(sega_315_5378)
304static const smsvdp_interface _315_5246_intf =
306305{
307   smsvdp_video_init( machine, &config_315_5378 );
308}
306   MODEL_315_5246,
307   sms_int_callback,
308   sms_pause_callback
309};
309310
310static VIDEO_START(sega_store_315_5124)
311static const smsvdp_interface _315_5378_intf =
311312{
312   smsvdp_video_init( machine, &config_store );
313   MODEL_315_5378,
314   sms_int_callback,
315   sms_pause_callback
316};
317
318static const smsvdp_interface sms_store_intf =
319{
320   MODEL_315_5124,
321   sms_store_int_callback,
322   sms_pause_callback
323};
324
325static VIDEO_UPDATE(sms)
326{
327   const device_config *smsvdp = devtag_get_device(screen->machine, "sms_vdp");
328   smsvdp_update(smsvdp, bitmap, cliprect);
329
330   return 0;
313331}
314332
315333static MACHINE_DRIVER_START( sms_cartslot )
r6078r6079
339357   MDRV_PALETTE_LENGTH(64+16)
340358   MDRV_PALETTE_INIT(sms)
341359
342   MDRV_VIDEO_START(sega_315_5124)
343360   MDRV_VIDEO_UPDATE(sms)
344361
362   MDRV_SMSVDP_ADD("sms_vdp", _315_5124_intf)
363
345364   /* sound hardware */
346365   MDRV_SPEAKER_STANDARD_MONO("mono")
347366   MDRV_SOUND_ADD("smsiii", SMSIII, XTAL_53_693175MHz/15)
r6078r6079
352371
353372static MACHINE_DRIVER_START(sms2ntsc)
354373   MDRV_IMPORT_FROM(sms1ntsc)
355   MDRV_VIDEO_START(sega_315_5246)
374
375   MDRV_DEVICE_REMOVE("sms_vdp")
376   MDRV_SMSVDP_ADD("sms_vdp", _315_5246_intf)
356377MACHINE_DRIVER_END
357378
358379static MACHINE_DRIVER_START(smssdisp)
359380   MDRV_IMPORT_FROM(sms1ntsc)
360381
361   MDRV_VIDEO_START(sega_store_315_5124)
382   MDRV_DEVICE_REMOVE("sms_vdp")
383   MDRV_SMSVDP_ADD("sms_vdp", sms_store_intf)
362384
363385   MDRV_CPU_ADD("control", Z80, XTAL_53_693175MHz/15)
364386   MDRV_CPU_PROGRAM_MAP(sms_store_mem)
r6078r6079
406428   MDRV_PALETTE_LENGTH(64+16)
407429   MDRV_PALETTE_INIT(sms)
408430
409   MDRV_VIDEO_START(sega_315_5124)
410431   MDRV_VIDEO_UPDATE(sms)
411432
433   MDRV_SMSVDP_ADD("sms_vdp", _315_5124_intf)
434
412435   /* sound hardware */
413436   MDRV_SPEAKER_STANDARD_MONO("mono")
414437   MDRV_SOUND_ADD("smsiii", SMSIII, MASTER_CLOCK_PAL/15)
r6078r6079
419442
420443static MACHINE_DRIVER_START(sms2pal)
421444   MDRV_IMPORT_FROM(sms1pal)
422   MDRV_VIDEO_START(sega_315_5246)
445
446   MDRV_DEVICE_REMOVE("sms_vdp")
447   MDRV_SMSVDP_ADD("sms_vdp", _315_5246_intf)
423448MACHINE_DRIVER_END
424449
425450static MACHINE_DRIVER_START(smsfm)
r6078r6079
464489   MDRV_PALETTE_LENGTH(4096)
465490   MDRV_PALETTE_INIT(gamegear)
466491
467   MDRV_VIDEO_START(sega_315_5378)
468492   MDRV_VIDEO_UPDATE(sms)
469493
494   MDRV_SMSVDP_ADD("sms_vdp", _315_5378_intf)
495
470496   /* sound hardware */
471497   MDRV_SPEAKER_STANDARD_MONO("mono")
472498   MDRV_SOUND_ADD("gamegear", GAMEGEAR, XTAL_53_693175MHz/15)
r6078r6079
482508
483509ROM_START(sms1)
484510   ROM_REGION(0x4000, "maincpu", 0)
485   ROM_FILL(0x0000,0x4000,0xFF)
511   ROM_FILL(0x0000, 0x4000, 0xff)
512
486513   ROM_REGION(0x20000, "user1", 0)
487514   ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
488515   ROMX_LOAD("bios13fx.rom", 0x0000, 0x2000, CRC(0072ED54) SHA1(c315672807d8ddb8d91443729405c766dd95cae7), ROM_BIOS(1))
r6078r6079
498525
499526ROM_START(sms)
500527   ROM_REGION(0x4000, "maincpu", 0)
501   ROM_FILL(0x0000,0x4000,0xFF)
528   ROM_FILL(0x0000, 0x4000, 0xff)
529
502530   ROM_REGION(0x20000, "user1", 0)
503531   ROM_SYSTEM_BIOS( 0, "alexkidd", "US/European BIOS with Alex Kidd in Miracle World (1990)" )
504532   ROMX_LOAD("akbios.rom", 0x0000, 0x20000, CRC(CF4A09EA) SHA1(3af7b66248d34eb26da40c92bf2fa4c73a46a051), ROM_BIOS(1))
r6078r6079
506534
507535ROM_START(smssdisp)
508536   ROM_REGION(0x4000, "maincpu", 0)
509   ROM_FILL(0x0000,0x4000,0x00)
537   ROM_FILL(0x0000, 0x4000, 0x00)
538
510539   ROM_REGION(0x4000, "user1", 0)
511   ROM_FILL(0x0000,0x4000,0xFF)
540   ROM_FILL(0x0000, 0x4000, 0xff)
541
512542   ROM_REGION(0x4000, "control", 0)
513543   ROM_LOAD("smssdisp.rom", 0x0000, 0x4000, CRC(ee2c29ba) SHA1(fc465122134d95363112eb51b9ab71db3576cefd))
514544ROM_END
515545
516546ROM_START(sms1pal)
517547   ROM_REGION(0x4000, "maincpu", 0)
518   ROM_FILL(0x0000,0x4000,0xFF)
548   ROM_FILL(0x0000, 0x4000, 0xff)
549
519550   ROM_REGION(0x20000, "user1", 0)
520    ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
551   ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
521552   ROMX_LOAD("bios13fx.rom", 0x0000, 0x2000, CRC(0072ED54) SHA1(c315672807d8ddb8d91443729405c766dd95cae7), ROM_BIOS(1))
522553   ROM_SYSTEM_BIOS( 1, "hangonsh", "US/European BIOS v2.4 with Hang On and Safari Hunt (1988)" )
523554   ROMX_LOAD("hshbios.rom", 0x0000, 0x20000, CRC(91E93385) SHA1(9e179392cd416af14024d8f31c981d9ee9a64517), ROM_BIOS(2))
r6078r6079
529560
530561ROM_START(smspal)
531562   ROM_REGION(0x4000, "maincpu", 0)
532   ROM_FILL(0x0000,0x4000,0xFF)
563   ROM_FILL(0x0000, 0x4000, 0xff)
564
533565   ROM_REGION(0x40000, "user1", 0)
534566   ROM_SYSTEM_BIOS( 0, "alexkidd", "US/European BIOS with Alex Kidd in Miracle World (1990)" )
535567   ROMX_LOAD("akbios.rom", 0x0000, 0x20000, CRC(CF4A09EA) SHA1(3af7b66248d34eb26da40c92bf2fa4c73a46a051), ROM_BIOS(1))
r6078r6079
539571
540572ROM_START(sg1000m3)
541573   ROM_REGION(0x4000, "maincpu", 0)
542   ROM_FILL(0x0000,0x4000,0x00)
574   ROM_FILL(0x0000, 0x4000, 0x00)
543575ROM_END
544576
545577ROM_START(smsj)
546578   ROM_REGION(0x4000, "maincpu", 0)
547   ROM_FILL(0x0000,0x4000,0xFF)
579   ROM_FILL(0x0000, 0x4000, 0xff)
580
548581   ROM_REGION(0x4000, "user1", 0)
549    ROM_SYSTEM_BIOS( 0, "jbios21", "Japanese BIOS v2.1 (1987)" )
582   ROM_SYSTEM_BIOS( 0, "jbios21", "Japanese BIOS v2.1 (1987)" )
550583   ROMX_LOAD("jbios21.rom", 0x0000, 0x2000, CRC(48D44A13) SHA1(a8c1b39a2e41137835eda6a5de6d46dd9fadbaf2), ROM_BIOS(1))
551584ROM_END
552585
553586ROM_START(sms2kr)
554587   ROM_REGION(0x4000, "maincpu", 0)
555   ROM_FILL(0x0000,0x4000,0xFF)
588   ROM_FILL(0x0000, 0x4000, 0xff)
589
556590   ROM_REGION(0x20000, "user1", 0)
557591   ROM_SYSTEM_BIOS( 0, "akbioskr", "Samsung Gam*Boy II with Alex Kidd in Miracle World (1990)" )
558592   ROMX_LOAD("akbioskr.rom", 0x000, 0x20000, CRC(9c5bad91) SHA1(2feafd8f1c40fdf1bd5668f8c5c02e5560945b17), ROM_BIOS(1))
559593ROM_END
560594
561595ROM_START(gamegear)
562   ROM_REGION(0x4000, "maincpu",0)
563   ROM_FILL(0x0000,0x4000,0x00)
596   ROM_REGION(0x4000, "maincpu", 0)
597   ROM_FILL(0x0000, 0x4000, 0x00)
598
564599   ROM_REGION(0x0400, "user1", 0)
565600   ROM_SYSTEM_BIOS( 0, "none", "No BIOS" ) /* gamegear */
566601   ROM_SYSTEM_BIOS( 1, "majesco", "Majesco BIOS" ) /* gamg */
docs/messnew.txt
r6078r6079
10.134.1
10.134.1
22
33New System Drivers Supported (in no particular order):
44------------------------------------------------------
r6078r6079
6060- [SMS] Fixed rapid-fire to only fire when buttons are
6161  pressed. (Bug #1864)  [Enik Land]
6262
63- [SMS] Added Light Phaser support. (Bug #1123)  [Enik Land]
64
65- [SMS] Converted SMS VDP to be a device [Fabio Priuli]
66
6367- [SPECTRUM] Fixed bug #803 48K SNA load routine bad PC. [Miodrag Milanovic]
6468
6569- [TRS80] Model 4 and 4P can boot from floppy disk. [Robbbert]

Previous 15994 Revisions Next


© 1998-2018 The MESS Team