Previous 15994 Revisions Next

r11573 Tuesday 17th May, 2011 at 18:41:01 UTC by Angelo Salese
Moved SMS again into its place, nw
[src/mame/drivers]segasms.c
[src/mame/includes]segasms.h
[src/mame/machine]segasms.c
[src/mame/video]smsvdp.c smsvdp.h
[src/mess]mess.mak
[src/mess/drivers]segasms.c*
[src/mess/includes]segasms.h*
[src/mess/machine]segasms.c*
[src/mess/video]smsvdp.c* smsvdp.h*

src/mame/machine/segasms.c
r11572r11573
1#include "emu.h"
2#include "crsshair.h"
3#include "image.h"
4#include "includes/segasms.h"
5#include "video/smsvdp.h"
6#include "sound/2413intf.h"
7#include "imagedev/cartslot.h"
8#include "hashfile.h"
9
10#define VERBOSE 0
11#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
12
13#define CF_CODEMASTERS_MAPPER    0x01
14#define CF_KOREAN_MAPPER         0x02
15#define CF_KOREAN_ZEMINA_MAPPER  0x04
16#define CF_KOREAN_NOBANK_MAPPER  0x08
17#define CF_93C46_EEPROM          0x10
18#define CF_ONCART_RAM            0x20
19#define CF_GG_SMS_MODE           0x40
20
21#define LGUN_RADIUS           6
22#define LGUN_X_INTERVAL       4
23
24
25static void setup_rom(address_space *space);
26
27
28static TIMER_CALLBACK( rapid_fire_callback )
29{
30   sms_state *state = machine.driver_data<sms_state>();
31   state->m_rapid_fire_state_1 ^= 0xff;
32   state->m_rapid_fire_state_2 ^= 0xff;
33}
34
35
36static WRITE8_HANDLER( sms_input_write )
37{
38   sms_state *state = space->machine().driver_data<sms_state>();
39
40   switch (offset)
41   {
42   case 0:
43      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) & 0x0f)
44      {
45      case 0x03:   /* Sports Pad */
46         if (data != state->m_sports_pad_last_data_1)
47         {
48            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
49
50            state->m_sports_pad_last_data_1 = data;
51            if (cpu_cycles - state->m_last_sports_pad_time_1 > 512)
52            {
53               state->m_sports_pad_state_1 = 3;
54               state->m_sports_pad_1_x = input_port_read(space->machine(), "SPORT0");
55               state->m_sports_pad_1_y = input_port_read(space->machine(), "SPORT1");
56            }
57            state->m_last_sports_pad_time_1 = cpu_cycles;
58            state->m_sports_pad_state_1 = (state->m_sports_pad_state_1 + 1) & 3;
59         }
60         break;
61      }
62      break;
63
64   case 1:
65      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) >> 4)
66      {
67      case 0x03:   /* Sports Pad */
68         if (data != state->m_sports_pad_last_data_2)
69         {
70            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
71
72            state->m_sports_pad_last_data_2 = data;
73            if (cpu_cycles - state->m_last_sports_pad_time_2 > 2048)
74            {
75               state->m_sports_pad_state_2 = 3;
76               state->m_sports_pad_2_x = input_port_read(space->machine(), "SPORT2");
77               state->m_sports_pad_2_y = input_port_read(space->machine(), "SPORT3");
78            }
79            state->m_last_sports_pad_time_2 = cpu_cycles;
80            state->m_sports_pad_state_2 = (state->m_sports_pad_state_2 + 1) & 3;
81         }
82         break;
83      }
84      break;
85   }
86}
87
88/* FIXME: this function is a hack for Light Phaser emulation. Theoretically
89   sms_vdp_hcount_latch() should be used instead, but it returns incorrect
90   position for unknown reason (timing?) */
91static void sms_vdp_hcount_lphaser( running_machine &machine, int hpos )
92{
93   sms_state *state = machine.driver_data<sms_state>();
94   int hpos_tmp = hpos + state->m_lphaser_x_offs;
95   UINT8 tmp = ((hpos_tmp - 46) >> 1) & 0xff;
96
97   //printf ("sms_vdp_hcount_lphaser: hpos %3d hpos_tmp %3d => hcount %2X\n", hpos, hpos_tmp, tmp);
98   sms_vdp_hcount_latch_w(state->m_vdp, 0, tmp);
99}
100
101
102/*
103    Light Phaser (light gun) emulation notes:
104    - The sensor is activated based on color brightness of some individual
105      pixels being drawn by the beam, at circular area where the gun is aiming.
106    - Currently, brightness is calculated based only on single pixels.
107    - In general, after the trigger is pressed, games draw the next frame using
108      a light color pattern, to make sure sensor will be activated. If emulation
109      skips that frame, sensor may stay deactivated. Frameskip set to 0 (no skip) is
110      recommended to avoid problems.
111    - When sensor switches from off to on, a value is latched for HCount register
112      and a flag is set. The emulation uses the flag to avoid sequential latches and
113      to signal that TH line is activated when the status of the input port is read.
114      When the status is read, the flag is cleared, or else it is cleared later when
115      the Pause status is read (end of a frame). This is necessary because the
116      "Color & Switch Test" ROM only reads the TH bit after the VINT line.
117    - The gun test of "Color & Switch Test" is an example that requires checks
118      of sensor status independent of other events, like trigger press or TH bit
119      reads. Another example is the title screen of "Hang-On & Safari Hunt", where
120      the game only reads HCount register in a loop, expecting a latch by the gun.
121    - The whole procedure is managed by a timer callback, that always reschedule
122      itself to run in some intervals when the beam is at the circular area.
123*/
124static int lgun_bright_aim_area( running_machine &machine, emu_timer *timer, int lgun_x, int lgun_y )
125{
126   sms_state *state = machine.driver_data<sms_state>();
127   const int r_x_r = LGUN_RADIUS * LGUN_RADIUS;
128   screen_device *screen = machine.first_screen();
129   const rectangle &visarea = screen->visible_area();
130   int beam_x = screen->hpos();
131   int beam_y = screen->vpos();
132   int dx, dy;
133   int result = 0;
134   int pos_changed = 0;
135   double dx_circ;
136
137    while (1)
138   {
139      dy = abs(beam_y - lgun_y);
140
141      if (dy > LGUN_RADIUS || beam_y < visarea.min_y || beam_y > visarea.max_y)
142      {
143         beam_y = lgun_y - LGUN_RADIUS;
144         if (beam_y < visarea.min_y)
145            beam_y = visarea.min_y;
146         dy = abs(beam_y - lgun_y);
147         pos_changed = 1;
148      }
149      /* step 1: r^2 = dx^2 + dy^2 */
150      /* step 2: dx^2 = r^2 - dy^2 */
151      /* step 3: dx = sqrt(r^2 - dy^2) */
152      dx_circ = ceil((float) sqrt((float) (r_x_r - (dy * dy))));
153      dx = abs(beam_x - lgun_x);
154
155      if (dx > dx_circ || beam_x < visarea.min_x || beam_x > visarea.max_x)
156      {
157         if (beam_x > lgun_x)
158         {
159            beam_x = 0;
160            beam_y++;
161            continue;
162         }
163         beam_x = lgun_x - dx_circ;
164         if (beam_x < visarea.min_x)
165            beam_x = visarea.min_x;
166         pos_changed = 1;
167      }
168
169      if (!pos_changed)
170      {
171         result = sms_vdp_check_brightness(state->m_vdp, beam_x, beam_y);
172         /* next check at same line */
173         beam_x += LGUN_X_INTERVAL;
174         pos_changed = 1;
175      }
176      else
177         break;
178   }
179   timer->adjust(machine.first_screen()->time_until_pos(beam_y, beam_x));
180
181   return result;
182}
183
184static UINT8 sms_vdp_hcount( running_machine &machine )
185{
186   UINT8 tmp;
187   screen_device *screen = machine.first_screen();
188   int hpos = screen->hpos();
189
190   /* alternative method: pass HCounter test, but some others fail */
191   //int hpos_tmp = hpos;
192   //if ((hpos + 2) % 6 == 0) hpos_tmp--;
193   //tmp = ((hpos_tmp - 46) >> 1) & 0xff;
194
195   UINT64 calc_cycles;
196   attotime time_end;
197   int vpos = screen->vpos();
198   int max_hpos = screen->width() - 1;
199
200   if (hpos == max_hpos)
201      time_end = attotime::zero;
202   else
203      time_end = screen->time_until_pos(vpos, max_hpos);
204   calc_cycles = machine.device<cpu_device>("maincpu")->attotime_to_clocks(time_end);
205
206   /* equation got from SMSPower forum, posted by Flubba. */
207   tmp = ((590 - (calc_cycles * 3)) / 4) & 0xff;
208
209   //printf ("sms_vdp_hcount: hpos %3d => hcount %2X\n", hpos, tmp);
210   return tmp;
211}
212
213
214static void sms_vdp_hcount_latch( running_machine &machine )
215{
216   sms_state *state = machine.driver_data<sms_state>();
217   UINT8 value = sms_vdp_hcount(machine);
218
219   sms_vdp_hcount_latch_w(state->m_vdp, 0, value);
220}
221
222
223static UINT16 screen_hpos_nonscaled( screen_device *screen, int scaled_hpos )
224{
225   const rectangle &visarea = screen->visible_area();
226   int offset_x = (scaled_hpos * (visarea.max_x - visarea.min_x)) / 255;
227   return visarea.min_x + offset_x;
228}
229
230
231static UINT16 screen_vpos_nonscaled( screen_device *screen, int scaled_vpos )
232{
233   const rectangle &visarea = screen->visible_area();
234   int offset_y = (scaled_vpos * (visarea.max_y - visarea.min_y)) / 255;
235   return visarea.min_y + offset_y;
236}
237
238
239static void lphaser1_sensor_check( running_machine &machine )
240{
241   sms_state *state = machine.driver_data<sms_state>();
242   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER0"));
243   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER1"));
244
245   if (lgun_bright_aim_area(machine, state->m_lphaser_1_timer, x, y))
246   {
247      if (state->m_lphaser_1_latch == 0)
248      {
249         state->m_lphaser_1_latch = 1;
250         sms_vdp_hcount_lphaser(machine, x);
251      }
252   }
253}
254
255static void lphaser2_sensor_check( running_machine &machine )
256{
257   sms_state *state = machine.driver_data<sms_state>();
258   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER2"));
259   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER3"));
260
261   if (lgun_bright_aim_area(machine, state->m_lphaser_2_timer, x, y))
262   {
263      if (state->m_lphaser_2_latch == 0)
264      {
265         state->m_lphaser_2_latch = 1;
266         sms_vdp_hcount_lphaser(machine, x);
267      }
268   }
269}
270
271
272// at each input port read we check if lightguns are enabled in one of the ports:
273// if so, we turn on crosshair and the lightgun timer
274static TIMER_CALLBACK( lightgun_tick )
275{
276   sms_state *state = machine.driver_data<sms_state>();
277
278   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f) == 0x01)
279   {
280      /* enable crosshair */
281      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
282      if (!state->m_lphaser_1_timer->enabled())
283         lphaser1_sensor_check(machine);
284   }
285   else
286   {
287      /* disable crosshair */
288      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
289      state->m_lphaser_1_timer->enable(0);
290   }
291
292   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0) == 0x10)
293   {
294      /* enable crosshair */
295      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_ALL);
296      if (!state->m_lphaser_2_timer->enabled())
297         lphaser2_sensor_check(machine);
298   }
299   else
300   {
301      /* disable crosshair */
302      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_NONE);
303      state->m_lphaser_2_timer->enable(0);
304   }
305}
306
307
308static TIMER_CALLBACK( lphaser_1_callback )
309{
310   lphaser1_sensor_check(machine);
311}
312
313
314static TIMER_CALLBACK( lphaser_2_callback )
315{
316   lphaser2_sensor_check(machine);
317}
318
319
320INPUT_CHANGED( lgun1_changed )
321{
322   sms_state *state = field.machine().driver_data<sms_state>();
323   if (!state->m_lphaser_1_timer ||
324      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0x0f) != 0x01)
325      return;
326
327   if (newval != oldval)
328      lphaser1_sensor_check(field.machine());
329}
330
331INPUT_CHANGED( lgun2_changed )
332{
333   sms_state *state = field.machine().driver_data<sms_state>();
334   if (!state->m_lphaser_2_timer ||
335      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0xf0) != 0x10)
336      return;
337
338   if (newval != oldval)
339      lphaser2_sensor_check(field.machine());
340}
341
342
343static void sms_get_inputs( address_space *space )
344{
345   sms_state *state = space->machine().driver_data<sms_state>();
346   UINT8 data = 0x00;
347   UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
348   running_machine &machine = space->machine();
349
350   state->m_input_port0 = 0xff;
351   state->m_input_port1 = 0xff;
352
353   if (cpu_cycles - state->m_last_paddle_read_time > 256)
354   {
355      state->m_paddle_read_state ^= 0xff;
356      state->m_last_paddle_read_time = cpu_cycles;
357   }
358
359   /* Check if lightgun has been chosen as input: if so, enable crosshair */
360   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
361
362   /* Player 1 */
363   switch (input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f)
364   {
365   case 0x00:  /* Joystick */
366      data = input_port_read(machine, "PORT_DC");
367      /* Check Rapid Fire setting for Button A */
368      if (!(data & 0x10) && (input_port_read(machine, "RFU") & 0x01))
369         data |= state->m_rapid_fire_state_1 & 0x10;
370
371      /* Check Rapid Fire setting for Button B */
372      if (!(data & 0x20) && (input_port_read(machine, "RFU") & 0x02))
373         data |= state->m_rapid_fire_state_1 & 0x20;
374
375      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
376      break;
377
378   case 0x01:  /* Light Phaser */
379      data = (input_port_read(machine, "CTRLIPT") & 0x01) << 4;
380      if (!(data & 0x10))
381      {
382         if (input_port_read(machine, "RFU") & 0x01)
383            data |= state->m_rapid_fire_state_1 & 0x10;
384      }
385      /* just consider the button (trigger) bit */
386      data |= ~0x10;
387      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
388      break;
389
390   case 0x02:  /* Paddle Control */
391      /* Get button A state */
392      data = input_port_read(machine, "PADDLE0");
393
394      if (state->m_paddle_read_state)
395         data = data >> 4;
396
397      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x0f) | (state->m_paddle_read_state & 0x20)
398                      | ((input_port_read(machine, "CTRLIPT") & 0x02) << 3);
399      break;
400
401   case 0x03:   /* Sega Sports Pad */
402      switch (state->m_sports_pad_state_1)
403      {
404      case 0:
405         data = (state->m_sports_pad_1_x >> 4) & 0x0f;
406         break;
407      case 1:
408         data = state->m_sports_pad_1_x & 0x0f;
409         break;
410      case 2:
411         data = (state->m_sports_pad_1_y >> 4) & 0x0f;
412         break;
413      case 3:
414         data = state->m_sports_pad_1_y & 0x0f;
415         break;
416      }
417      state->m_input_port0 = (state->m_input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
418      break;
419   }
420
421   /* Player 2 */
422   switch (input_port_read_safe(machine, "CTRLSEL", 0x00)  & 0xf0)
423   {
424   case 0x00:   /* Joystick */
425      data = input_port_read(machine, "PORT_DC");
426      state->m_input_port0 = (state->m_input_port0 & 0x3f) | (data & 0xc0);
427
428      data = input_port_read(machine, "PORT_DD");
429      /* Check Rapid Fire setting for Button A */
430      if (!(data & 0x04) && (input_port_read(machine, "RFU") & 0x04))
431         data |= state->m_rapid_fire_state_2 & 0x04;
432
433      /* Check Rapid Fire setting for Button B */
434      if (!(data & 0x08) && (input_port_read(machine, "RFU") & 0x08))
435         data |= state->m_rapid_fire_state_2 & 0x08;
436
437      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
438      break;
439
440   case 0x10:   /* Light Phaser */
441      data = (input_port_read(machine, "CTRLIPT") & 0x10) >> 2;
442      if (!(data & 0x04))
443      {
444         if (input_port_read(machine, "RFU") & 0x04)
445            data |= state->m_rapid_fire_state_2 & 0x04;
446      }
447      /* just consider the button (trigger) bit */
448      data |= ~0x04;
449      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
450      break;
451
452   case 0x20:   /* Paddle Control */
453      /* Get button A state */
454      data = input_port_read(machine, "PADDLE1");
455      if (state->m_paddle_read_state)
456         data = data >> 4;
457
458      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
459      state->m_input_port1 = (state->m_input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (state->m_paddle_read_state & 0x08)
460                      | ((input_port_read(machine, "CTRLIPT") & 0x20) >> 3);
461      break;
462
463   case 0x30:   /* Sega Sports Pad */
464      switch (state->m_sports_pad_state_2)
465      {
466      case 0:
467         data = state->m_sports_pad_2_x & 0x0f;
468         break;
469      case 1:
470         data = (state->m_sports_pad_2_x >> 4) & 0x0f;
471         break;
472      case 2:
473         data = state->m_sports_pad_2_y & 0x0f;
474         break;
475      case 3:
476         data = (state->m_sports_pad_2_y >> 4) & 0x0f;
477         break;
478      }
479      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
480      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
481      break;
482   }
483}
484
485
486WRITE8_HANDLER( sms_fm_detect_w )
487{
488   sms_state *state = space->machine().driver_data<sms_state>();
489
490   if (state->m_has_fm)
491      state->m_fm_detect = (data & 0x01);
492}
493
494
495READ8_HANDLER( sms_fm_detect_r )
496{
497   sms_state *state = space->machine().driver_data<sms_state>();
498
499   if (state->m_has_fm)
500   {
501      return state->m_fm_detect;
502   }
503   else
504   {
505      if (state->m_bios_port & IO_CHIP)
506      {
507         return 0xff;
508      }
509      else
510      {
511         sms_get_inputs(space);
512         return state->m_input_port0;
513      }
514   }
515}
516
517WRITE8_HANDLER( sms_io_control_w )
518{
519   sms_state *state = space->machine().driver_data<sms_state>();
520
521   if (data & 0x08)
522   {
523      /* check if TH pin level is high (1) and was low last time */
524      if (data & 0x80 && !(state->m_ctrl_reg & 0x80))
525      {
526         sms_vdp_hcount_latch(space->machine());
527      }
528      sms_input_write(space, 0, (data & 0x20) >> 5);
529   }
530
531   if (data & 0x02)
532   {
533      if (data & 0x20 && !(state->m_ctrl_reg & 0x20))
534      {
535         sms_vdp_hcount_latch(space->machine());
536      }
537      sms_input_write(space, 1, (data & 0x80) >> 7);
538   }
539
540   state->m_ctrl_reg = data;
541}
542
543
544READ8_HANDLER( sms_count_r )
545{
546   sms_state *state = space->machine().driver_data<sms_state>();
547
548   if (offset & 0x01)
549      return sms_vdp_hcount_latch_r(state->m_vdp, offset);
550   else
551      return sms_vdp_vcount_r(state->m_vdp, offset);
552}
553
554
555/*
556 Check if the pause button is pressed.
557 If the gamegear is in sms mode, check if the start button is pressed.
558 */
559void sms_pause_callback( running_machine &machine )
560{
561   sms_state *state = machine.driver_data<sms_state>();
562
563   if (state->m_is_gamegear && !(state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE))
564      return;
565
566   if (!(input_port_read(machine, state->m_is_gamegear ? "START" : "PAUSE") & 0x80))
567   {
568      if (!state->m_paused)
569      {
570         cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE);
571      }
572      state->m_paused = 1;
573   }
574   else
575   {
576      state->m_paused = 0;
577   }
578
579   /* clear Light Phaser latch flags for next frame */
580   state->m_lphaser_1_latch = 0;
581   state->m_lphaser_2_latch = 0;
582}
583
584READ8_HANDLER( sms_input_port_0_r )
585{
586   sms_state *state = space->machine().driver_data<sms_state>();
587
588   if (state->m_bios_port & IO_CHIP)
589   {
590      return 0xff;
591   }
592   else
593   {
594      sms_get_inputs(space);
595      return state->m_input_port0;
596   }
597}
598
599
600READ8_HANDLER( sms_input_port_1_r )
601{
602   sms_state *state = space->machine().driver_data<sms_state>();
603
604   if (state->m_bios_port & IO_CHIP)
605      return 0xff;
606
607   sms_get_inputs(space);
608
609   /* Reset Button */
610   state->m_input_port1 = (state->m_input_port1 & 0xef) | (input_port_read_safe(space->machine(), "RESET", 0x01) & 0x01) << 4;
611
612   /* Do region detection if TH of ports A and B are set to output (0) */
613   if (!(state->m_ctrl_reg & 0x0a))
614   {
615      /* Move bits 7,5 of IO control port into bits 7, 6 */
616      state->m_input_port1 = (state->m_input_port1 & 0x3f) | (state->m_ctrl_reg & 0x80) | (state->m_ctrl_reg & 0x20) << 1;
617
618      /* Inverse region detect value for Japanese machines */
619      if (state->m_is_region_japan)
620         state->m_input_port1 ^= 0xc0;
621   }
622   else
623   {
624      if (state->m_ctrl_reg & 0x02 && state->m_lphaser_1_latch)
625      {
626         state->m_input_port1 &= ~0x40;
627         state->m_lphaser_1_latch = 0;
628      }
629
630      if (state->m_ctrl_reg & 0x08 && state->m_lphaser_2_latch)
631      {
632         state->m_input_port1 &= ~0x80;
633         state->m_lphaser_2_latch = 0;
634      }
635   }
636
637   return state->m_input_port1;
638}
639
640
641
642WRITE8_HANDLER( sms_ym2413_register_port_0_w )
643{
644   sms_state *state = space->machine().driver_data<sms_state>();
645
646   if (state->m_has_fm)
647      ym2413_w(state->m_ym, 0, (data & 0x3f));
648}
649
650
651WRITE8_HANDLER( sms_ym2413_data_port_0_w )
652{
653   sms_state *state = space->machine().driver_data<sms_state>();
654
655   if (state->m_has_fm)
656   {
657      logerror("data_port_0_w %x %x\n", offset, data);
658      ym2413_w(state->m_ym, 1, data);
659   }
660}
661
662
663READ8_HANDLER( gg_input_port_2_r )
664{
665   sms_state *state = space->machine().driver_data<sms_state>();
666
667   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
668   return ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine(), "START") & 0x80));
669}
670
671
672READ8_HANDLER( sms_sscope_r )
673{
674   sms_state *state = space->machine().driver_data<sms_state>();
675   return state->m_sscope_state;
676}
677
678
679WRITE8_HANDLER( sms_sscope_w )
680{
681   sms_state *state = space->machine().driver_data<sms_state>();
682   state->m_sscope_state = data;
683}
684
685
686READ8_HANDLER( sms_mapper_r )
687{
688   sms_state *state = space->machine().driver_data<sms_state>();
689   return state->m_mapper[offset];
690}
691
692/* Terebi Oekaki */
693/* The following code comes from sg1000.c. We should eventually merge these TV Draw implementations */
694static WRITE8_HANDLER( sms_tvdraw_axis_w )
695{
696   sms_state *state = space->machine().driver_data<sms_state>();
697   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
698
699   if (data & 0x01)
700   {
701      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_X") : 0x80;
702
703      if (state->m_tvdraw_data < 4) state->m_tvdraw_data = 4;
704      if (state->m_tvdraw_data > 251) state->m_tvdraw_data = 251;
705   }
706   else
707   {
708      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_Y") + 0x20 : 0x80;
709   }
710}
711
712static READ8_HANDLER( sms_tvdraw_status_r )
713{
714   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
715   return tvboard_on ? input_port_read(space->machine(), "TVDRAW_PEN") : 0x01;
716}
717
718static READ8_HANDLER( sms_tvdraw_data_r )
719{
720   sms_state *state = space->machine().driver_data<sms_state>();
721   return state->m_tvdraw_data;
722}
723
724
725WRITE8_HANDLER( sms_mapper_w )
726{
727   sms_state *state = space->machine().driver_data<sms_state>();
728   int page;
729   UINT8 *SOURCE_BIOS;
730   UINT8 *SOURCE_CART;
731   UINT8 *SOURCE;
732   UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
733
734   offset &= 3;
735
736   state->m_mapper[offset] = data;
737   state->m_mapper_ram[offset] = data;
738
739   if (state->m_cartridge[state->m_current_cartridge].ROM)
740   {
741      SOURCE_CART = state->m_cartridge[state->m_current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
742   }
743   else
744   {
745      SOURCE_CART = state->m_banking_none[1];
746   }
747
748   if (state->m_BIOS)
749   {
750      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
751   }
752   else
753   {
754      SOURCE_BIOS = state->m_banking_none[1];
755   }
756
757   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
758   {
759      if (!(state->m_bios_port & IO_CARTRIDGE) || (state->m_is_gamegear && state->m_BIOS == NULL))
760      {
761         page = (rom_page_count > 0) ? data % rom_page_count : 0;
762         if (!state->m_cartridge[state->m_current_cartridge].ROM)
763            return;
764         SOURCE = SOURCE_CART;
765      }
766      else
767      {
768         /* nothing to page in */
769         return;
770      }
771   }
772   else
773   {
774      page = (state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0;
775      if (!state->m_BIOS)
776         return;
777      SOURCE = SOURCE_BIOS;
778   }
779
780   switch (offset)
781   {
782   case 0: /* Control */
783      /* Is it ram or rom? */
784      if (data & 0x08) /* it's ram */
785      {
786         state->m_cartridge[state->m_current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
787         if (data & 0x04)
788         {
789            LOG(("ram 1 paged.\n"));
790            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM + 0x4000;
791         }
792         else
793         {
794            LOG(("ram 0 paged.\n"));
795            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM;
796         }
797         memory_set_bankptr(space->machine(),  "bank5", SOURCE);
798         memory_set_bankptr(space->machine(),  "bank6", SOURCE + 0x2000);
799      }
800      else /* it's rom */
801      {
802         if (state->m_bios_port & IO_BIOS_ROM || ! state->m_has_bios)
803         {
804            page = (rom_page_count > 0) ? state->m_mapper[3] % rom_page_count : 0;
805            SOURCE = state->m_banking_cart[5];
806         }
807         else
808         {
809            page = (state->m_bios_page_count > 0) ? state->m_mapper[3] % state->m_bios_page_count : 0;
810            SOURCE = state->m_banking_bios[5];
811         }
812         LOG(("rom 2 paged in %x.\n", page));
813         memory_set_bankptr(space->machine(), "bank5", SOURCE);
814         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
815      }
816      break;
817
818   case 1: /* Select 16k ROM bank for 0400-3FFF */
819      LOG(("rom 0 paged in %x.\n", page));
820      state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
821      state->m_banking_cart[2] = SOURCE_CART + 0x0400;
822      if (state->m_is_gamegear)
823         SOURCE = SOURCE_CART;
824
825      memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
826      break;
827
828   case 2: /* Select 16k ROM bank for 4000-7FFF */
829      LOG(("rom 1 paged in %x.\n", page));
830      state->m_banking_bios[3] = SOURCE_BIOS;
831      state->m_banking_cart[3] = SOURCE_CART;
832      if (state->m_is_gamegear)
833         SOURCE = SOURCE_CART;
834
835      memory_set_bankptr(space->machine(), "bank3", SOURCE);
836      memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
837      break;
838
839   case 3: /* Select 16k ROM bank for 8000-BFFF */
840      state->m_banking_bios[5] = SOURCE_BIOS;
841      if (state->m_is_gamegear)
842         SOURCE = SOURCE_CART;
843
844      if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
845      {
846         if (SOURCE == SOURCE_CART)
847         {
848            SOURCE = state->m_banking_cart[5];
849         }
850      }
851      else
852      {
853         state->m_banking_cart[5] = SOURCE_CART;
854      }
855
856      if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
857      {
858         LOG(("rom 2 paged in %x.\n", page));
859         memory_set_bankptr(space->machine(), "bank5", SOURCE);
860         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
861      }
862      break;
863   }
864}
865
866#ifdef MESS
867static WRITE8_HANDLER( sms_korean_zemina_banksw_w )
868{
869   sms_state *state = space->machine().driver_data<sms_state>();
870
871   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
872   {
873      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x2000;
874      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
875
876      if (!state->m_cartridge[state->m_current_cartridge].ROM)
877         return;
878
879      switch (offset & 3)
880      {
881         case 0:
882            state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
883            memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
884            break;
885         case 1:
886            state->m_banking_cart[6] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
887            memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[6]);
888            break;
889         case 2:
890            state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
891            memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
892            break;
893         case 3:
894            state->m_banking_cart[4] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
895            memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[4]);
896            break;
897      }
898      LOG(("Zemina mapper write: offset %x data %x.\n", offset, page));
899   }
900}
901
902static WRITE8_HANDLER( sms_codemasters_page0_w )
903{
904   sms_state *state = space->machine().driver_data<sms_state>();
905
906   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
907   {
908      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
909      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
910      state->m_banking_cart[2] = state->m_banking_cart[1] + 0x0400;
911      memory_set_bankptr(space->machine(), "bank1", state->m_banking_cart[1]);
912      memory_set_bankptr(space->machine(), "bank2", state->m_banking_cart[2]);
913   }
914}
915
916
917static WRITE8_HANDLER( sms_codemasters_page1_w )
918{
919   sms_state *state = space->machine().driver_data<sms_state>();
920
921   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
922   {
923      /* Check if we need to switch in some RAM */
924      if (data & 0x80)
925      {
926         state->m_cartridge[state->m_current_cartridge].ram_page = data & 0x07;
927         memory_set_bankptr(space->machine(), "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM + state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000);
928      }
929      else
930      {
931         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
932         state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
933         memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
934         memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[3] + 0x2000);
935         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
936      }
937   }
938}
939#endif
940
941static READ8_HANDLER( sms_kor_nobank_r )
942{
943//  printf("read %x\n", offset);
944   return space->read_byte(0xdffc + offset);
945}
946
947static WRITE8_HANDLER( sms_kor_nobank_w )
948{
949   sms_state *state = space->machine().driver_data<sms_state>();
950   UINT8 *SOURCE_BIOS;
951   UINT8 *SOURCE = NULL;
952
953   space->write_byte(0xdffc + offset, data);
954   state->m_mapper[offset] = data;
955
956   // FIXME: the code below (which only handles the BIOS bankswitch at start) should be cleaned up
957   // currently, it's just a stripped down version of sms_mapper_w
958   if (state->m_BIOS)
959      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
960   else
961      SOURCE_BIOS = state->m_banking_none[1];
962
963   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
964      return;
965   else
966   {
967      if (!state->m_BIOS)
968         return;
969      SOURCE = SOURCE_BIOS;
970   }
971
972//  printf("write %x, %x\n", offset, data);
973
974   switch (offset)
975   {
976      case 0: /* Control */
977         if (!(data & 0x08)) /* it's not ram */
978         {
979            if (!(state->m_bios_port & IO_BIOS_ROM) && state->m_has_bios)
980            {
981               SOURCE = state->m_banking_bios[5];
982               memory_set_bankptr(space->machine(), "bank5", SOURCE);
983               memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
984            }
985         }
986         break;
987
988      case 1: /* Select 16k ROM bank for 0400-3FFF */
989         state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
990         memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
991         break;
992
993      case 2: /* Select 16k ROM bank for 4000-7FFF */
994         state->m_banking_bios[3] = SOURCE_BIOS;
995         memory_set_bankptr(space->machine(), "bank3", SOURCE);
996         memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
997         break;
998
999      case 3: /* Select 16k ROM bank for 8000-BFFF */
1000         state->m_banking_bios[5] = SOURCE_BIOS;
1001         if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
1002         {
1003            memory_set_bankptr(space->machine(), "bank5", SOURCE);
1004            memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
1005         }
1006         break;
1007   }
1008}
1009
1010WRITE8_HANDLER( sms_bios_w )
1011{
1012   sms_state *state = space->machine().driver_data<sms_state>();
1013   state->m_bios_port = data;
1014
1015   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(&space->device()));
1016
1017   setup_rom(space);
1018}
1019
1020
1021WRITE8_HANDLER( sms_cartram2_w )
1022{
1023   sms_state *state = space->machine().driver_data<sms_state>();
1024
1025   if (state->m_mapper[0] & 0x08)
1026   {
1027      logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000);
1028      if (state->m_mapper[0] & 0x04)
1029      {
1030         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x6000] = data;
1031      }
1032      else
1033      {
1034         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x2000] = data;
1035      }
1036   }
1037
1038   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1039   {
1040      state->m_cartridge[state->m_current_cartridge].cartRAM[state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000 + offset] = data;
1041   }
1042
1043   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
1044   {
1045      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1046      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
1047
1048      if (!state->m_cartridge[state->m_current_cartridge].ROM)
1049         return;
1050
1051      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1052      memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1053      memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1054      LOG(("rom 2 paged in %x (Korean mapper).\n", page));
1055   }
1056}
1057
1058
1059WRITE8_HANDLER( sms_cartram_w )
1060{
1061   sms_state *state = space->machine().driver_data<sms_state>();
1062   int page;
1063
1064   if (state->m_mapper[0] & 0x08)
1065   {
1066      logerror("write %02X to cartram at offset #%04X\n", data, offset);
1067      if (state->m_mapper[0] & 0x04)
1068      {
1069         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x4000] = data;
1070      }
1071      else
1072      {
1073         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset] = data;
1074      }
1075   }
1076   else
1077   {
1078      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
1079      {
1080         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1081         page = (rom_page_count > 0) ? data % rom_page_count : 0;
1082         if (!state->m_cartridge[state->m_current_cartridge].ROM)
1083            return;
1084         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1085         memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1086         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1087         LOG(("rom 2 paged in %x (Codemasters mapper).\n", page));
1088      }
1089      else if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1090      {
1091         state->m_cartridge[state->m_current_cartridge].cartRAM[offset & (state->m_cartridge[state->m_current_cartridge].ram_size - 1)] = data;
1092      }
1093      else
1094      {
1095         logerror("INVALID write %02X to cartram at offset #%04X\n", data, offset);
1096      }
1097   }
1098}
1099
1100
1101WRITE8_HANDLER( gg_sio_w )
1102{
1103   sms_state *state = space->machine().driver_data<sms_state>();
1104   logerror("*** write %02X to SIO register #%d\n", data, offset);
1105
1106   state->m_gg_sio[offset & 0x07] = data;
1107   switch (offset & 7)
1108   {
1109      case 0x00: /* Parallel Data */
1110         break;
1111
1112      case 0x01: /* Data Direction/ NMI Enable */
1113         break;
1114
1115      case 0x02: /* Serial Output */
1116         break;
1117
1118      case 0x03: /* Serial Input */
1119         break;
1120
1121      case 0x04: /* Serial Control / Status */
1122         break;
1123   }
1124}
1125
1126
1127READ8_HANDLER( gg_sio_r )
1128{
1129   sms_state *state = space->machine().driver_data<sms_state>();
1130   logerror("*** read SIO register #%d\n", offset);
1131
1132   switch (offset & 7)
1133   {
1134      case 0x00: /* Parallel Data */
1135         break;
1136
1137      case 0x01: /* Data Direction/ NMI Enable */
1138         break;
1139
1140      case 0x02: /* Serial Output */
1141         break;
1142
1143      case 0x03: /* Serial Input */
1144         break;
1145
1146      case 0x04: /* Serial Control / Status */
1147         break;
1148   }
1149
1150   return state->m_gg_sio[offset];
1151}
1152
1153static void sms_machine_stop( running_machine &machine )
1154{
1155   sms_state *state = machine.driver_data<sms_state>();
1156
1157   /* Does the cartridge have SRAM that should be saved? */
1158   if (state->m_cartridge[state->m_current_cartridge].sram_save) {
1159      device_image_interface *image = dynamic_cast<device_image_interface *>(machine.device("cart1"));
1160      image->battery_save(state->m_cartridge[state->m_current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
1161   }
1162}
1163
1164
1165static void setup_rom( address_space *space )
1166{
1167   sms_state *state = space->machine().driver_data<sms_state>();
1168   running_machine &machine = space->machine();
1169
1170   /* 1. set up bank pointers to point to nothing */
1171   memory_set_bankptr(machine, "bank1", state->m_banking_none[1]);
1172   memory_set_bankptr(machine, "bank2", state->m_banking_none[2]);
1173   memory_set_bankptr(machine, "bank3", state->m_banking_none[3]);
1174   memory_set_bankptr(machine, "bank4", state->m_banking_none[3] + 0x2000);
1175   memory_set_bankptr(machine, "bank5", state->m_banking_none[5]);
1176   memory_set_bankptr(machine, "bank6", state->m_banking_none[5] + 0x2000);
1177
1178   /* 2. check and set up expansion port */
1179   if (!(state->m_bios_port & IO_EXPANSION) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_CARD))
1180   {
1181      /* TODO: Implement me */
1182      logerror("Switching to unsupported expansion port.\n");
1183   }
1184
1185   /* 3. check and set up card rom */
1186   if (!(state->m_bios_port & IO_CARD) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_EXPANSION))
1187   {
1188      /* TODO: Implement me */
1189      logerror("Switching to unsupported card rom port.\n");
1190   }
1191
1192   /* 4. check and set up cartridge rom */
1193   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || state->m_is_gamegear) { */
1194   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
1195   if ((!(state->m_bios_port & IO_CARTRIDGE)) || state->m_is_gamegear)
1196   {
1197      memory_set_bankptr(machine, "bank1", state->m_banking_cart[1]);
1198      memory_set_bankptr(machine, "bank2", state->m_banking_cart[2]);
1199      memory_set_bankptr(machine, "bank3", state->m_banking_cart[3]);
1200      memory_set_bankptr(machine, "bank4", state->m_banking_cart[3] + 0x2000);
1201      memory_set_bankptr(machine, "bank5", state->m_banking_cart[5]);
1202      memory_set_bankptr(machine, "bank6", state->m_banking_cart[5] + 0x2000);
1203      logerror("Switched in cartridge rom.\n");
1204   }
1205
1206   /* 5. check and set up bios rom */
1207   if (!(state->m_bios_port & IO_BIOS_ROM))
1208   {
1209      /* 0x0400 bioses */
1210      if (state->m_has_bios_0400)
1211      {
1212         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1213         logerror("Switched in 0x0400 bios.\n");
1214      }
1215      /* 0x2000 bioses */
1216      if (state->m_has_bios_2000)
1217      {
1218         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1219         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1220         logerror("Switched in 0x2000 bios.\n");
1221      }
1222      if (state->m_has_bios_full)
1223      {
1224         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1225         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1226         memory_set_bankptr(machine, "bank3", state->m_banking_bios[3]);
1227         memory_set_bankptr(machine, "bank4", state->m_banking_bios[3] + 0x2000);
1228         memory_set_bankptr(machine, "bank5", state->m_banking_bios[5]);
1229         memory_set_bankptr(machine, "bank6", state->m_banking_bios[5] + 0x2000);
1230         logerror("Switched in full bios.\n");
1231      }
1232   }
1233
1234   if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1235   {
1236      memory_set_bankptr(machine, "bank5", state->m_cartridge[state->m_current_cartridge].cartRAM);
1237      memory_set_bankptr(machine, "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM);
1238   }
1239}
1240
1241
1242static int sms_verify_cart( UINT8 *magic, int size )
1243{
1244   int retval;
1245
1246   retval = IMAGE_VERIFY_FAIL;
1247
1248   /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */
1249   if (size >= 0x8000)
1250   {
1251      if (!strncmp((char*)&magic[0x7ff0], "TMR SEGA", 8))
1252      {
1253#if 0
1254         /* Technically, it should be this, but remove for now until verified: */
1255         if (!strcmp(sysname, "gamegear"))
1256         {
1257            if ((unsigned char)magic[0x7ffd] < 0x50)
1258               retval = IMAGE_VERIFY_PASS;
1259         }
1260         if (!strcmp(sysname, "sms"))
1261         {
1262            if ((unsigned char)magic[0x7ffd] >= 0x50)
1263               retval = IMAGE_VERIFY_PASS;
1264         }
1265#endif
1266         retval = IMAGE_VERIFY_PASS;
1267      }
1268
1269#if 0
1270      /* Check at $81f0 also */
1271      if (!retval)
1272      {
1273         if (!strncmp(&magic[0x81f0], "TMR SEGA", 8))
1274         {
1275#if 0
1276            /* Technically, it should be this, but remove for now until verified: */
1277            if (!strcmp(sysname, "gamegear"))
1278            {
1279               if ((unsigned char)magic[0x81fd] < 0x50)
1280                  retval = IMAGE_VERIFY_PASS;
1281            }
1282            if (!strcmp(sysname, "sms"))
1283            {
1284               if ((unsigned char)magic[0x81fd] >= 0x50)
1285                  retval = IMAGE_VERIFY_PASS;
1286            }
1287#endif
1288            retval = IMAGE_VERIFY_PASS;
1289         }
1290      }
1291#endif
1292
1293   }
1294
1295   return retval;
1296}
1297
1298#ifdef UNUSED_FUNCTION
1299// For the moment we switch to a different detection routine which allows to detect
1300// in a single run Codemasters mapper, Korean mapper (including Jang Pung 3 which
1301// uses a diff signature then the one below here) and Zemina mapper (used by Wonsiin, etc.).
1302// I leave these here to document alt. detection routines and in the case these functions
1303// can be updated
1304
1305/* Check for Codemasters mapper
1306  0x7FE3 - 93 - sms Cosmis Spacehead
1307              - sms Dinobasher
1308              - sms The Excellent Dizzy Collection
1309              - sms Fantastic Dizzy
1310              - sms Micro Machines
1311              - gamegear Cosmic Spacehead
1312              - gamegear Micro Machines
1313         - 94 - gamegear Dropzone
1314              - gamegear Ernie Els Golf (also has 64KB additional RAM on the cartridge)
1315              - gamegear Pete Sampras Tennis
1316              - gamegear S.S. Lucifer
1317         - 95 - gamegear Micro Machines 2 - Turbo Tournament
1318
1319The Korean game Jang Pung II also seems to use a codemasters style mapper.
1320 */
1321static int detect_codemasters_mapper( UINT8 *rom )
1322{
1323   static const UINT8 jang_pung2[16] = { 0x00, 0xba, 0x38, 0x0d, 0x00, 0xb8, 0x38, 0x0c, 0x00, 0xb6, 0x38, 0x0b, 0x00, 0xb4, 0x38, 0x0a };
1324
1325   if (((rom[0x7fe0] & 0x0f ) <= 9) && (rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95) &&  rom[0x7fef] == 0x00)
1326      return 1;
1327
1328   if (!memcmp(&rom[0x7ff0], jang_pung2, 16))
1329      return 1;
1330
1331   return 0;
1332}
1333
1334
1335static int detect_korean_mapper( UINT8 *rom )
1336{
1337   static const UINT8 signatures[2][16] =
1338   {
1339      { 0x3e, 0x11, 0x32, 0x00, 0xa0, 0x78, 0xcd, 0x84, 0x85, 0x3e, 0x02, 0x32, 0x00, 0xa0, 0xc9, 0xff }, /* Dodgeball King */
1340      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
1341   };
1342   int i;
1343
1344   for (i = 0; i < 2; i++)
1345   {
1346      if (!memcmp(&rom[0x7ff0], signatures[i], 16))
1347      {
1348         return 1;
1349      }
1350   }
1351   return 0;
1352}
1353#endif
1354
1355
1356static int detect_tvdraw( UINT8 *rom )
1357{
1358   static const UINT8 terebi_oekaki[7] = { 0x61, 0x6e, 0x6e, 0x61, 0x6b, 0x6d, 0x6e };   // "annakmn"
1359
1360   if (!memcmp(&rom[0x13b3], terebi_oekaki, 7))
1361      return 1;
1362
1363   return 0;
1364}
1365
1366
1367static int detect_lphaser_xoffset( running_machine &machine, UINT8 *rom )
1368{
1369   sms_state *state = machine.driver_data<sms_state>();
1370
1371   static const UINT8 signatures[6][16] =
1372   {
1373      /* Spacegun */
1374      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9d, 0x99, 0x10, 0x90, 0x00, 0x40 },
1375      /* Gangster Town */
1376      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x19, 0x87, 0x1b, 0xc9, 0x74, 0x50, 0x00, 0x4f },
1377      /* Shooting Gallery */
1378      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0x8a, 0x3a, 0x72, 0x50, 0x00, 0x4f },
1379      /* Rescue Mission */
1380      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0xfb, 0xd3, 0x06, 0x51, 0x00, 0x4f },
1381      /* Laser Ghost */
1382      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x00, 0x00, 0xb7, 0x55, 0x74, 0x70, 0x00, 0x40 },
1383      /* Assault City */
1384      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9f, 0x74, 0x34, 0x70, 0x00, 0x40 },
1385   };
1386
1387   if (!(state->m_bios_port & IO_CARTRIDGE) && state->m_cartridge[state->m_current_cartridge].size >= 0x8000)
1388   {
1389      if (!memcmp(&rom[0x7ff0], signatures[0], 16) || !memcmp(&rom[0x7ff0], signatures[1], 16))
1390         return 40;
1391
1392      if (!memcmp(&rom[0x7ff0], signatures[2], 16))
1393         return 49;
1394
1395      if (!memcmp(&rom[0x7ff0], signatures[3], 16))
1396         return 47;
1397
1398      if (!memcmp(&rom[0x7ff0], signatures[4], 16))
1399         return 44;
1400
1401      if (!memcmp(&rom[0x7ff0], signatures[5], 16))
1402         return 53;
1403
1404   }
1405   return 50;
1406}
1407
1408
1409DEVICE_START( sms_cart )
1410{
1411   sms_state *state = device->machine().driver_data<sms_state>();
1412   int i;
1413
1414   for (i = 0; i < MAX_CARTRIDGES; i++)
1415   {
1416      state->m_cartridge[i].ROM = NULL;
1417      state->m_cartridge[i].size = 0;
1418      state->m_cartridge[i].features = 0;
1419      state->m_cartridge[i].cartSRAM = NULL;
1420      state->m_cartridge[i].sram_save = 0;
1421      state->m_cartridge[i].cartRAM = NULL;
1422      state->m_cartridge[i].ram_size = 0;
1423      state->m_cartridge[i].ram_page = 0;
1424   }
1425   state->m_current_cartridge = 0;
1426
1427   state->m_bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1428   if (!state->m_is_gamegear && !state->m_has_bios)
1429   {
1430      state->m_bios_port &= ~(IO_CARTRIDGE);
1431      state->m_bios_port |= IO_BIOS_ROM;
1432   }
1433}
1434
1435
1436DEVICE_IMAGE_LOAD( sms_cart )
1437{
1438   running_machine &machine = image.device().machine();
1439   sms_state *state = machine.driver_data<sms_state>();
1440   int size, index = 0, offset = 0;
1441   const char *extrainfo = NULL;
1442
1443   if (strcmp(image.device().tag(), "cart1") == 0)
1444      index = 0;
1445   if (strcmp(image.device().tag(), "cart2") == 0)
1446      index = 1;
1447   if (strcmp(image.device().tag(), "cart3") == 0)
1448      index = 2;
1449   if (strcmp(image.device().tag(), "cart4") == 0)
1450      index = 3;
1451   if (strcmp(image.device().tag(), "cart5") == 0)
1452      index = 4;
1453   if (strcmp(image.device().tag(), "cart6") == 0)
1454      index = 5;
1455   if (strcmp(image.device().tag(), "cart7") == 0)
1456      index = 6;
1457   if (strcmp(image.device().tag(), "cart8") == 0)
1458      index = 7;
1459   if (strcmp(image.device().tag(), "cart9") == 0)
1460      index = 8;
1461   if (strcmp(image.device().tag(), "cart10") == 0)
1462      index = 9;
1463   if (strcmp(image.device().tag(), "cart11") == 0)
1464      index = 10;
1465   if (strcmp(image.device().tag(), "cart12") == 0)
1466      index = 11;
1467   if (strcmp(image.device().tag(), "cart13") == 0)
1468      index = 12;
1469   if (strcmp(image.device().tag(), "cart14") == 0)
1470      index = 13;
1471   if (strcmp(image.device().tag(), "cart15") == 0)
1472      index = 14;
1473   if (strcmp(image.device().tag(), "cart16") == 0)
1474      index = 15;
1475
1476   state->m_cartridge[index].features = 0;
1477
1478   if (image.software_entry() == NULL)
1479   {
1480      size = image.length();
1481      extrainfo = hashfile_extrainfo(image);
1482   }
1483   else
1484      size = image.get_software_region_length("rom");
1485
1486   /* Check for 512-byte header */
1487   if ((size / 512) & 1)
1488   {
1489      offset = 512;
1490      size -= 512;
1491   }
1492
1493   if (!size)
1494   {
1495      image.seterror(IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small");
1496      return IMAGE_INIT_FAIL;
1497   }
1498
1499   /* Create a new memory region to hold the ROM. */
1500   /* Make sure the region holds only complete (0x4000) rom banks */
1501   state->m_cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1502   state->m_cartridge[index].ROM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].size);
1503   state->m_cartridge[index].cartSRAM = auto_alloc_array(machine, UINT8, NVRAM_SIZE);
1504
1505   /* Load ROM banks */
1506   if (image.software_entry() == NULL)
1507   {
1508      image.fseek(offset, SEEK_SET);
1509
1510      if (image.fread( state->m_cartridge[index].ROM, size) != size)
1511         return IMAGE_INIT_FAIL;
1512   }
1513   else
1514   {
1515      memcpy(state->m_cartridge[index].ROM, image.get_software_region("rom") + offset, size);
1516
1517      // check for mapper feature (currently only KOREAN_NOBANK, but eventually we will add the other mappers as well)
1518      if (image.get_feature("pcb") != NULL)
1519      {
1520         const char *mapper = image.get_feature("pcb");
1521         if (!strcmp(mapper, "korean_nobank"))
1522            state->m_cartridge[index].features |= CF_KOREAN_NOBANK_MAPPER;
1523      }
1524   }
1525
1526   /* check the image */
1527   if (!state->m_has_bios)
1528   {
1529      if (sms_verify_cart(state->m_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
1530      {
1531         logerror("Warning loading image: sms_verify_cart failed\n");
1532      }
1533   }
1534
1535   /* Detect special features from the extrainfo field */
1536   if (extrainfo)
1537   {
1538      /* Check for codemasters mapper */
1539      if (strstr(extrainfo, "CODEMASTERS"))
1540         state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1541
1542      /* Check for korean mapper */
1543      if (strstr(extrainfo, "KOREAN"))
1544         state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1545
1546      /* Check for special SMS Compatibility mode gamegear cartridges */
1547      if (state->m_is_gamegear && strstr(extrainfo, "GGSMS"))
1548         state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1549
1550      /* Check for 93C46 eeprom */
1551      if (strstr(extrainfo, "93C46"))
1552         state->m_cartridge[index].features |= CF_93C46_EEPROM;
1553
1554      /* Check for 8KB on-cart RAM */
1555      if (strstr(extrainfo, "8KB_CART_RAM"))
1556      {
1557         state->m_cartridge[index].features |= CF_ONCART_RAM;
1558         state->m_cartridge[index].ram_size = 0x2000;
1559         state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1560      }
1561   }
1562   else
1563   {
1564      /* If no extrainfo information is available try to find special information out on our own */
1565      /* Check for special cartridge features (new routine, courtesy of Omar Cornut, from MEKA)  */
1566      if (size >= 0x8000)
1567      {
1568         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, i;
1569         for (i = 0; i < 0x8000; i++)
1570         {
1571            if (state->m_cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
1572            {
1573               UINT16 addr = (state->m_cartridge[index].ROM[i + 2] << 8) | state->m_cartridge[index].ROM[i + 1];
1574               if (addr == 0xFFFF)
1575               { i += 2; cFFFF++; continue; }
1576               if (addr == 0x0002 || addr == 0x0003 || addr == 0x0004)
1577               { i += 2; c0002++; continue; }
1578               if (addr == 0x8000)
1579               { i += 2; c8000++; continue; }
1580               if (addr == 0xA000)
1581               { i += 2; cA000++; continue; }
1582            }
1583         }
1584
1585         LOG(("Mapper test: c002 = %d, c8000 = %d, cA000 = %d, cFFFF = %d\n", c0002, c8000, cA000, cFFFF));
1586
1587         // 2 is a security measure, although tests on existing ROM showed it was not needed
1588         if (c0002 > cFFFF + 2 || (c0002 > 0 && cFFFF == 0))
1589            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1590         else if (c8000 > cFFFF + 2 || (c8000 > 0 && cFFFF == 0))
1591            state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1592         else if (cA000 > cFFFF + 2 || (cA000 > 0 && cFFFF == 0))
1593            state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1594
1595      }
1596
1597      /* Check for special SMS Compatibility mode gamegear cartridges */
1598      if (state->m_is_gamegear && image.software_entry() == NULL)   // not sure about how to handle this with softlists
1599      {
1600         /* Just in case someone passes us an sms file */
1601         if (!mame_stricmp (image.filetype(), "sms"))
1602            state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1603      }
1604   }
1605
1606   if (state->m_cartridge[index].features & CF_CODEMASTERS_MAPPER)
1607   {
1608      state->m_cartridge[index].ram_size = 0x10000;
1609      state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1610      state->m_cartridge[index].ram_page = 0;
1611   }
1612
1613   /* For Light Phaser games, we have to detect the x offset */
1614   state->m_lphaser_x_offs = detect_lphaser_xoffset(machine, state->m_cartridge[index].ROM);
1615
1616   /* Terebi Oekaki (TV Draw) is a SG1000 game with special input device which is compatible with SG1000 Mark III */
1617   if ((detect_tvdraw(state->m_cartridge[index].ROM)) && state->m_is_region_japan)
1618   {
1619      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1620      program->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_tvdraw_axis_w));
1621      program->install_legacy_read_handler(0x8000, 0x8000, FUNC(sms_tvdraw_status_r));
1622      program->install_legacy_read_handler(0xa000, 0xa000, FUNC(sms_tvdraw_data_r));
1623      program->nop_write(0xa000, 0xa000);
1624   }
1625
1626   if (state->m_cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
1627   {
1628      // FIXME: we should have by default FFFD-FFFF to be only a mirror for DFFD-DFFF (with no bankswitch logic)
1629      // and then the handlers should be installed here for all but the KOREAN_NOBANK carts
1630      // However, to avoid memory map breakage, we currently go the other way around
1631      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1632      program->install_legacy_readwrite_handler(0xfffc, 0xffff, FUNC(sms_kor_nobank_r), FUNC(sms_kor_nobank_w));
1633   }
1634
1635   LOG(("Cart Features: %x\n", state->m_cartridge[index].features));
1636
1637   /* Load battery backed RAM, if available */
1638   image.battery_load(state->m_cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE, 0x00);
1639
1640   return IMAGE_INIT_PASS;
1641}
1642
1643
1644static void setup_cart_banks( running_machine &machine )
1645{
1646   sms_state *state = machine.driver_data<sms_state>();
1647   if (state->m_cartridge[state->m_current_cartridge].ROM)
1648   {
1649      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1650      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM;
1651      state->m_banking_cart[2] = state->m_cartridge[state->m_current_cartridge].ROM + 0x0400;
1652      state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1653      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
1654      /* Codemasters mapper points to bank 0 for page 2 */
1655      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1656      {
1657         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM;
1658      }
1659   }
1660   else
1661   {
1662      state->m_banking_cart[1] = state->m_banking_none[1];
1663      state->m_banking_cart[2] = state->m_banking_none[2];
1664      state->m_banking_cart[3] = state->m_banking_none[3];
1665      state->m_banking_cart[5] = state->m_banking_none[5];
1666   }
1667}
1668
1669#ifdef MESS
1670static void setup_banks( running_machine &machine )
1671{
1672   sms_state *state = machine.driver_data<sms_state>();
1673   UINT8 *mem = machine.region("maincpu")->base();
1674   state->m_banking_bios[1] = state->m_banking_cart[1] = state->m_banking_none[1] = mem;
1675   state->m_banking_bios[2] = state->m_banking_cart[2] = state->m_banking_none[2] = mem;
1676   state->m_banking_bios[3] = state->m_banking_cart[3] = state->m_banking_none[3] = mem;
1677   state->m_banking_bios[4] = state->m_banking_cart[4] = state->m_banking_none[4] = mem;
1678   state->m_banking_bios[5] = state->m_banking_cart[5] = state->m_banking_none[5] = mem;
1679   state->m_banking_bios[6] = state->m_banking_cart[6] = state->m_banking_none[6] = mem;
1680
1681   state->m_BIOS = machine.region("user1")->base();
1682
1683   state->m_bios_page_count = (state->m_BIOS ? machine.region("user1")->bytes() / 0x4000 : 0);
1684
1685   setup_cart_banks(machine);
1686
1687   if (state->m_BIOS == NULL || state->m_BIOS[0] == 0x00)
1688   {
1689      state->m_BIOS = NULL;
1690      state->m_bios_port |= IO_BIOS_ROM;
1691   }
1692
1693   if (state->m_BIOS)
1694   {
1695      state->m_banking_bios[1] = state->m_BIOS;
1696      state->m_banking_bios[2] = state->m_BIOS + 0x0400;
1697      state->m_banking_bios[3] = state->m_BIOS + ((1 < state->m_bios_page_count) ? 0x4000 : 0);
1698      state->m_banking_bios[5] = state->m_BIOS + ((2 < state->m_bios_page_count) ? 0x8000 : 0);
1699   }
1700}
1701#endif
1702
1703MACHINE_START( sms )
1704{
1705   sms_state *state = machine.driver_data<sms_state>();
1706
1707   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(sms_machine_stop),&machine));
1708   state->m_rapid_fire_timer = machine.scheduler().timer_alloc(FUNC(rapid_fire_callback));
1709   state->m_rapid_fire_timer->adjust(attotime::from_hz(10), 0, attotime::from_hz(10));
1710
1711   state->m_lphaser_1_timer = machine.scheduler().timer_alloc(FUNC(lphaser_1_callback));
1712   state->m_lphaser_2_timer = machine.scheduler().timer_alloc(FUNC(lphaser_2_callback));
1713
1714   state->m_main_cpu = machine.device("maincpu");
1715   state->m_control_cpu = machine.device("control");
1716   state->m_vdp = machine.device("sms_vdp");
1717   state->m_ym = machine.device("ym2413");
1718   state->m_main_scr = machine.device("screen");
1719   state->m_left_lcd = machine.device("left_lcd");
1720   state->m_right_lcd = machine.device("right_lcd");
1721
1722   /* Check if lightgun has been chosen as input: if so, enable crosshair */
1723   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
1724}
1725
1726#ifdef MESS
1727MACHINE_RESET( sms )
1728{
1729   address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
1730   sms_state *state = machine.driver_data<sms_state>();
1731
1732   state->m_ctrl_reg = 0xff;
1733   if (state->m_has_fm)
1734      state->m_fm_detect = 0x01;
1735
1736   state->m_mapper_ram = (UINT8*)space->get_write_ptr(0xdffc);
1737
1738   state->m_bios_port = 0;
1739
1740   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1741   {
1742      /* Install special memory handlers */
1743      space->install_legacy_write_handler(0x0000, 0x0000, FUNC(sms_codemasters_page0_w));
1744      space->install_legacy_write_handler(0x4000, 0x4000, FUNC(sms_codemasters_page1_w));
1745   }
1746
1747   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
1748      space->install_legacy_write_handler(0x0000, 0x0003, FUNC(sms_korean_zemina_banksw_w));
1749
1750   if (state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE)
1751      sms_vdp_set_ggsmsmode(state->m_vdp, 1);
1752
1753   /* Initialize SIO stuff for GG */
1754   state->m_gg_sio[0] = 0x7f;
1755   state->m_gg_sio[1] = 0xff;
1756   state->m_gg_sio[2] = 0x00;
1757   state->m_gg_sio[3] = 0xff;
1758   state->m_gg_sio[4] = 0x00;
1759
1760   state->m_store_control = 0;
1761
1762   setup_banks(machine);
1763
1764   setup_rom(space);
1765
1766   state->m_rapid_fire_state_1 = 0;
1767   state->m_rapid_fire_state_2 = 0;
1768
1769   state->m_last_paddle_read_time = 0;
1770   state->m_paddle_read_state = 0;
1771
1772   state->m_last_sports_pad_time_1 = 0;
1773   state->m_last_sports_pad_time_2 = 0;
1774   state->m_sports_pad_state_1 = 0;
1775   state->m_sports_pad_state_2 = 0;
1776   state->m_sports_pad_last_data_1 = 0;
1777   state->m_sports_pad_last_data_2 = 0;
1778   state->m_sports_pad_1_x = 0;
1779   state->m_sports_pad_1_y = 0;
1780   state->m_sports_pad_2_x = 0;
1781   state->m_sports_pad_2_y = 0;
1782
1783   state->m_lphaser_1_latch = 0;
1784   state->m_lphaser_2_latch = 0;
1785
1786   state->m_sscope_state = 0;
1787
1788   state->m_tvdraw_data = 0;
1789}
1790#endif
1791
1792READ8_HANDLER( sms_store_cart_select_r )
1793{
1794   return 0xff;
1795}
1796
1797
1798WRITE8_HANDLER( sms_store_cart_select_w )
1799{
1800   sms_state *state = space->machine().driver_data<sms_state>();
1801   UINT8 slot = data >> 4;
1802   UINT8 slottype = data & 0x08;
1803
1804   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
1805   /* cartridge? slot #0 */
1806   if (slottype == 0)
1807      state->m_current_cartridge = slot;
1808
1809   setup_cart_banks(space->machine());
1810   memory_set_bankptr(space->machine(), "bank10", state->m_banking_cart[3] + 0x2000);
1811   setup_rom(space);
1812}
1813
1814
1815READ8_HANDLER( sms_store_select1 )
1816{
1817   return 0xff;
1818}
1819
1820
1821READ8_HANDLER( sms_store_select2 )
1822{
1823   return 0xff;
1824}
1825
1826
1827READ8_HANDLER( sms_store_control_r )
1828{
1829   sms_state *state = space->machine().driver_data<sms_state>();
1830   return state->m_store_control;
1831}
1832
1833
1834WRITE8_HANDLER( sms_store_control_w )
1835{
1836   sms_state *state = space->machine().driver_data<sms_state>();
1837   logerror("0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(&space->device()), data);
1838   if (data & 0x02)
1839   {
1840      space->machine().device<cpu_device>("maincpu")->resume(SUSPEND_REASON_HALT);
1841   }
1842   else
1843   {
1844      /* Pull reset line of CPU #0 low */
1845      space->machine().device("maincpu")->reset();
1846      space->machine().device<cpu_device>("maincpu")->suspend(SUSPEND_REASON_HALT, 1);
1847   }
1848   state->m_store_control = data;
1849}
1850
1851void sms_store_int_callback( running_machine &machine, int state )
1852{
1853   sms_state *driver_state = machine.driver_data<sms_state>();
1854   device_set_input_line(driver_state->m_store_control & 0x01 ? driver_state->m_control_cpu : driver_state->m_main_cpu, 0, state);
1855}
1856
1857
1858static void sms_set_zero_flag( running_machine &machine )
1859{
1860   sms_state *state = machine.driver_data<sms_state>();
1861   state->m_is_gamegear = 0;
1862   state->m_is_region_japan = 0;
1863   state->m_has_bios_0400 = 0;
1864   state->m_has_bios_2000 = 0;
1865   state->m_has_bios_full = 0;
1866   state->m_has_bios = 0;
1867   state->m_has_fm = 0;
1868}
1869
1870DRIVER_INIT( sg1000m3 )
1871{
1872   sms_state *state = machine.driver_data<sms_state>();
1873   sms_set_zero_flag(machine);
1874   state->m_is_region_japan = 1;
1875   state->m_has_fm = 1;
1876}
1877
1878
1879DRIVER_INIT( sms1 )
1880{
1881   sms_state *state = machine.driver_data<sms_state>();
1882   sms_set_zero_flag(machine);
1883   state->m_has_bios_full = 1;
1884}
1885
1886
1887DRIVER_INIT( smsj )
1888{
1889   sms_state *state = machine.driver_data<sms_state>();
1890   sms_set_zero_flag(machine);
1891   state->m_is_region_japan = 1;
1892   state->m_has_bios_2000 = 1;
1893   state->m_has_fm = 1;
1894}
1895
1896
1897DRIVER_INIT( sms2kr )
1898{
1899   sms_state *state = machine.driver_data<sms_state>();
1900   sms_set_zero_flag(machine);
1901   state->m_is_region_japan = 1;
1902   state->m_has_bios_full = 1;
1903   state->m_has_fm = 1;
1904}
1905
1906
1907DRIVER_INIT( smssdisp )
1908{
1909   sms_set_zero_flag(machine);
1910}
1911
1912
1913DRIVER_INIT( gamegear )
1914{
1915   sms_state *state = machine.driver_data<sms_state>();
1916   sms_set_zero_flag(machine);
1917   state->m_is_gamegear = 1;
1918}
1919
1920
1921DRIVER_INIT( gamegeaj )
1922{
1923   sms_state *state = machine.driver_data<sms_state>();
1924   sms_set_zero_flag(machine);
1925   state->m_is_region_japan = 1;
1926   state->m_is_gamegear = 1;
1927   state->m_has_bios_0400 = 1;
1928}
1929
1930
1931static void sms_black_bitmap( const screen_device *screen, bitmap_t *bitmap )
1932{
1933   const int width = screen->width();
1934   const int height = screen->height();
1935   int x, y;
1936
1937   for (y = 0; y < height; y++)
1938      for (x = 0; x < width; x++)
1939         *BITMAP_ADDR32(bitmap, y, x) = MAKE_RGB(0,0,0);
1940}
1941
1942VIDEO_START( sms1 )
1943{
1944   sms_state *state = machine.driver_data<sms_state>();
1945   screen_device *screen = machine.first_screen();
1946   int width = screen->width();
1947   int height = screen->height();
1948
1949   state->m_prevleft_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1950   state->m_prevright_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1951   state->save_item(NAME(*state->m_prevleft_bitmap));
1952   state->save_item(NAME(*state->m_prevright_bitmap));
1953}
1954
1955SCREEN_UPDATE( sms1 )
1956{
1957   sms_state *state = screen->machine().driver_data<sms_state>();
1958   UINT8 sscope = input_port_read_safe(screen->machine(), "SEGASCOPE", 0x00);
1959   UINT8 sscope_binocular_hack = input_port_read_safe(screen->machine(), "SSCOPE_BINOCULAR", 0x00);
1960   UINT8 occluded_view = 0;
1961
1962   // without SegaScope, both LCDs for glasses go black
1963   if ((screen != state->m_main_scr) && !sscope)
1964      occluded_view = 1;
1965
1966   // with SegaScope, sscope_state 0 = left screen OFF, right screen ON
1967   if (!(state->m_sscope_state & 0x01) && (screen == state->m_left_lcd))
1968      occluded_view = 1;
1969
1970   // with SegaScope, sscope_state 1 = left screen ON, right screen OFF
1971   if ((state->m_sscope_state & 0x01) && (screen == state->m_right_lcd))
1972      occluded_view = 1;
1973
1974   if (!occluded_view)
1975   {
1976      sms_vdp_update(state->m_vdp, bitmap, cliprect);
1977
1978      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1979      // save a copy of current bitmap for the binocular hack
1980      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1981         copybitmap(state->m_prevleft_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1982      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1983         copybitmap(state->m_prevright_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1984   }
1985   else
1986   {
1987      sms_black_bitmap(screen, bitmap);
1988
1989      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1990      // use the copied bitmap for the binocular hack
1991      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1992         copybitmap(bitmap, state->m_prevleft_bitmap, 0, 0, 0, 0, cliprect);
1993      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1994         copybitmap(bitmap, state->m_prevright_bitmap, 0, 0, 0, 0, cliprect);
1995   }
1996
1997   return 0;
1998}
1999
2000SCREEN_UPDATE( sms )
2001{
2002   sms_state *state = screen->machine().driver_data<sms_state>();
2003   sms_vdp_update(state->m_vdp, bitmap, cliprect);
2004   return 0;
2005}
2006
2007VIDEO_START( gamegear )
2008{
2009   sms_state *state = machine.driver_data<sms_state>();
2010   screen_device *screen = machine.first_screen();
2011   int width = screen->width();
2012   int height = screen->height();
2013
2014   state->m_prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2015   state->m_tmp_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2016   state->save_item(NAME(*state->m_prev_bitmap));
2017}
2018
2019SCREEN_UPDATE( gamegear )
2020{
2021   sms_state *state = screen->machine().driver_data<sms_state>();
2022   int width = screen->width();
2023   int height = screen->height();
2024   int x, y;
2025
2026   sms_vdp_update(state->m_vdp, state->m_tmp_bitmap, cliprect);
2027
2028   // HACK: fake LCD persistence effect
2029   // (it would be better to generalize this in the core, to be used for all LCD systems)
2030   for (y = 0; y < height; y++)
2031   {
2032      UINT32 *line0 = BITMAP_ADDR32(state->m_tmp_bitmap, y, 0);
2033      UINT32 *line1 = BITMAP_ADDR32(state->m_prev_bitmap, y, 0);
2034      for (x = 0; x < width; x++)
2035      {
2036         UINT32 color0 = line0[x];
2037         UINT32 color1 = line1[x];
2038         UINT16 r0 = (color0 >> 16) & 0x000000ff;
2039         UINT16 g0 = (color0 >>  8) & 0x000000ff;
2040         UINT16 b0 = (color0 >>  0) & 0x000000ff;
2041         UINT16 r1 = (color1 >> 16) & 0x000000ff;
2042         UINT16 g1 = (color1 >>  8) & 0x000000ff;
2043         UINT16 b1 = (color1 >>  0) & 0x000000ff;
2044         UINT8 r = (UINT8)((r0 + r1) >> 1);
2045         UINT8 g = (UINT8)((g0 + g1) >> 1);
2046         UINT8 b = (UINT8)((b0 + b1) >> 1);
2047         *BITMAP_ADDR32(bitmap, y, x) = (r << 16) | (g << 8) | b;
2048      }
2049   }
2050   copybitmap(state->m_prev_bitmap, state->m_tmp_bitmap, 0, 0, 0, 0, cliprect);
2051
2052   return 0;
2053}
src/mame/includes/segasms.h
r11572r11573
1/*****************************************************************************
2 *
3 * includes/sms.h
4 *
5 ****************************************************************************/
6
7#ifndef SMS_H_
8#define SMS_H_
9
10#define LOG_REG
11#define LOG_PAGING
12#define LOG_COLOR
13
14#define NVRAM_SIZE             (0x08000)
15#define CPU_ADDRESSABLE_SIZE   (0x10000)
16
17#define MAX_CARTRIDGES        16
18
19class sms_state : public driver_device
20{
21public:
22   sms_state(const machine_config &mconfig, device_type type, const char *tag)
23   : driver_device(mconfig, type, tag) { }
24
25   // device_ts
26   device_t *m_main_cpu;
27   device_t *m_control_cpu;
28   device_t *m_vdp;
29   device_t *m_ym;
30   device_t *m_main_scr;
31   device_t *m_left_lcd;
32   device_t *m_right_lcd;
33
34   UINT8 m_bios_page_count;
35   UINT8 m_fm_detect;
36   UINT8 m_ctrl_reg;
37   int m_paused;
38   UINT8 m_bios_port;
39   UINT8 *m_BIOS;
40   UINT8 *m_mapper_ram;
41   UINT8 m_mapper[4];
42   // we are going to use 1-6, same as bank numbers. Notice, though, that most mappers
43   // only work on 16K banks and, hence, banks 4-6 are not always directly set
44   // (they often use bank3 + 0x2000 and bank5 + 0x2000)
45   UINT8 *m_banking_bios[7];
46   UINT8 *m_banking_cart[7];
47   UINT8 *m_banking_none[7];
48   UINT8 m_gg_sio[5];
49   UINT8 m_store_control;
50   UINT8 m_input_port0;
51   UINT8 m_input_port1;
52
53   // for gamegear LCD persistence hack
54   bitmap_t *m_tmp_bitmap;
55   bitmap_t *m_prev_bitmap;
56
57   // for 3D glass binocular hack
58   bitmap_t *m_prevleft_bitmap;
59   bitmap_t *m_prevright_bitmap;
60
61   /* Model identifiers */
62   UINT8 m_is_gamegear;
63   UINT8 m_is_region_japan;
64   UINT8 m_has_bios_0400;
65   UINT8 m_has_bios_2000;
66   UINT8 m_has_bios_full;
67   UINT8 m_has_bios;
68   UINT8 m_has_fm;
69
70   /* Data needed for Rapid Fire Unit support */
71   emu_timer *m_rapid_fire_timer;
72   UINT8 m_rapid_fire_state_1;
73   UINT8 m_rapid_fire_state_2;
74
75   /* Data needed for Paddle Control controller */
76   UINT32 m_last_paddle_read_time;
77   UINT8 m_paddle_read_state;
78
79   /* Data needed for Sports Pad controller */
80   UINT32 m_last_sports_pad_time_1;
81   UINT32 m_last_sports_pad_time_2;
82   UINT8 m_sports_pad_state_1;
83   UINT8 m_sports_pad_state_2;
84   UINT8 m_sports_pad_last_data_1;
85   UINT8 m_sports_pad_last_data_2;
86   UINT8 m_sports_pad_1_x;
87   UINT8 m_sports_pad_1_y;
88   UINT8 m_sports_pad_2_x;
89   UINT8 m_sports_pad_2_y;
90
91   /* Data needed for Light Phaser */
92   emu_timer *m_lphaser_1_timer;
93   emu_timer *m_lphaser_2_timer;
94   UINT8 m_lphaser_1_latch;
95   UINT8 m_lphaser_2_latch;
96   int m_lphaser_x_offs;   /* Needed to 'calibrate' lphaser; set at cart loading */
97
98   /* Data needed for SegaScope (3D glasses) */
99   UINT8 m_sscope_state;
100
101   /* Data needed for Terebi Oekaki (TV Draw) */
102   UINT8 m_tvdraw_data;
103
104   /* Cartridge slot info */
105   UINT8 m_current_cartridge;
106   struct
107   {
108      UINT8 *ROM;        /* Pointer to ROM image data */
109      UINT32 size;       /* Size of the ROM image */
110      UINT8 features;    /* on-cartridge special hardware */
111      UINT8 *cartSRAM;   /* on-cartridge SRAM */
112      UINT8 sram_save;   /* should be the contents of the on-cartridge SRAM be saved */
113      UINT8 *cartRAM;    /* additional on-cartridge RAM (64KB for Ernie Els Golf) */
114      UINT32 ram_size;   /* size of the on-cartridge RAM */
115      UINT8 ram_page;    /* currently swapped in cartridge RAM */
116   } m_cartridge[MAX_CARTRIDGES];
117};
118
119
120/*----------- defined in machine/sms.c -----------*/
121
122/* Function prototypes */
123WRITE8_HANDLER( sms_cartram_w );
124WRITE8_HANDLER( sms_cartram2_w );
125WRITE8_HANDLER( sms_fm_detect_w );
126READ8_HANDLER( sms_fm_detect_r );
127READ8_HANDLER( sms_input_port_0_r );
128READ8_HANDLER( sms_input_port_1_r );
129WRITE8_HANDLER( sms_ym2413_register_port_0_w );
130WRITE8_HANDLER( sms_ym2413_data_port_0_w );
131WRITE8_HANDLER( sms_io_control_w );
132READ8_HANDLER( sms_count_r );
133WRITE8_HANDLER( sms_sscope_w );
134READ8_HANDLER( sms_sscope_r );
135WRITE8_HANDLER( sms_mapper_w );
136READ8_HANDLER( sms_mapper_r );
137WRITE8_HANDLER( sms_bios_w );
138WRITE8_HANDLER( gg_sio_w );
139READ8_HANDLER( gg_sio_r );
140READ8_HANDLER( gg_input_port_2_r );
141
142INPUT_CHANGED( lgun1_changed );
143INPUT_CHANGED( lgun2_changed );
144
145void sms_pause_callback( running_machine &machine );
146void sms_store_int_callback( running_machine &machine, int state );
147
148DEVICE_START( sms_cart );
149DEVICE_IMAGE_LOAD( sms_cart );
150
151MACHINE_START( sms );
152MACHINE_RESET( sms );
153
154READ8_HANDLER( sms_store_cart_select_r );
155WRITE8_HANDLER( sms_store_cart_select_w );
156READ8_HANDLER( sms_store_select1 );
157READ8_HANDLER( sms_store_select2 );
158READ8_HANDLER( sms_store_control_r );
159WRITE8_HANDLER( sms_store_control_w );
160
161#define IO_EXPANSION    (0x80)   /* Expansion slot enable (1= disabled, 0= enabled) */
162#define IO_CARTRIDGE    (0x40)   /* Cartridge slot enable (1= disabled, 0= enabled) */
163#define IO_CARD         (0x20)   /* Card slot disabled (1= disabled, 0= enabled) */
164#define IO_WORK_RAM     (0x10)   /* Work RAM disabled (1= disabled, 0= enabled) */
165#define IO_BIOS_ROM     (0x08)   /* BIOS ROM disabled (1= disabled, 0= enabled) */
166#define IO_CHIP         (0x04)   /* I/O chip disabled (1= disabled, 0= enabled) */
167
168
169DRIVER_INIT( sg1000m3 );
170DRIVER_INIT( sms1 );
171DRIVER_INIT( smsj );
172DRIVER_INIT( sms2kr );
173DRIVER_INIT( smssdisp );
174DRIVER_INIT( gamegear );
175DRIVER_INIT( gamegeaj );
176
177VIDEO_START( sms1 );
178VIDEO_START( gamegear );
179SCREEN_UPDATE( sms1 );
180SCREEN_UPDATE( sms );
181SCREEN_UPDATE( gamegear );
182
183#endif /* SMS_H_ */
src/mame/video/smsvdp.c
r11572r11573
1/*********************************************************************
2
3    smsvdp.c
4
5    Implementation of video hardware chip used by Sega Master System
6
7**********************************************************************/
8
9/*
10    For more information, please see:
11    - http://cgfm2.emuviews.com/txt/msvdp.txt
12    - http://www.smspower.org/forums/viewtopic.php?p=44198
13
14A scanline contains the following sections:
15  - horizontal sync     1  ED      => HSYNC high/increment line counter/generate interrupts/etc
16  - left blanking       2  ED-EE
17  - color burst        14  EE-EF
18  - left blanking       8  F5-F9
19  - left border        13  F9-FF
20  - active display    256  00-7F
21  - right border       15  80-87
22  - right blanking      8  87-8B
23  - horizontal sync    25  8B-97   => HSYNC low
24
25
26NTSC frame timing
27                       256x192         256x224        256x240 (doesn't work on real hardware)
28  - vertical blanking   3  D5-D7        3  E5-E7       3  EE-F0
29  - top blanking       13  D8-E4       13  E8-F4      13  F1-FD
30  - top border         27  E5-FF       11  F5-FF       3  FD-FF
31  - active display    192  00-BF      224  00-DF     240  00-EF
32  - bottom border      24  C0-D7        8  E0-E7       0  F0-F0
33  - bottom blanking     3  D8-DA        3  E8-EA       3  F0-F2
34
35
36PAL frame timing
37                       256x192         256x224        256x240
38  - vertical blanking   3  BA-BC        3  CA-CC       3  D2-D4
39  - top blanking       13  BD-C9       13  CD-D9      13  D5-E1
40  - top border         54  CA-FF       38  DA-FF      30  E2-FF
41  - active display    192  00-BF      224  00-DF     240  00-EF
42  - bottom border      48  C0-EF       32  E0-FF      24  F0-07
43  - bottom blanking     3  F0-F2        3  00-02       3  08-0A
44
45  TODO:
46    - implement differences between SMS1_VDP & SMS_VDP/GG_VDP
47
48*/
49
50#include "emu.h"
51#include "video/smsvdp.h"
52
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)
56
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 */
61
62#define VINT_HPOS             23
63#define HINT_HPOS             23
64#define VCOUNT_CHANGE_HPOS    22
65#define VINT_FLAG_HPOS        7
66#define SPROVR_HPOS           6
67#define SPRCOL_BASEHPOS       42
68#define DISPLAY_CB_HPOS       5  /* fix X-Scroll latchtime (Flubba's VDPTest) */
69
70#define GG_CRAM_SIZE          0x40   /* 32 colors x 2 bytes per color = 64 bytes */
71#define SMS_CRAM_SIZE         0x20   /* 32 colors x 1 bytes per color = 32 bytes */
72#define MAX_CRAM_SIZE         0x40
73
74#define VRAM_SIZE             0x4000
75
76#define PRIORITY_BIT          0x1000
77#define BACKDROP_COLOR        ((smsvdp->vdp_mode == 4 ? 0x10 : 0x00) + (smsvdp->reg[0x07] & 0x0f))
78
79#define NUM_OF_REGISTER       0x10  /* 16 registers */
80
81#define INIT_VCOUNT           0
82#define VERTICAL_BLANKING     1
83#define TOP_BLANKING          2
84#define TOP_BORDER            3
85#define ACTIVE_DISPLAY_V      4
86#define BOTTOM_BORDER         5
87#define BOTTOM_BLANKING       6
88
89static const UINT8 sms_ntsc_192[7] = { 0xd5, 3, 13, 27, 192, 24, 3 };
90static const UINT8 sms_ntsc_224[7] = { 0xe5, 3, 13, 11, 224,  8, 3 };
91static const UINT8 sms_ntsc_240[7] = { 0xee, 3, 13,  3, 240,  0, 3 };
92static const UINT8 sms_pal_192[7]  = { 0xba, 3, 13, 54, 192, 48, 3 };
93static const UINT8 sms_pal_224[7]  = { 0xca, 3, 13, 38, 224, 32, 3 };
94static const UINT8 sms_pal_240[7]  = { 0xd2, 3, 13, 30, 240, 24, 3 };
95
96
97typedef struct _smsvdp_t smsvdp_t;
98struct _smsvdp_t
99{
100   UINT32           features;
101   UINT8            reg[NUM_OF_REGISTER];     /* All the registers */
102   UINT8            status;                   /* Status register */
103   UINT8            reg9copy;                 /* Internal copy of register 9 */
104   UINT8            addrmode;                 /* Type of VDP action */
105   UINT16           addr;                     /* Contents of internal VDP address register */
106   UINT8            cram_mask;                /* Mask to switch between SMS and GG CRAM sizes */
107   int              cram_dirty;               /* Have there been any changes to the CRAM area */
108   int              pending;
109   UINT8            buffer;
110   int              gg_sms_mode;              /* Shrunk SMS screen on GG lcd mode flag */
111   int              irq_state;                /* The status of the IRQ line of the VDP */
112   int              vdp_mode;                 /* Current mode of the VDP: 0,1,2,3,4 */
113   int              y_pixels;                 /* 192, 224, 240 */
114   UINT8            line_counter;
115   UINT8            hcounter;
116   memory_region            *VRAM;                    /* Pointer to VRAM */
117   memory_region            *CRAM;                    /* Pointer to CRAM */
118   const UINT8      *sms_frame_timing;
119   bitmap_t         *tmpbitmap;
120   UINT8            *collision_buffer;
121
122   /* line_buffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
123       Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
124       sms compatibility mode. */
125   int              *line_buffer;
126   int              current_palette[32];
127   smsvdp_int_cb    int_callback;
128   smsvdp_pause_cb  pause_callback;
129   emu_timer        *smsvdp_display_timer;
130};
131
132static TIMER_CALLBACK( smsvdp_display_callback );
133static void sms_refresh_line( running_machine &machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int offsetx, int offsety, int line );
134static void sms_update_palette( smsvdp_t *smsvdp );
135
136
137/*****************************************************************************
138    INLINE FUNCTIONS
139*****************************************************************************/
140
141INLINE smsvdp_t *get_safe_token(device_t *device)
142{
143   assert(device != NULL);
144   assert(device->type() == SMSVDP);
145
146   return (smsvdp_t *)downcast<legacy_device_base *>(device)->token();
147}
148
149INLINE const smsvdp_interface *get_interface(device_t *device)
150{
151   assert(device != NULL);
152   assert((device->type() == SMSVDP));
153   return (const smsvdp_interface *) device->static_config();
154}
155
156/*************************************
157 *
158 *  Utilities
159 *
160 *************************************/
161
162static void set_display_settings( device_t *device )
163{
164   smsvdp_t *smsvdp = get_safe_token(device);
165
166   screen_device *screen = device->machine().first_screen();
167   int height = screen->height();
168   int M1, M2, M3, M4;
169   M1 = smsvdp->reg[0x01] & 0x10;
170   M2 = smsvdp->reg[0x00] & 0x02;
171   M3 = smsvdp->reg[0x01] & 0x08;
172   M4 = smsvdp->reg[0x00] & 0x04;
173   smsvdp->y_pixels = 192;
174   if (M4)
175   {
176      /* mode 4 */
177      smsvdp->vdp_mode = 4;
178      if (M2 && (IS_SMS2_VDP || IS_GAMEGEAR_VDP))
179      {
180         if (M1 && !M3)
181            smsvdp->y_pixels = 224;   /* 224-line display */
182         else if (!M1 && M3)
183            smsvdp->y_pixels = 240;   /* 240-line display */
184      }
185   }
186   else
187   {
188      /* original TMS9918 mode */
189      if (!M1 && !M2 && !M3)
190      {
191         smsvdp->vdp_mode = 0;
192      }
193      else
194//      if (M1 && !M2 && !M3)
195//      {
196//          smsvdp->vdp_mode = 1;
197//      }
198//      else
199      if (!M1 && M2 && !M3)
200      {
201         smsvdp->vdp_mode = 2;
202//      }
203//      else
204//      if (!M1 && !M2 && M3)
205//      {
206//          smsvdp->vdp_mode = 3;
207      }
208      else
209      {
210         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');
211      }
212   }
213
214   switch (smsvdp->y_pixels)
215   {
216   case 192:
217      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_192 : sms_ntsc_192;
218      break;
219
220   case 224:
221      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_224 : sms_ntsc_224;
222      break;
223
224   case 240:
225      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_240 : sms_ntsc_240;
226      break;
227   }
228   smsvdp->cram_dirty = 1;
229}
230
231
232READ8_DEVICE_HANDLER( sms_vdp_vcount_r )
233{
234   smsvdp_t *smsvdp = get_safe_token(device);
235   int vpos = device->machine().primary_screen->vpos();
236
237   if (device->machine().primary_screen->hpos() < VCOUNT_CHANGE_HPOS)
238   {
239      vpos--;
240      if (vpos < 0)
241         vpos += device->machine().primary_screen->height();
242   }
243
244   return (smsvdp->sms_frame_timing[INIT_VCOUNT] + vpos) & 0xff;
245}
246
247
248READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r )
249{
250   smsvdp_t *smsvdp = get_safe_token(device);
251
252   return smsvdp->hcounter;
253}
254
255WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w )
256{
257   smsvdp_t *smsvdp = get_safe_token(device);
258
259   smsvdp->hcounter = data;
260}
261
262
263void sms_vdp_set_ggsmsmode( device_t *device, int mode )
264{
265   smsvdp_t *smsvdp = get_safe_token(device);
266
267   smsvdp->gg_sms_mode = mode;
268   smsvdp->cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
269}
270
271
272static TIMER_CALLBACK( smsvdp_set_status )
273{
274   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
275
276   smsvdp->status |= (UINT8) param;
277}
278
279
280static TIMER_CALLBACK( smsvdp_check_hint )
281{
282   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
283
284   if (smsvdp->line_counter == 0x00)
285   {
286      smsvdp->line_counter = smsvdp->reg[0x0a];
287      smsvdp->status |= STATUS_HINT;
288   }
289   else
290   {
291      smsvdp->line_counter--;
292   }
293
294   if ((smsvdp->status & STATUS_HINT) && (smsvdp->reg[0x00] & 0x10))
295   {
296      smsvdp->irq_state = 1;
297
298      if (smsvdp->int_callback)
299         smsvdp->int_callback(machine, ASSERT_LINE);
300   }
301}
302
303
304static TIMER_CALLBACK( smsvdp_check_vint )
305{
306   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
307
308   if ((smsvdp->status & STATUS_VINT) && (smsvdp->reg[0x01] & 0x20))
309   {
310      smsvdp->irq_state = 1;
311
312      if (smsvdp->int_callback)
313         smsvdp->int_callback(machine, ASSERT_LINE);
314   }
315}
316
317
318static TIMER_CALLBACK( smsvdp_display_callback )
319{
320   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
321
322   rectangle rec;
323   int vpos = machine.primary_screen->vpos();
324   int vpos_limit = smsvdp->sms_frame_timing[VERTICAL_BLANKING] + smsvdp->sms_frame_timing[TOP_BLANKING]
325                  + smsvdp->sms_frame_timing[TOP_BORDER] + smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V]
326                  + smsvdp->sms_frame_timing[BOTTOM_BORDER] + smsvdp->sms_frame_timing[BOTTOM_BLANKING];
327
328   rec.min_y = rec.max_y = vpos;
329
330   /* Check if we're on the last line of a frame */
331   if (vpos == vpos_limit - 1)
332   {
333      smsvdp->line_counter = smsvdp->reg[0x0a];
334      if (smsvdp->pause_callback)
335         smsvdp->pause_callback(machine);
336
337      return;
338   }
339
340   vpos_limit -= smsvdp->sms_frame_timing[BOTTOM_BLANKING];
341
342   /* Check if we're below the bottom border */
343   if (vpos >= vpos_limit)
344   {
345      smsvdp->line_counter = smsvdp->reg[0x0a];
346      return;
347   }
348
349   vpos_limit -= smsvdp->sms_frame_timing[BOTTOM_BORDER];
350
351   /* Check if we're in the bottom border area */
352   if (vpos >= vpos_limit)
353   {
354      if (vpos == vpos_limit)
355      {
356         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, HINT_HPOS), FUNC(smsvdp_check_hint),0 ,smsvdp);
357      }
358      else
359      {
360         smsvdp->line_counter = smsvdp->reg[0x0a];
361      }
362
363      if (vpos == vpos_limit + 1)
364      {
365         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, VINT_FLAG_HPOS), FUNC(smsvdp_set_status), (int)STATUS_VINT, smsvdp);
366         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, VINT_HPOS), FUNC(smsvdp_check_vint), 0, smsvdp);
367      }
368
369      sms_update_palette(smsvdp);
370
371      /* Draw left border */
372      rec.min_x = LBORDER_START;
373      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
374      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
375
376      /* Draw right border */
377      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
378      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
379      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
380
381      /* Draw middle of the border */
382      /* We need to do this through the regular drawing function so it will */
383      /* be included in the gamegear scaling functions */
384      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]));
385      return;
386   }
387
388   vpos_limit -= smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V];
389
390   /* Check if we're in the active display area */
391   if (vpos >= vpos_limit)
392   {
393      if (vpos == vpos_limit)
394      {
395         smsvdp->reg9copy = smsvdp->reg[0x09];
396      }
397
398      machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, HINT_HPOS), FUNC(smsvdp_check_hint),0, smsvdp);
399
400      sms_update_palette(smsvdp);
401
402      /* Draw left border */
403      rec.min_x = LBORDER_START;
404      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
405      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
406
407      /* Draw right border */
408      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
409      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
410      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
411
412      sms_refresh_line(machine, smsvdp, smsvdp->tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit);
413
414      return;
415   }
416
417   vpos_limit -= smsvdp->sms_frame_timing[TOP_BORDER];
418
419   /* Check if we're in the top border area */
420   if (vpos >= vpos_limit)
421   {
422      smsvdp->line_counter = smsvdp->reg[0x0a];
423      sms_update_palette(smsvdp);
424
425      /* Draw left border */
426      rec.min_x = LBORDER_START;
427      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
428      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
429
430      /* Draw right border */
431      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
432      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
433      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
434
435      /* Draw middle of the border */
436      /* We need to do this through the regular drawing function so it will */
437      /* be included in the gamegear scaling functions */
438      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]));
439      return;
440   }
441
442   /* we're in the vertical or top blanking area */
443   smsvdp->line_counter = smsvdp->reg[0x0a];
444}
445
446
447READ8_DEVICE_HANDLER( sms_vdp_data_r )
448{
449   smsvdp_t *smsvdp = get_safe_token(device);
450   UINT8 temp;
451
452   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
453   /* to the address register when in the middle of doing a command.               */
454   /* Cosmic Spacehead needs this, among others                                    */
455   /* Clear pending write flag */
456   smsvdp->pending = 0;
457
458   /* Return read buffer contents */
459   temp = smsvdp->buffer;
460
461   /* Load read buffer */
462   smsvdp->buffer = smsvdp->VRAM->u8((smsvdp->addr & 0x3fff));
463
464   /* Bump internal address register */
465   smsvdp->addr += 1;
466   return temp;
467}
468
469
470READ8_DEVICE_HANDLER( sms_vdp_ctrl_r )
471{
472   smsvdp_t *smsvdp = get_safe_token(device);
473
474   UINT8 temp = smsvdp->status;
475
476   /* Clear pending write flag */
477   smsvdp->pending = 0;
478
479   smsvdp->status &= ~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT);
480
481   if (smsvdp->irq_state == 1)
482   {
483      smsvdp->irq_state = 0;
484
485      if (smsvdp->int_callback)
486         smsvdp->int_callback(device->machine(), CLEAR_LINE);
487   }
488
489   /* low 5 bits return non-zero data (it fixes PGA Tour Golf course map introduction) */
490   return temp | 0x1f;
491}
492
493
494WRITE8_DEVICE_HANDLER( sms_vdp_data_w )
495{
496   smsvdp_t *smsvdp = get_safe_token(device);
497   int address;
498
499   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
500   /* to the address register when in the middle of doing a command.               */
501   /* Cosmic Spacehead needs this, among others                                    */
502   /* Clear pending write flag */
503   smsvdp->pending = 0;
504
505   switch(smsvdp->addrmode)
506   {
507      case 0x00:
508      case 0x01:
509      case 0x02:
510         address = (smsvdp->addr & 0x3fff);
511         smsvdp->VRAM->u8(address) = data;
512         break;
513
514      case 0x03:
515         address = smsvdp->addr & smsvdp->cram_mask;
516         if (data != smsvdp->CRAM->u8(address))
517         {
518            smsvdp->CRAM->u8(address) = data;
519            smsvdp->cram_dirty = 1;
520         }
521         break;
522   }
523
524   smsvdp->buffer = data;
525   smsvdp->addr += 1;
526}
527
528
529WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w )
530{
531   smsvdp_t *smsvdp = get_safe_token(device);
532
533   int reg_num;
534
535   if (smsvdp->pending == 0)
536   {
537      smsvdp->addr = (smsvdp->addr & 0xff00) | data;
538      smsvdp->pending = 1;
539   }
540   else
541   {
542      /* Clear pending write flag */
543      smsvdp->pending = 0;
544
545      smsvdp->addrmode = (data >> 6) & 0x03;
546      smsvdp->addr = (data << 8) | (smsvdp->addr & 0xff);
547      switch (smsvdp->addrmode)
548      {
549      case 0:      /* VRAM reading mode */
550         smsvdp->buffer = smsvdp->VRAM->u8(smsvdp->addr & 0x3fff);
551         smsvdp->addr += 1;
552         break;
553
554      case 1:      /* VRAM writing mode */
555         break;
556
557      case 2:      /* VDP register write */
558         reg_num = data & 0x0f;
559         smsvdp->reg[reg_num] = smsvdp->addr & 0xff;
560         if (reg_num == 0 && smsvdp->addr & 0x02)
561            logerror("overscan enabled.\n");
562
563         if (reg_num == 0 || reg_num == 1)
564            set_display_settings(device);
565
566         if (reg_num == 1)
567         {
568            device->machine().scheduler().timer_set(device->machine().primary_screen->time_until_pos(device->machine().primary_screen->vpos(), VINT_HPOS), FUNC(smsvdp_check_vint),0 ,smsvdp);
569         }
570         smsvdp->addrmode = 0;
571         break;
572
573      case 3:      /* CRAM writing mode */
574         break;
575      }
576   }
577}
578
579
580static void sms_refresh_line_mode4( smsvdp_t *smsvdp, int *line_buffer, int *priority_selected, int line )
581{
582   int tile_column;
583   int x_scroll, y_scroll, x_scroll_start_column;
584   int pixel_x, pixel_plot_x;
585   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
586   int scroll_mod;
587   UINT16 name_table_address;
588   UINT8 *name_table;
589
590   if (smsvdp->y_pixels != 192)
591   {
592      name_table_address = ((smsvdp->reg[0x02] & 0x0c) << 10) | 0x0700;
593      scroll_mod = 256;
594   }
595   else
596   {
597      name_table_address = (smsvdp->reg[0x02] << 10) & 0x3800;
598      scroll_mod = 224;
599   }
600
601   if (IS_SMS1_VDP)
602      name_table_address = name_table_address & (((smsvdp->reg[0x02] & 0x01) << 10) | 0x3bff);
603
604   /* if top 2 rows of screen not affected by horizontal scrolling, then x_scroll = 0 */
605   /* else x_scroll = reg[0x08]                                                       */
606   x_scroll = (((smsvdp->reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - smsvdp->reg[0x08]);
607
608   x_scroll_start_column = (x_scroll >> 3);          /* x starting column tile */
609
610   /* Draw background layer */
611   for (tile_column = 0; tile_column < 33; tile_column++)
612   {
613      UINT16 tile_data;
614      int tile_selected, palette_selected, horiz_selected, vert_selected, priority_select;
615      int tile_line;
616
617      /* Rightmost 8 columns for SMS (or 2 columns for GG) not affected by */
618      /* vertical scrolling when bit 7 of reg[0x00] is set */
619      y_scroll = ((smsvdp->reg[0x00] & 0x80) && (tile_column > 23)) ? 0 : smsvdp->reg9copy;
620
621      name_table = smsvdp->VRAM->base() + name_table_address + ((((line + y_scroll) % scroll_mod) >> 3) << 6);
622
623      tile_line = ((tile_column + x_scroll_start_column) & 0x1f) * 2;
624      tile_data = name_table[tile_line] | (name_table[tile_line + 1] << 8);
625
626      tile_selected = (tile_data & 0x01ff);
627      priority_select = tile_data & PRIORITY_BIT;
628      palette_selected = (tile_data >> 11) & 0x01;
629      vert_selected = (tile_data >> 10) & 0x01;
630      horiz_selected = (tile_data >> 9) & 0x01;
631
632      tile_line = line - ((0x07 - (y_scroll & 0x07)) + 1);
633      if (vert_selected)
634         tile_line = 0x07 - tile_line;
635
636      bit_plane_0 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x00);
637      bit_plane_1 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x01);
638      bit_plane_2 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x02);
639      bit_plane_3 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x03);
640
641      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
642      {
643         UINT8 pen_bit_0, pen_bit_1, pen_bit_2, pen_bit_3;
644         UINT8 pen_selected;
645
646         pen_bit_0 = (bit_plane_0 >> (7 - pixel_x)) & 0x01;
647         pen_bit_1 = (bit_plane_1 >> (7 - pixel_x)) & 0x01;
648         pen_bit_2 = (bit_plane_2 >> (7 - pixel_x)) & 0x01;
649         pen_bit_3 = (bit_plane_3 >> (7 - pixel_x)) & 0x01;
650
651         pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0);
652         if (palette_selected)
653            pen_selected |= 0x10;
654
655
656         if (!horiz_selected)
657         {
658            pixel_plot_x = pixel_x;
659         }
660         else
661         {
662            pixel_plot_x = 7 - pixel_x;
663         }
664         pixel_plot_x = (0 - (x_scroll & 0x07) + (tile_column << 3) + pixel_plot_x);
665         if (pixel_plot_x >= 0 && pixel_plot_x < 256)
666         {
667//              logerror("%x %x\n", pixel_plot_x + pixel_offset_x, pixel_plot_y);
668            line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
669            priority_selected[pixel_plot_x] = priority_select | (pen_selected & 0x0f);
670         }
671      }
672   }
673}
674
675static void sms_refresh_mode4_sprites( running_machine &machine, smsvdp_t *smsvdp, int *line_buffer, int *priority_selected, int pixel_plot_y, int line )
676{
677   int sprite_index;
678   int pixel_x, pixel_plot_x;
679   int sprite_x, sprite_y, sprite_line, sprite_tile_selected, sprite_height, sprite_zoom;
680   int sprite_col_occurred, sprite_col_x;
681   int sprite_buffer[8], sprite_buffer_count, sprite_buffer_index;
682   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
683   UINT8 *sprite_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x05] << 7) & 0x3f00);
684
685   /* Draw sprite layer */
686   sprite_height = (smsvdp->reg[0x01] & 0x02 ? 16 : 8);
687   sprite_zoom = 1;
688
689   if (smsvdp->reg[0x01] & 0x01)      /* sprite doubling */
690      sprite_zoom = 2;
691
692   sprite_buffer_count = 0;
693   for (sprite_index = 0; (sprite_index < 64) && (sprite_table[sprite_index] != 0xd0 || smsvdp->y_pixels != 192) && (sprite_buffer_count < 9); sprite_index++)
694   {
695      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
696
697      if (sprite_y > 240)
698         sprite_y -= 256; /* wrap from top if y position is > 240 */
699
700      if ((line >= sprite_y) && (line < (sprite_y + sprite_height * sprite_zoom)))
701      {
702         if (sprite_buffer_count < 8)
703         {
704            sprite_buffer[sprite_buffer_count] = sprite_index;
705         }
706         else if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
707         {
708            /* Too many sprites per line */
709            machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPROVR_HPOS), FUNC(smsvdp_set_status), (int)STATUS_SPROVR, smsvdp);
710         }
711         sprite_buffer_count++;
712      }
713   }
714
715   /* Check if display is disabled */
716   if (!(smsvdp->reg[0x01] & 0x40))
717      return;
718
719   if (sprite_buffer_count > 8)
720      sprite_buffer_count = 8;
721
722   memset(smsvdp->collision_buffer, 0, SMS_X_PIXELS);
723   sprite_buffer_count--;
724
725   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
726   {
727      sprite_index = sprite_buffer[sprite_buffer_index];
728      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
729
730      if (sprite_y > 240)
731         sprite_y -= 256; /* wrap from top if y position is > 240 */
732
733      sprite_x = sprite_table[0x80 + (sprite_index << 1)];
734
735      if (smsvdp->reg[0x00] & 0x08)
736         sprite_x -= 0x08;    /* sprite shift */
737
738      sprite_tile_selected = sprite_table[0x81 + (sprite_index << 1)];
739
740      if (smsvdp->reg[0x06] & 0x04)
741         sprite_tile_selected += 256; /* pattern table select */
742
743      if (smsvdp->reg[0x01] & 0x02)
744         sprite_tile_selected &= 0x01fe; /* force even index */
745
746      sprite_line = (line - sprite_y) / sprite_zoom;
747
748      if (sprite_line > 0x07)
749         sprite_tile_selected += 1;
750
751      bit_plane_0 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x00);
752      bit_plane_1 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x01);
753      bit_plane_2 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x02);
754      bit_plane_3 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x03);
755
756      sprite_col_occurred = 0;
757      sprite_col_x = 0;
758
759      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
760      {
761         UINT8 pen_bit_0, pen_bit_1, pen_bit_2, pen_bit_3;
762         int pen_selected;
763
764         pen_bit_0 = (bit_plane_0 >> (7 - pixel_x)) & 0x01;
765         pen_bit_1 = (bit_plane_1 >> (7 - pixel_x)) & 0x01;
766         pen_bit_2 = (bit_plane_2 >> (7 - pixel_x)) & 0x01;
767         pen_bit_3 = (bit_plane_3 >> (7 - pixel_x)) & 0x01;
768
769         pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0) | 0x10;
770
771         if (pen_selected == 0x10)      /* Transparent palette so skip draw */
772         {
773            continue;
774         }
775
776         if (smsvdp->reg[0x01] & 0x01)
777         {
778            /* sprite doubling is enabled */
779            pixel_plot_x = sprite_x + (pixel_x << 1);
780
781            /* check to prevent going outside of active display area */
782            if (pixel_plot_x < 0 || pixel_plot_x > 255)
783            {
784               continue;
785            }
786
787            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
788            {
789               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
790               line_buffer[pixel_plot_x + 1] = smsvdp->current_palette[pen_selected];
791            }
792            else
793            {
794               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
795               {
796                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
797               }
798               if (priority_selected[pixel_plot_x + 1] == PRIORITY_BIT)
799               {
800                  line_buffer[pixel_plot_x + 1] = smsvdp->current_palette[pen_selected];
801               }
802            }
803            if (smsvdp->collision_buffer[pixel_plot_x] != 1)
804            {
805               smsvdp->collision_buffer[pixel_plot_x] = 1;
806            }
807            else
808            {
809               if (!sprite_col_occurred)
810               {
811                  sprite_col_occurred = 1;
812                  sprite_col_x = pixel_plot_x;
813               }
814            }
815            if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
816            {
817               smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
818            }
819            else
820            {
821               if (!sprite_col_occurred)
822               {
823                  sprite_col_occurred = 1;
824                  sprite_col_x = pixel_plot_x;
825               }
826            }
827         }
828         else
829         {
830            pixel_plot_x = sprite_x + pixel_x;
831
832            /* check to prevent going outside of active display area */
833            if (pixel_plot_x < 0 || pixel_plot_x > 255)
834            {
835               continue;
836            }
837
838            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
839            {
840               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
841            }
842            else
843            {
844               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
845               {
846                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
847               }
848            }
849            if (smsvdp->collision_buffer[pixel_plot_x] != 1)
850            {
851               smsvdp->collision_buffer[pixel_plot_x] = 1;
852            }
853            else
854            {
855               if (!sprite_col_occurred)
856               {
857                  sprite_col_occurred = 1;
858                  sprite_col_x = pixel_plot_x;
859               }
860            }
861         }
862      }
863      if (sprite_col_occurred)
864         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x), FUNC(smsvdp_set_status), (int)STATUS_SPRCOL, smsvdp);
865   }
866
867   /* Fill column 0 with overscan color from reg[0x07] */
868   if (smsvdp->reg[0x00] & 0x20)
869   {
870      line_buffer[0] = smsvdp->current_palette[BACKDROP_COLOR];
871      line_buffer[1] = smsvdp->current_palette[BACKDROP_COLOR];
872      line_buffer[2] = smsvdp->current_palette[BACKDROP_COLOR];
873      line_buffer[3] = smsvdp->current_palette[BACKDROP_COLOR];
874      line_buffer[4] = smsvdp->current_palette[BACKDROP_COLOR];
875      line_buffer[5] = smsvdp->current_palette[BACKDROP_COLOR];
876      line_buffer[6] = smsvdp->current_palette[BACKDROP_COLOR];
877      line_buffer[7] = smsvdp->current_palette[BACKDROP_COLOR];
878   }
879}
880
881
882static void sms_refresh_tms9918_sprites( running_machine &machine, smsvdp_t *smsvdp, int *line_buffer, int pixel_plot_y, int line )
883{
884   int pixel_plot_x;
885   int sprite_col_occurred, sprite_col_x;
886   int sprite_height, sprite_buffer_count, sprite_index, sprite_buffer[5], sprite_buffer_index;
887   UINT8 *sprite_table, *sprite_pattern_table;
888
889   /* Draw sprite layer */
890   sprite_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x05] & 0x7f) << 7);
891   sprite_pattern_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x06] & 0x07) << 11);
892   sprite_height = 8;
893
894   if (smsvdp->reg[0x01] & 0x02)                         /* Check if SI is set */
895      sprite_height = sprite_height * 2;
896   if (smsvdp->reg[0x01] & 0x01)                         /* Check if MAG is set */
897      sprite_height = sprite_height * 2;
898
899   sprite_buffer_count = 0;
900   for (sprite_index = 0; (sprite_index < 32 * 4) && (sprite_table[sprite_index] != 0xd0) && (sprite_buffer_count < 5); sprite_index += 4)
901   {
902      int sprite_y = sprite_table[sprite_index] + 1;
903
904      if (sprite_y > 240)
905         sprite_y -= 256;
906
907      if ((line >= sprite_y) && (line < (sprite_y + sprite_height)))
908      {
909         if (sprite_buffer_count < 5)
910         {
911            sprite_buffer[sprite_buffer_count] = sprite_index;
912         }
913         else if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
914         {
915            /* Too many sprites per line */
916            machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPROVR_HPOS), FUNC(smsvdp_set_status), (int)STATUS_SPROVR, smsvdp);
917         }
918         sprite_buffer_count++;
919      }
920   }
921
922   /* Check if display is disabled */
923   if (!(smsvdp->reg[0x01] & 0x40))
924      return;
925
926   if (sprite_buffer_count > 4)
927      sprite_buffer_count = 4;
928
929   memset(smsvdp->collision_buffer, 0, SMS_X_PIXELS);
930   sprite_buffer_count--;
931
932   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
933   {
934      int pen_selected;
935      int sprite_line, pixel_x, sprite_x, sprite_tile_selected;
936      int sprite_y;
937      UINT8 pattern;
938
939      sprite_index = sprite_buffer[sprite_buffer_index];
940      sprite_y = sprite_table[sprite_index] + 1;
941
942      if (sprite_y > 240)
943         sprite_y -= 256;
944
945      sprite_x = sprite_table[sprite_index + 1];
946      pen_selected = sprite_table[sprite_index + 3] & 0x0f;
947
948      if (IS_GAMEGEAR_VDP)
949         pen_selected |= 0x10;
950
951      if (sprite_table[sprite_index + 3] & 0x80)
952         sprite_x -= 32;
953
954      sprite_tile_selected = sprite_table[sprite_index + 2];
955      sprite_line = line - sprite_y;
956
957      if (smsvdp->reg[0x01] & 0x01)
958         sprite_line >>= 1;
959
960      if (smsvdp->reg[0x01] & 0x02)
961      {
962         sprite_tile_selected &= 0xfc;
963
964         if (sprite_line > 0x07)
965         {
966            sprite_tile_selected += 1;
967            sprite_line -= 8;
968         }
969      }
970
971      pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
972
973      sprite_col_occurred = 0;
974      sprite_col_x = 0;
975
976      for (pixel_x = 0; pixel_x < 8; pixel_x++)
977      {
978         if (smsvdp->reg[0x01] & 0x01)
979         {
980            pixel_plot_x = sprite_x + pixel_x * 2;
981            if (pixel_plot_x < 0 || pixel_plot_x > 255)
982            {
983               continue;
984            }
985
986            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
987            {
988               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
989
990               if (smsvdp->collision_buffer[pixel_plot_x] != 1)
991               {
992                  smsvdp->collision_buffer[pixel_plot_x] = 1;
993               }
994               else
995               {
996                  if (!sprite_col_occurred)
997                  {
998                     sprite_col_occurred = 1;
999                     sprite_col_x = pixel_plot_x;
1000                  }
1001               }
1002
1003               line_buffer[pixel_plot_x+1] = smsvdp->current_palette[pen_selected];
1004
1005               if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
1006               {
1007                  smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
1008               }
1009               else
1010               {
1011                  if (!sprite_col_occurred)
1012                  {
1013                     sprite_col_occurred = 1;
1014                     sprite_col_x = pixel_plot_x;
1015                  }
1016               }
1017            }
1018         }
1019         else
1020         {
1021            pixel_plot_x = sprite_x + pixel_x;
1022
1023            if (pixel_plot_x < 0 || pixel_plot_x > 255)
1024            {
1025               continue;
1026            }
1027
1028            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
1029            {
1030               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1031
1032               if (smsvdp->collision_buffer[pixel_plot_x] != 1)
1033               {
1034                  smsvdp->collision_buffer[pixel_plot_x] = 1;
1035               }
1036               else
1037               {
1038                  if (!sprite_col_occurred)
1039                  {
1040                     sprite_col_occurred = 1;
1041                     sprite_col_x = pixel_plot_x;
1042                  }
1043               }
1044            }
1045         }
1046      }
1047
1048      if (smsvdp->reg[0x01] & 0x02)
1049      {
1050         sprite_tile_selected += 2;
1051         pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
1052         sprite_x += (smsvdp->reg[0x01] & 0x01 ? 16 : 8);
1053
1054         for (pixel_x = 0; pixel_x < 8; pixel_x++)
1055         {
1056            if (smsvdp->reg[0x01] & 0x01)
1057            {
1058               pixel_plot_x = sprite_x + pixel_x * 2;
1059
1060               if (pixel_plot_x < 0 || pixel_plot_x > 255)
1061               {
1062                  continue;
1063               }
1064
1065               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
1066               {
1067                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1068
1069                  if (smsvdp->collision_buffer[pixel_plot_x] != 1)
1070                  {
1071                     smsvdp->collision_buffer[pixel_plot_x] = 1;
1072                  }
1073                  else
1074                  {
1075                     if (!sprite_col_occurred)
1076                     {
1077                        sprite_col_occurred = 1;
1078                        sprite_col_x = pixel_plot_x;
1079                     }
1080                  }
1081
1082                  line_buffer[pixel_plot_x+1] = smsvdp->current_palette[pen_selected];
1083
1084                  if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
1085                  {
1086                     smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
1087                  }
1088                  else
1089                  {
1090                     if (!sprite_col_occurred)
1091                     {
1092                        sprite_col_occurred = 1;
1093                        sprite_col_x = pixel_plot_x;
1094                     }
1095                  }
1096               }
1097            }
1098            else
1099            {
1100               pixel_plot_x = sprite_x + pixel_x;
1101
1102               if (pixel_plot_x < 0 || pixel_plot_x > 255)
1103               {
1104                  continue;
1105               }
1106
1107               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
1108               {
1109                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1110
1111                  if (smsvdp->collision_buffer[pixel_plot_x] != 1)
1112                  {
1113                     smsvdp->collision_buffer[pixel_plot_x] = 1;
1114                  }
1115                  else
1116                  {
1117                     if (!sprite_col_occurred)
1118                     {
1119                        sprite_col_occurred = 1;
1120                        sprite_col_x = pixel_plot_x;
1121                     }
1122                  }
1123               }
1124            }
1125         }
1126      }
1127      if (sprite_col_occurred)
1128         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x), FUNC(smsvdp_set_status), (int)STATUS_SPRCOL, smsvdp);
1129   }
1130}
1131
1132
1133static void sms_refresh_line_mode2( smsvdp_t *smsvdp, int *line_buffer, int line )
1134{
1135   int tile_column;
1136   int pixel_x, pixel_plot_x;
1137   UINT8 *name_table, *color_table, *pattern_table;
1138   int pattern_mask, color_mask, pattern_offset;
1139
1140   /* Draw background layer */
1141   name_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1142   color_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x03] & 0x80) << 6);
1143   color_mask = ((smsvdp->reg[0x03] & 0x7f) << 3) | 0x07;
1144   pattern_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x04] & 0x04) << 11);
1145   pattern_mask = ((smsvdp->reg[0x04] & 0x03) << 8) | 0xff;
1146   pattern_offset = (line & 0xc0) << 2;
1147
1148   for (tile_column = 0; tile_column < 32; tile_column++)
1149   {
1150      UINT8 pattern;
1151      UINT8 colors;
1152
1153      pattern = pattern_table[(((pattern_offset + name_table[tile_column]) & pattern_mask) * 8) + (line & 0x07)];
1154      colors = color_table[(((pattern_offset + name_table[tile_column]) & color_mask) * 8) + (line & 0x07)];
1155
1156      for (pixel_x = 0; pixel_x < 8; pixel_x++)
1157      {
1158         UINT8 pen_selected;
1159
1160         if (pattern & (1 << (7 - pixel_x)))
1161         {
1162            pen_selected = colors >> 4;
1163         }
1164         else
1165         {
1166            pen_selected = colors & 0x0f;
1167         }
1168
1169         if (!pen_selected)
1170            pen_selected = BACKDROP_COLOR;
1171
1172         pixel_plot_x = (tile_column << 3) + pixel_x;
1173
1174         if (IS_GAMEGEAR_VDP)
1175            pen_selected |= 0x10;
1176
1177         line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1178      }
1179   }
1180}
1181
1182
1183static void sms_refresh_line_mode0( smsvdp_t *smsvdp, int *line_buffer, int line)
1184{
1185   int tile_column;
1186   int pixel_x, pixel_plot_x;
1187   UINT8 *name_table, *color_table, *pattern_table;
1188
1189   /* Draw background layer */
1190   name_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1191   color_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x03] << 6) & (VRAM_SIZE - 1));
1192   pattern_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x04] << 11) & (VRAM_SIZE - 1));
1193
1194   for (tile_column = 0; tile_column < 32; tile_column++)
1195   {
1196      UINT8 pattern;
1197      UINT8 colors;
1198
1199      pattern = pattern_table[(name_table[tile_column] * 8) + (line & 0x07)];
1200      colors = color_table[name_table[tile_column] >> 3];
1201
1202      for (pixel_x = 0; pixel_x < 8; pixel_x++)
1203      {
1204         int pen_selected;
1205
1206         if (pattern & (1 << (7 - pixel_x)))
1207            pen_selected = colors >> 4;
1208         else
1209            pen_selected = colors & 0x0f;
1210
1211         if (IS_GAMEGEAR_VDP)
1212            pen_selected |= 0x10;
1213
1214         pixel_plot_x = (tile_column << 3) + pixel_x;
1215         line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1216      }
1217   }
1218}
1219
1220
1221static void sms_refresh_line( running_machine &machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int pixel_offset_x, int pixel_plot_y, int line )
1222{
1223   int x;
1224   int *blitline_buffer = smsvdp->line_buffer;
1225   int priority_selected[256];
1226
1227   switch( smsvdp->vdp_mode )
1228   {
1229   case 0:
1230      if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1231         sms_refresh_line_mode0(smsvdp, blitline_buffer, line);
1232      sms_refresh_tms9918_sprites(machine, smsvdp, blitline_buffer, pixel_plot_y, line);
1233      break;
1234
1235   case 2:
1236      if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1237         sms_refresh_line_mode2(smsvdp, blitline_buffer, line);
1238      sms_refresh_tms9918_sprites(machine, smsvdp, blitline_buffer, pixel_plot_y, line);
1239      break;
1240
1241   case 4:
1242   default:
1243      memset(priority_selected, 0, sizeof(priority_selected));
1244      if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1245         sms_refresh_line_mode4(smsvdp, blitline_buffer, priority_selected, line);
1246      sms_refresh_mode4_sprites(machine, smsvdp, blitline_buffer, priority_selected, pixel_plot_y, line);
1247      break;
1248   }
1249
1250   /* Check if display is disabled or we're below/above active area */
1251   if (!(smsvdp->reg[0x01] & 0x40) || line < 0 || line >= smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1252   {
1253      for (x = 0; x < 256; x++)
1254      {
1255         blitline_buffer[x] = smsvdp->current_palette[BACKDROP_COLOR];
1256      }
1257   }
1258
1259   if (IS_GAMEGEAR_VDP && smsvdp->gg_sms_mode)
1260   {
1261      int *combineline_buffer = smsvdp->line_buffer + ((line & 0x03) + 1) * 256;
1262      int plot_x = 48;
1263
1264      /* Do horizontal scaling */
1265      for (x = 8; x < 248;)
1266      {
1267         int combined;
1268
1269         /* Take red and green from first pixel, and blue from second pixel */
1270         combined = (blitline_buffer[x] & 0x00ff) | (blitline_buffer[x + 1] & 0x0f00);
1271         combineline_buffer[plot_x] = combined;
1272
1273         /* Take red from second pixel, and green and blue from third pixel */
1274         combined = (blitline_buffer[x + 1] & 0x000f) | (blitline_buffer[x + 2] & 0x0ff0);
1275         combineline_buffer[plot_x + 1] = combined;
1276         x += 3;
1277         plot_x += 2;
1278      }
1279
1280      /* Do vertical scaling for a screen with 192 or 224 lines
1281           Lines 0-2 and 221-223 have no effect on the output on the GG screen.
1282           We will calculate the gamegear lines as follows:
1283           GG_0 = 1/6 * SMS_3 + 1/3 * SMS_4 + 1/3 * SMS_5 + 1/6 * SMS_6
1284           GG_1 = 1/6 * SMS_4 + 1/3 * SMS_5 + 1/3 * SMS_6 + 1/6 * SMS_7
1285           GG_2 = 1/6 * SMS_6 + 1/3 * SMS_7 + 1/3 * SMS_8 + 1/6 * SMS_9
1286           GG_3 = 1/6 * SMS_7 + 1/3 * SMS_8 + 1/3 * SMS_9 + 1/6 * SMS_10
1287           GG_4 = 1/6 * SMS_9 + 1/3 * SMS_10 + 1/3 * SMS_11 + 1/6 * SMS_12
1288           .....
1289           GG_142 = 1/6 * SMS_216 + 1/3 * SMS_217 + 1/3 * SMS_218 + 1/6 * SMS_219
1290           GG_143 = 1/6 * SMS_217 + 1/3 * SMS_218 + 1/3 * SMS_219 + 1/6 * SMS_220
1291        */
1292      {
1293         int gg_line;
1294         int my_line = pixel_plot_y + line - (TBORDER_START + NTSC_224_TBORDER_Y_PIXELS);
1295         int *line1, *line2, *line3, *line4;
1296
1297         /* First make sure there's enough data to draw anything */
1298         /* We need one more line of data if we're on line 8, 11, 14, 17, etc */
1299         if (my_line < 6 || my_line > 220 || ((my_line - 8) % 3 == 0))
1300         {
1301            return;
1302         }
1303
1304         gg_line = ((my_line - 6) / 3) * 2;
1305
1306         /* If we're on SMS line 7, 10, 13, etc we're on an odd GG line */
1307         if (my_line % 3)
1308         {
1309            gg_line++;
1310         }
1311
1312         /* Calculate the line we will be drawing on */
1313         pixel_plot_y = TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 24 + gg_line;
1314
1315         /* Setup our source lines */
1316         line1 = smsvdp->line_buffer + (((my_line - 3) & 0x03) + 1) * 256;
1317         line2 = smsvdp->line_buffer + (((my_line - 2) & 0x03) + 1) * 256;
1318         line3 = smsvdp->line_buffer + (((my_line - 1) & 0x03) + 1) * 256;
1319         line4 = smsvdp->line_buffer + (((my_line - 0) & 0x03) + 1) * 256;
1320
1321         for (x = 0 + 48; x < 160 + 48; x++)
1322         {
1323            rgb_t   c1 = machine.pens[line1[x]];
1324            rgb_t   c2 = machine.pens[line2[x]];
1325            rgb_t   c3 = machine.pens[line3[x]];
1326            rgb_t   c4 = machine.pens[line4[x]];
1327            *BITMAP_ADDR32(bitmap, pixel_plot_y, pixel_offset_x + x) =
1328               MAKE_RGB((RGB_RED(c1) / 6 + RGB_RED(c2) / 3 + RGB_RED(c3) / 3 + RGB_RED(c4) / 6 ),
1329                  (RGB_GREEN(c1) / 6 + RGB_GREEN(c2) / 3 + RGB_GREEN(c3) / 3 + RGB_GREEN(c4) / 6 ),
1330                  (RGB_BLUE(c1) / 6 + RGB_BLUE(c2) / 3 + RGB_BLUE(c3) / 3 + RGB_BLUE(c4) / 6 ) );
1331         }
1332         return;
1333      }
1334      blitline_buffer = combineline_buffer;
1335   }
1336
1337   for (x = 0; x < 256; x++)
1338   {
1339      *BITMAP_ADDR32(bitmap, pixel_plot_y + line, pixel_offset_x + x) = machine.pens[blitline_buffer[x]];
1340   }
1341}
1342
1343
1344// This is only used by Light Phaser. Should it be moved elsewhere?
1345int sms_vdp_check_brightness(device_t *device, int x, int y)
1346{
1347   /* brightness of the lightgray color in the frame drawn by Light Phaser games */
1348   const UINT8 sensor_min_brightness = 0x7f;
1349
1350   /* TODO: Check how Light Phaser behaves for border areas. For Gangster Town, should */
1351   /* a shot at right border (HC~=0x90) really appear at active scr, near to left border? */
1352   if (x < LBORDER_START + LBORDER_X_PIXELS || x >= LBORDER_START + LBORDER_X_PIXELS + 256)
1353      return 0;
1354
1355   smsvdp_t *smsvdp = get_safe_token(device);
1356   rgb_t color = *BITMAP_ADDR32(smsvdp->tmpbitmap, y, x);
1357
1358   /* reference: http://www.w3.org/TR/AERT#color-contrast */
1359   UINT8 brightness = (RGB_RED(color) * 0.299) + (RGB_GREEN(color) * 0.587) + (RGB_BLUE(color) * 0.114);
1360   //printf ("color brightness: %2X for x %d y %d\n", brightness, x, y);
1361
1362   return (brightness >= sensor_min_brightness) ? 1 : 0;
1363}
1364
1365
1366static void sms_update_palette( smsvdp_t *smsvdp )
1367{
1368   int i;
1369
1370   /* Exit if palette is has no changes */
1371   if (smsvdp->cram_dirty == 0)
1372   {
1373      return;
1374   }
1375   smsvdp->cram_dirty = 0;
1376
1377   if (smsvdp->vdp_mode != 4 && ! IS_GAMEGEAR_VDP)
1378   {
1379      for(i = 0; i < 16; i++)
1380      {
1381         smsvdp->current_palette[i] = 64 + i;
1382      }
1383      return;
1384   }
1385
1386   if (IS_GAMEGEAR_VDP)
1387   {
1388      if (smsvdp->gg_sms_mode)
1389      {
1390         for (i = 0; i < 32; i++)
1391         {
1392            smsvdp->current_palette[i] = ((smsvdp->CRAM->u8(i) & 0x30) << 6) | ((smsvdp->CRAM->u8(i) & 0x0c ) << 4) | ((smsvdp->CRAM->u8(i) & 0x03) << 2);
1393         }
1394      }
1395      else
1396      {
1397         for (i = 0; i < 32; i++)
1398         {
1399            smsvdp->current_palette[i] = ((smsvdp->CRAM->u8(i * 2 + 1) << 8) | smsvdp->CRAM->u8(i * 2)) & 0x0fff;
1400         }
1401      }
1402   }
1403   else
1404   {
1405      for (i = 0; i < 32; i++)
1406      {
1407         smsvdp->current_palette[i] = smsvdp->CRAM->u8(i) & 0x3f;
1408      }
1409   }
1410}
1411
1412
1413UINT32 sms_vdp_update( device_t *device, bitmap_t *bitmap, const rectangle *cliprect )
1414{
1415   smsvdp_t *smsvdp = get_safe_token(device);
1416   copybitmap(bitmap, smsvdp->tmpbitmap, 0, 0, 0, 0, cliprect);
1417   return 0;
1418}
1419
1420
1421/*****************************************************************************
1422    DEVICE INTERFACE
1423*****************************************************************************/
1424
1425static DEVICE_START( smsvdp )
1426{
1427   smsvdp_t *smsvdp = get_safe_token(device);
1428   const smsvdp_interface *intf = get_interface(device);
1429
1430   screen_device *screen = device->machine().first_screen();
1431   int width = screen->width();
1432   int height = screen->height();
1433
1434   smsvdp->features = intf->model;
1435   smsvdp->int_callback = intf->int_callback;
1436   smsvdp->pause_callback = intf->pause_callback;
1437
1438   /* Allocate video RAM */
1439   smsvdp->VRAM = device->machine().region_alloc("vdp_vram", VRAM_SIZE, 1, ENDIANNESS_LITTLE);
1440   smsvdp->CRAM = device->machine().region_alloc("vdp_cram", MAX_CRAM_SIZE, 1, ENDIANNESS_LITTLE);
1441   smsvdp->line_buffer = auto_alloc_array(device->machine(), int, 256 * 5);
1442
1443   smsvdp->collision_buffer = auto_alloc_array(device->machine(), UINT8, SMS_X_PIXELS);
1444   smsvdp->sms_frame_timing = auto_alloc_array(device->machine(), UINT8, 7);
1445
1446   /* Make temp bitmap for rendering */
1447   smsvdp->tmpbitmap = auto_bitmap_alloc(device->machine(), width, height, BITMAP_FORMAT_INDEXED32);
1448
1449   smsvdp->smsvdp_display_timer = device->machine().scheduler().timer_alloc(FUNC(smsvdp_display_callback), smsvdp);
1450   smsvdp->smsvdp_display_timer->adjust(screen->time_until_pos(0, DISPLAY_CB_HPOS), 0, screen->scan_period());
1451
1452   device->save_item(NAME(smsvdp->status));
1453   device->save_item(NAME(smsvdp->reg9copy));
1454   device->save_item(NAME(smsvdp->addrmode));
1455   device->save_item(NAME(smsvdp->addr));
1456   device->save_item(NAME(smsvdp->cram_mask));
1457   device->save_item(NAME(smsvdp->cram_dirty));
1458   device->save_item(NAME(smsvdp->pending));
1459   device->save_item(NAME(smsvdp->buffer));
1460   device->save_item(NAME(smsvdp->gg_sms_mode));
1461   device->save_item(NAME(smsvdp->irq_state));
1462   device->save_item(NAME(smsvdp->vdp_mode));
1463   device->save_item(NAME(smsvdp->y_pixels));
1464   device->save_item(NAME(smsvdp->line_counter));
1465   device->save_item(NAME(smsvdp->hcounter));
1466   device->save_item(NAME(smsvdp->reg));
1467   device->save_item(NAME(smsvdp->current_palette));
1468   device->save_pointer(NAME(smsvdp->line_buffer), 256 * 5);
1469   device->save_pointer(NAME(smsvdp->collision_buffer), SMS_X_PIXELS);
1470   device->save_item(NAME(*smsvdp->tmpbitmap));
1471}
1472
1473static DEVICE_RESET( smsvdp )
1474{
1475   smsvdp_t *smsvdp = get_safe_token(device);
1476   int i;
1477
1478   /* Most register are 0x00 at power-up */
1479   for (i = 0; i < NUM_OF_REGISTER; i++)
1480      smsvdp->reg[i] = 0x00;
1481
1482   smsvdp->reg[0x02] = 0x0e;
1483   smsvdp->reg[0x0a] = 0xff;
1484
1485   smsvdp->status = 0;
1486   smsvdp->reg9copy = 0;
1487   smsvdp->addrmode = 0;
1488   smsvdp->addr = 0;
1489   smsvdp->gg_sms_mode = 0;
1490   smsvdp->cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
1491   smsvdp->cram_dirty = 1;
1492   smsvdp->pending = 0;
1493   smsvdp->buffer = 0;
1494   smsvdp->irq_state = 0;
1495   smsvdp->line_counter = 0;
1496   smsvdp->hcounter = 0;
1497
1498   for (i = 0; i < 0x20; i++)
1499      smsvdp->current_palette[i] = 0;
1500
1501   set_display_settings(device);
1502
1503   /* Clear RAM */
1504   memset(smsvdp->VRAM->base(), 0, VRAM_SIZE);
1505   memset(smsvdp->CRAM->base(), 0, MAX_CRAM_SIZE);
1506   memset(smsvdp->line_buffer, 0, 256 * 5 * sizeof(int));
1507}
1508
1509DEVICE_GET_INFO( smsvdp )
1510{
1511   switch (state)
1512   {
1513      /* --- the following bits of info are returned as 64-bit signed integers --- */
1514      case DEVINFO_INT_TOKEN_BYTES:         info->i = sizeof(smsvdp_t);               break;
1515
1516      /* --- the following bits of info are returned as pointers to data or functions --- */
1517      case DEVINFO_FCT_START:               info->start = DEVICE_START_NAME(smsvdp);      break;
1518      case DEVINFO_FCT_STOP:               /* Nothing */                           break;
1519      case DEVINFO_FCT_RESET:               info->reset = DEVICE_RESET_NAME(smsvdp);      break;
1520
1521      /* --- the following bits of info are returned as NULL-terminated strings --- */
1522      case DEVINFO_STR_NAME:               strcpy(info->s, "Sega Master SYstem / Game Gear VDP");            break;
1523      case DEVINFO_STR_FAMILY:            strcpy(info->s, "Sega MS VDP");               break;
1524      case DEVINFO_STR_VERSION:            strcpy(info->s, "1.0");                     break;
1525      case DEVINFO_STR_SOURCE_FILE:         strcpy(info->s, __FILE__);                  break;
1526      case DEVINFO_STR_CREDITS:            strcpy(info->s, "Copyright MAME / MESS Team");         break;
1527   }
1528}
1529
1530DEFINE_LEGACY_DEVICE(SMSVDP, smsvdp);
src/mame/video/smsvdp.h
r11572r11573
1/*************************************************************************
2
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
20#define MODEL_315_5124         0x0001
21#define MODEL_315_5246         0x0002
22#define MODEL_315_5378         0x0004
23
24#define SMS_X_PIXELS         342      /* 342 pixels */
25#define NTSC_Y_PIXELS         262      /* 262 lines */
26#define PAL_Y_PIXELS         313      /* 313 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 */
32#define NTSC_192_BBORDER_Y_PIXELS   (0x18)      /* 24 lines */
33#define NTSC_224_TBORDER_Y_PIXELS   (0x0b)      /* 11 lines */
34#define NTSC_224_BBORDER_Y_PIXELS   (0x08)      /* 8 lines */
35#define PAL_192_TBORDER_Y_PIXELS   (0x36)      /* 54 lines */
36#define PAL_192_BBORDER_Y_PIXELS   (0x30)      /* 48 lines */
37#define PAL_224_TBORDER_Y_PIXELS   (0x26)      /* 38 lines */
38#define PAL_224_BBORDER_Y_PIXELS   (0x20)      /* 32 lines */
39#define PAL_240_TBORDER_Y_PIXELS   (0x1e)      /* 30 lines */
40#define PAL_240_BBORDER_Y_PIXELS   (0x18)      /* 24 lines */
41
42
43/***************************************************************************
44    TYPE DEFINITIONS
45***************************************************************************/
46
47typedef void (*smsvdp_int_cb)( running_machine &machine, int state );
48typedef void (*smsvdp_pause_cb)( running_machine &machine );
49
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    DEVICE CONFIGURATION MACROS
60***************************************************************************/
61
62DECLARE_LEGACY_DEVICE(SMSVDP, smsvdp);
63
64#define MCFG_SMSVDP_ADD(_tag, _interface) \
65   MCFG_DEVICE_ADD(_tag, SMSVDP, 0) \
66   MCFG_DEVICE_CONFIG(_interface)
67
68
69/***************************************************************************
70    DEVICE I/O FUNCTIONS
71***************************************************************************/
72
73/* prototypes */
74
75UINT32 sms_vdp_update( device_t *device, bitmap_t *bitmap, const rectangle *cliprect );
76READ8_DEVICE_HANDLER( sms_vdp_vcount_r );
77READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r );
78WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w );
79READ8_DEVICE_HANDLER( sms_vdp_data_r );
80WRITE8_DEVICE_HANDLER( sms_vdp_data_w );
81READ8_DEVICE_HANDLER( sms_vdp_ctrl_r );
82WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w );
83void sms_vdp_set_ggsmsmode( device_t *device, int mode );
84int sms_vdp_check_brightness( device_t *device, int x, int y );
85
86#endif /* __SMSVDP_H__ */
src/mame/drivers/segasms.c
r11572r11573
1/******************************************************************************
2 Contributors:
3
4    Marat Fayzullin (MG source)
5    Charles Mac Donald
6    Mathis Rosenhauer
7    Brad Oliver
8    Michael Luong
9
10 To do:
11
12 - PSG control for Game Gear (needs custom SN76489 with stereo output for each channel)
13 - SIO interface for Game Gear (needs netplay, I guess)
14 - SMS lightgun support
15 - LCD persistence emulation for GG
16 - SMS 3D glass support
17
18 The Game Gear SIO and PSG hardware are not emulated but have some
19 placeholders in 'machine/sms.c'
20
21 Changes:
22    Apr 02 - Added raster palette effects for SMS & GG (ML)
23                 - Added sprite collision (ML)
24                 - Added zoomed sprites (ML)
25    May 02 - Fixed paging bug (ML)
26                 - Fixed sprite and tile priority bug (ML)
27                 - Fixed bug #66 (ML)
28                 - Fixed bug #78 (ML)
29                 - try to implement LCD persistence emulation for GG (ML)
30    Jun 10, 02 - Added bios emulation (ML)
31    Jun 12, 02 - Added PAL & NTSC systems (ML)
32    Jun 25, 02 - Added border emulation (ML)
33    Jun 27, 02 - Version bits for Game Gear (bits 6 of port 00) (ML)
34    Nov-Dec, 05 - Numerous cleanups, fixes, updates (WP)
35    Mar, 07 - More cleanups, fixes, mapper additions, etc (WP)
36
37SMS Store Unit memory map for the second CPU:
38
390000-3FFF - BIOS
404000-47FF - RAM
418000      - System Control Register (R/W)
42            Reading:
43            bit7      - ready (0 = ready, 1 = not ready)
44            bit6-bit5 - unknown
45            bit4-bit3 - timer selection bit switches
46            bit2-bit0 - unknown
47            Writing:
48            bit7-bit4 - unknown, maybe led of selected game to set?
49            bit3      - unknown, 1 seems to be written all the time
50            bit2      - unknown, 1 seems to be written all the time
51            bit1      - reset signal for sms cpu, 0 = reset low, 1 = reset high
52            bit0      - which cpu receives interrupt signals, 0 = sms cpu, 1 = controlling cpu
53C000      - Card/Cartridge selction register (W)
54            bit7-bit4 - slot to select
55            bit3      - slot type (0 = cartridge, 1 = card ?)
56            bit2-bit0 - unknown
57C400      - ???? (used once)
58D800      - Selection buttons #1, 1-8 (R)
59DC00      - Selection buttons #2, 9-16 (R)
60
61 ******************************************************************************/
62
63#include "emu.h"
64#include "cpu/z80/z80.h"
65#include "sound/sn76496.h"
66#include "sound/2413intf.h"
67#include "includes/segasms.h"
68#include "video/smsvdp.h"
69#include "imagedev/cartslot.h"
70
71#include "sms1.lh"
72
73#define MASTER_CLOCK_PAL   53203400   /* This might be a tiny bit too low */
74
75
76static ADDRESS_MAP_START( sms1_mem, AS_PROGRAM, 8 )
77   AM_RANGE(0x0000, 0x03ff) AM_ROMBANK("bank1")               /* First 0x0400 part always points to first page */
78   AM_RANGE(0x0400, 0x3fff) AM_ROMBANK("bank2")               /* switchable rom bank */
79   AM_RANGE(0x4000, 0x5fff) AM_ROMBANK("bank3")               /* switchable rom bank */
80   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank4")               /* switchable rom bank */
81   AM_RANGE(0x8000, 0x9fff) AM_READ_BANK("bank5") AM_WRITE(sms_cartram_w)   /* ROM bank / on-cart RAM */
82   AM_RANGE(0xa000, 0xbfff) AM_READ_BANK("bank6") AM_WRITE(sms_cartram2_w)   /* ROM bank / on-cart RAM */
83   AM_RANGE(0xc000, 0xdff7) AM_MIRROR(0x2000) AM_RAM         /* RAM (mirror at 0xE000) */
84   AM_RANGE(0xdff8, 0xdfff) AM_RAM                  /* RAM "underneath" frame registers */
85   AM_RANGE(0xfff8, 0xfffb) AM_READWRITE(sms_sscope_r, sms_sscope_w)   /* 3-D glasses */
86   AM_RANGE(0xfffc, 0xffff) AM_READWRITE(sms_mapper_r, sms_mapper_w)   /* Bankswitch control */
87ADDRESS_MAP_END
88
89static ADDRESS_MAP_START( sms_mem, AS_PROGRAM, 8 )
90   AM_RANGE(0x0000, 0x03ff) AM_ROMBANK("bank1")               /* First 0x0400 part always points to first page */
91   AM_RANGE(0x0400, 0x3fff) AM_ROMBANK("bank2")               /* switchable rom bank */
92   AM_RANGE(0x4000, 0x5fff) AM_ROMBANK("bank3")               /* switchable rom bank */
93   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank4")               /* switchable rom bank */
94   AM_RANGE(0x8000, 0x9fff) AM_READ_BANK("bank5") AM_WRITE(sms_cartram_w)   /* ROM bank / on-cart RAM */
95   AM_RANGE(0xa000, 0xbfff) AM_READ_BANK("bank6") AM_WRITE(sms_cartram2_w)   /* ROM bank / on-cart RAM */
96   AM_RANGE(0xc000, 0xdffb) AM_MIRROR(0x2000) AM_RAM         /* RAM (mirror at 0xE000) */
97   AM_RANGE(0xdffc, 0xdfff) AM_RAM                  /* RAM "underneath" frame registers */
98   AM_RANGE(0xfffc, 0xffff) AM_READWRITE(sms_mapper_r, sms_mapper_w)   /* Bankswitch control */
99ADDRESS_MAP_END
100
101static ADDRESS_MAP_START( sms_store_mem, AS_PROGRAM, 8 )
102   AM_RANGE(0x0000, 0x3fff) AM_ROM                  /* BIOS */
103   AM_RANGE(0x4000, 0x47ff) AM_RAM                  /* RAM */
104   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank10")               /* Cartridge/card peek area */
105   AM_RANGE(0x8000, 0x8000) AM_READWRITE(sms_store_control_r, sms_store_control_w)   /* Control */
106   AM_RANGE(0xc000, 0xc000) AM_READWRITE(sms_store_cart_select_r, sms_store_cart_select_w)    /* cartridge/card slot selector */
107   AM_RANGE(0xd800, 0xd800) AM_READ(sms_store_select1)         /* Game selector port #1 */
108   AM_RANGE(0xdc00, 0xdc00) AM_READ(sms_store_select2)         /* Game selector port #2 */
109ADDRESS_MAP_END
110
111static ADDRESS_MAP_START( sms_io, AS_IO, 8 )
112   ADDRESS_MAP_GLOBAL_MASK(0xff)
113   ADDRESS_MAP_UNMAP_HIGH
114   AM_RANGE(0x00, 0x00) AM_MIRROR(0x3e) AM_WRITE(sms_bios_w)
115   AM_RANGE(0x01, 0x01) AM_MIRROR(0x3e) AM_WRITE(sms_io_control_w)
116   AM_RANGE(0x40, 0x7f)                 AM_READ(sms_count_r)
117   AM_RANGE(0x40, 0x7f)                 AM_DEVWRITE("smsiii", sn76496_w)
118   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
119   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
120   AM_RANGE(0xc0, 0xc0) AM_MIRROR(0x1e) AM_READ(sms_input_port_0_r)
121   AM_RANGE(0xc1, 0xc1) AM_MIRROR(0x1e) AM_READ(sms_input_port_1_r)
122   AM_RANGE(0xe0, 0xe0) AM_MIRROR(0x0e) AM_READ(sms_input_port_0_r)
123   AM_RANGE(0xe1, 0xe1) AM_MIRROR(0x0e) AM_READ(sms_input_port_1_r)
124   AM_RANGE(0xf0, 0xf0)                 AM_READWRITE(sms_input_port_0_r, sms_ym2413_register_port_0_w)
125   AM_RANGE(0xf1, 0xf1)                 AM_READWRITE(sms_input_port_1_r, sms_ym2413_data_port_0_w)
126   AM_RANGE(0xf2, 0xf2)                 AM_READWRITE(sms_fm_detect_r, sms_fm_detect_w)
127   AM_RANGE(0xf3, 0xf3)                 AM_READ(sms_input_port_1_r)
128   AM_RANGE(0xf4, 0xf4) AM_MIRROR(0x02) AM_READ(sms_input_port_0_r)
129   AM_RANGE(0xf5, 0xf5) AM_MIRROR(0x02) AM_READ(sms_input_port_1_r)
130   AM_RANGE(0xf8, 0xf8) AM_MIRROR(0x06) AM_READ(sms_input_port_0_r)
131   AM_RANGE(0xf9, 0xf9) AM_MIRROR(0x06) AM_READ(sms_input_port_1_r)
132ADDRESS_MAP_END
133
134static ADDRESS_MAP_START( gg_io, AS_IO, 8 )
135   ADDRESS_MAP_GLOBAL_MASK(0xff)
136   ADDRESS_MAP_UNMAP_HIGH
137   AM_RANGE(0x00, 0x00)                 AM_READ(gg_input_port_2_r)
138   AM_RANGE(0x01, 0x05)                 AM_READWRITE(gg_sio_r, gg_sio_w)
139   AM_RANGE(0x06, 0x06)                 AM_DEVWRITE("gamegear", sn76496_stereo_w)
140   AM_RANGE(0x07, 0x07)                 AM_WRITE(sms_io_control_w)
141   AM_RANGE(0x08, 0x08) AM_MIRROR(0x06) AM_WRITE(sms_bios_w)
142   AM_RANGE(0x09, 0x09) AM_MIRROR(0x06) AM_WRITE(sms_io_control_w)
143   AM_RANGE(0x10, 0x10) AM_MIRROR(0x0e) AM_WRITE(sms_bios_w)
144   AM_RANGE(0x11, 0x11) AM_MIRROR(0x0e) AM_WRITE(sms_io_control_w)
145   AM_RANGE(0x20, 0x20) AM_MIRROR(0x1e) AM_WRITE(sms_bios_w)
146   AM_RANGE(0x21, 0x21) AM_MIRROR(0x1e) AM_WRITE(sms_io_control_w)
147   AM_RANGE(0x40, 0x7f)                 AM_READ(sms_count_r)
148   AM_RANGE(0x40, 0x7f)                 AM_DEVWRITE("gamegear", sn76496_w)
149   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
150   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
151   AM_RANGE(0xc0, 0xc0)                 AM_READ_PORT("PORT_DC")
152   AM_RANGE(0xc1, 0xc1)                 AM_READ_PORT("PORT_DD")
153   AM_RANGE(0xdc, 0xdc)                 AM_READ_PORT("PORT_DC")
154   AM_RANGE(0xdd, 0xdd)                 AM_READ_PORT("PORT_DD")
155ADDRESS_MAP_END
156
157
158static INPUT_PORTS_START( sms )
159   PORT_START("PORT_DC")
160   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
161   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
162   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
163   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
164   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(10) PORT_PLAYER(1)
165   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(10) PORT_PLAYER(1)
166   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
167   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
168
169   PORT_START("PORT_DD")
170   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
171   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
172   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(20) PORT_PLAYER(2)
173   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(20) PORT_PLAYER(2)
174   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNUSED ) /* Software Reset bit */
175   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_UNUSED )
176   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNUSED ) /* Port A TH */
177   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNUSED ) /* Port B TH */
178
179   PORT_START("PAUSE")
180   PORT_BIT( 0x7f, IP_ACTIVE_LOW, IPT_UNUSED )
181   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_START ) PORT_NAME(DEF_STR(Pause))
182
183   PORT_START("LPHASER0")   /* Light phaser X - player 1 */
184   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) PORT_CHANGED(lgun1_changed, NULL)
185
186   PORT_START("LPHASER1")   /* Light phaser Y - player 1 */
187   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) PORT_CHANGED(lgun1_changed, NULL)
188
189   PORT_START("LPHASER2")   /* Light phaser X - player 2 */
190   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) PORT_CHANGED(lgun2_changed, NULL)
191
192   PORT_START("LPHASER3")   /* Light phaser Y - player 2 */
193   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) PORT_CHANGED(lgun2_changed, NULL)
194
195   PORT_START("RFU")   /* Rapid Fire Unit */
196   PORT_CONFNAME( 0x03, 0x00, "Rapid Fire Unit - Player 1" )
197   PORT_CONFSETTING(   0x00, DEF_STR( Off ) )
198   PORT_CONFSETTING(   0x01, "Button A" )
199   PORT_CONFSETTING(   0x02, "Button B" )
200   PORT_CONFSETTING(   0x03, "Button A + B" )
201   PORT_CONFNAME( 0x0c, 0x00, "Rapid Fire Unit - Player 2" )
202   PORT_CONFSETTING(   0x00, DEF_STR( Off ) )
203   PORT_CONFSETTING(   0x04, "Button A" )
204   PORT_CONFSETTING(   0x08, "Button B" )
205   PORT_CONFSETTING(   0x0c, "Button A + B" )
206
207   PORT_START("PADDLE0")   /* Paddle player 1 */
208   PORT_BIT( 0xff, 0x80, IPT_PADDLE) PORT_SENSITIVITY(40) PORT_KEYDELTA(20) PORT_CENTERDELTA(0) PORT_MINMAX(0,255) PORT_CATEGORY(12) PORT_PLAYER(1)
209
210   PORT_START("PADDLE1")   /* Paddle player 2 */
211   PORT_BIT( 0xff, 0x80, IPT_PADDLE) PORT_SENSITIVITY(40) PORT_KEYDELTA(20) PORT_CENTERDELTA(0) PORT_MINMAX(0,255) PORT_CATEGORY(22) PORT_PLAYER(2)
212
213   PORT_START("CTRLIPT")   /* Light Phaser and Paddle Control buttons */
214   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(11) PORT_PLAYER(1)
215   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(12) PORT_PLAYER(1)
216   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(13) PORT_PLAYER(1)
217   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(13) PORT_PLAYER(1)
218   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(21) PORT_PLAYER(2)
219   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(22) PORT_PLAYER(2)
220   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(23) PORT_PLAYER(2)
221   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(23) PORT_PLAYER(2)
222
223   PORT_START("CTRLSEL")   /* Controller selection */
224   PORT_CATEGORY_CLASS( 0x0f, 0x00, "Player 1 Controller" )
225   PORT_CATEGORY_ITEM( 0x00, DEF_STR( Joystick ), 10 )
226   PORT_CATEGORY_ITEM( 0x01, "Light Phaser", 11 )
227   PORT_CATEGORY_ITEM( 0x02, "Sega Paddle Control", 12 )
228   PORT_CATEGORY_ITEM( 0x03, "Sega Sports Pad", 13 )
229   PORT_CATEGORY_CLASS( 0xf0, 0x00, "Player 2 Controller" )
230   PORT_CATEGORY_ITEM( 0x00, DEF_STR( Joystick ), 20 )
231   PORT_CATEGORY_ITEM( 0x10, "Light Phaser", 21 )
232   PORT_CATEGORY_ITEM( 0x20, "Sega Paddle Control", 22 )
233   PORT_CATEGORY_ITEM( 0x30, "Sega Sports Pad", 23 )
234
235   PORT_START("SPORT0")   /* Player 1 Sports Pad X axis */
236   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(13) PORT_PLAYER(1)
237
238   PORT_START("SPORT1")   /* Player 1 Sports Pad Y axis */
239   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_Y ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(13) PORT_PLAYER(1)
240
241   PORT_START("SPORT2")   /* Player 2 Sports Pad X axis */
242   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(23) PORT_PLAYER(2)
243
244   PORT_START("SPORT3")   /* Player 2 Sports Pad Y axis */
245   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_Y ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(23) PORT_PLAYER(2)
246INPUT_PORTS_END
247
248static INPUT_PORTS_START( sms1 )
249   PORT_INCLUDE( sms )
250
251   PORT_START("RESET")
252   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SERVICE1 ) PORT_NAME("Reset Button")
253
254   PORT_START("SEGASCOPE")
255   PORT_CONFNAME( 0x01, 0x00, "SegaScope (3-D Glasses)" )
256   PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
257   PORT_CONFSETTING( 0x01, DEF_STR( On ) )
258
259   PORT_START("SSCOPE_BINOCULAR")
260   PORT_CONFNAME( 0x03, 0x00, "SegaScope - Binocular Hack" ) PORT_CONDITION("SEGASCOPE", 0x01, PORTCOND_EQUALS, 0x01)
261   PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
262   PORT_CONFSETTING( 0x01, "Left Lens" )
263   PORT_CONFSETTING( 0x02, "Right Lens" )
264   PORT_CONFSETTING( 0x03, "Both Lens" )
265   PORT_BIT( 0x03, 0x00, IPT_UNUSED ) PORT_CONDITION("SEGASCOPE", 0x01, PORTCOND_EQUALS, 0x00)
266INPUT_PORTS_END
267
268static INPUT_PORTS_START( smsj )
269   PORT_INCLUDE( sms1 )
270
271   PORT_START("TVDRAW")
272   PORT_CONFNAME( 0x01, 0x00, "Terebi Oekaki Graphics Tablet" )
273   PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
274   PORT_CONFSETTING( 0x01, DEF_STR( On ) )
275
276   PORT_START("TVDRAW_X")
277   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_NAME("Tablet - X Axis") PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(10) PORT_PLAYER(1)
278      PORT_CONDITION("TVDRAW", 0x01, PORTCOND_EQUALS, 0x01)
279
280   PORT_START("TVDRAW_Y")
281   PORT_BIT( 0xff, 0x60, IPT_LIGHTGUN_Y ) PORT_NAME("Tablet - Y Axis") PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_MINMAX(0, 191) PORT_SENSITIVITY(50) PORT_KEYDELTA(10) PORT_PLAYER(1)
282      PORT_CONDITION("TVDRAW", 0x01, PORTCOND_EQUALS, 0x01)
283
284   PORT_START("TVDRAW_PEN")
285   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("Tablet - Pen") PORT_CONDITION("TVDRAW", 0x01, PORTCOND_EQUALS, 0x01)
286INPUT_PORTS_END
287
288static INPUT_PORTS_START( gg )
289   PORT_START("PORT_DC")
290   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(1) PORT_8WAY
291   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(1) PORT_8WAY
292   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_PLAYER(1) PORT_8WAY
293   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(1) PORT_8WAY
294   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1)
295   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1)
296   PORT_BIT( 0xc0, IP_ACTIVE_LOW, IPT_UNUSED )
297
298   PORT_START("PORT_DD")
299   PORT_BIT( 0xff, IP_ACTIVE_LOW, IPT_UNUSED )
300
301   PORT_START("START")
302   PORT_BIT( 0x7f, IP_ACTIVE_LOW, IPT_UNUSED )
303   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_START ) PORT_NAME("Start") /* Game Gear START */
304INPUT_PORTS_END
305
306static PALETTE_INIT( sms )
307{
308   int i;
309   for (i = 0; i < 64; i++)
310   {
311      int r = i & 0x03;
312      int g = (i & 0x0c) >> 2;
313      int b = (i & 0x30) >> 4;
314      palette_set_color_rgb(machine, i, r << 6, g << 6, b << 6);
315   }
316   /* TMS9918 palette */
317   palette_set_color_rgb(machine, 64+ 0,   0,   0,   0);
318   palette_set_color_rgb(machine, 64+ 1,   0,   0,   0);
319   palette_set_color_rgb(machine, 64+ 2,  33, 200,  66);
320   palette_set_color_rgb(machine, 64+ 3,  94, 220, 120);
321   palette_set_color_rgb(machine, 64+ 4,  84,  85, 237);
322   palette_set_color_rgb(machine, 64+ 5, 125, 118, 252);
323   palette_set_color_rgb(machine, 64+ 6, 212,  82,  77);
324   palette_set_color_rgb(machine, 64+ 7,  66, 235, 245);
325   palette_set_color_rgb(machine, 64+ 8, 252,  85,  84);
326   palette_set_color_rgb(machine, 64+ 9, 255, 121, 120);
327   palette_set_color_rgb(machine, 64+10, 212, 193,  84);
328   palette_set_color_rgb(machine, 64+11, 230, 206, 128);
329   palette_set_color_rgb(machine, 64+12,  33, 176,  59);
330   palette_set_color_rgb(machine, 64+13, 201,  91, 186);
331   palette_set_color_rgb(machine, 64+14, 204, 204, 204);
332   palette_set_color_rgb(machine, 64+15, 255, 255, 255);
333}
334
335static PALETTE_INIT( gamegear )
336{
337   int i;
338   for (i = 0; i < 4096; i++)
339   {
340      int r = i & 0x000f;
341      int g = (i & 0x00f0) >> 4;
342      int b = (i & 0x0f00) >> 8;
343      palette_set_color_rgb(machine, i, r << 4, g << 4, b << 4);
344   }
345}
346
347
348
349static void sms_int_callback( running_machine &machine, int state )
350{
351   cputag_set_input_line(machine, "maincpu", 0, state);
352}
353
354static const smsvdp_interface _315_5124_intf =
355{
356   MODEL_315_5124,
357   sms_int_callback,
358   sms_pause_callback
359};
360
361static const smsvdp_interface _315_5246_intf =
362{
363   MODEL_315_5246,
364   sms_int_callback,
365   sms_pause_callback
366};
367
368static const smsvdp_interface _315_5378_intf =
369{
370   MODEL_315_5378,
371   sms_int_callback,
372   sms_pause_callback
373};
374
375static const smsvdp_interface sms_store_intf =
376{
377   MODEL_315_5124,
378   sms_store_int_callback,
379   sms_pause_callback
380};
381
382static MACHINE_CONFIG_FRAGMENT( sms_cartslot )
383   MCFG_CARTSLOT_ADD("cart1")
384   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin")
385   MCFG_CARTSLOT_NOT_MANDATORY
386   MCFG_CARTSLOT_INTERFACE("sms_cart")
387   MCFG_CARTSLOT_START(sms_cart)
388   MCFG_CARTSLOT_LOAD(sms_cart)
389
390   MCFG_SOFTWARE_LIST_ADD("cart_list","sms")
391MACHINE_CONFIG_END
392
393static MACHINE_CONFIG_FRAGMENT( gg_cartslot )
394   MCFG_CARTSLOT_ADD("cart1")
395   MCFG_CARTSLOT_EXTENSION_LIST("gg,bin")
396   MCFG_CARTSLOT_MANDATORY
397   MCFG_CARTSLOT_INTERFACE("gamegear_cart")
398   MCFG_CARTSLOT_START(sms_cart)
399   MCFG_CARTSLOT_LOAD(sms_cart)
400
401   MCFG_SOFTWARE_LIST_ADD("cart_list","gamegear")
402MACHINE_CONFIG_END
403
404static MACHINE_CONFIG_START( sms_ntsc_base, sms_state )
405   /* basic machine hardware */
406   MCFG_CPU_ADD("maincpu", Z80, XTAL_53_693175MHz/15)
407   MCFG_CPU_PROGRAM_MAP(sms_mem)
408   MCFG_CPU_IO_MAP(sms_io)
409
410   MCFG_QUANTUM_TIME(attotime::from_hz(60))
411
412   MCFG_MACHINE_START(sms)
413   MCFG_MACHINE_RESET(sms)
414
415   /* sound hardware */
416   MCFG_SPEAKER_STANDARD_MONO("mono")
417   MCFG_SOUND_ADD("smsiii", SMSIII, XTAL_53_693175MHz/15)
418   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
419
420   MCFG_FRAGMENT_ADD( sms_cartslot )
421MACHINE_CONFIG_END
422
423static MACHINE_CONFIG_DERIVED( sms2_ntsc, sms_ntsc_base )
424   /* video hardware */
425   MCFG_SCREEN_ADD("screen", RASTER)
426   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
427   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
428   MCFG_SCREEN_UPDATE(sms)
429
430   MCFG_PALETTE_LENGTH(64+16)
431   MCFG_PALETTE_INIT(sms)
432
433   MCFG_SMSVDP_ADD("sms_vdp", _315_5246_intf)
434MACHINE_CONFIG_END
435
436static MACHINE_CONFIG_DERIVED( sms1_ntsc, sms_ntsc_base )
437
438   MCFG_CPU_MODIFY("maincpu")
439   MCFG_CPU_PROGRAM_MAP(sms1_mem)   // This adds the SegaScope handlers for 3-D glasses
440   MCFG_CPU_IO_MAP(sms_io)
441
442   /* video hardware */
443   MCFG_SCREEN_ADD("screen", RASTER)
444   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
445   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
446   MCFG_SCREEN_UPDATE(sms1)
447
448   MCFG_SCREEN_ADD("left_lcd", LCD)   // This is needed for SegaScope Left LCD
449   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
450   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
451   MCFG_SCREEN_UPDATE(sms1)
452
453   MCFG_SCREEN_ADD("right_lcd", LCD)   // This is needed for SegaScope Right LCD
454   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
455   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
456   MCFG_SCREEN_UPDATE(sms1)
457
458   MCFG_DEFAULT_LAYOUT(layout_sms1)
459
460   MCFG_PALETTE_LENGTH(64+16)
461   MCFG_PALETTE_INIT(sms)
462
463   MCFG_VIDEO_START(sms1)
464
465   MCFG_SMSVDP_ADD("sms_vdp", _315_5124_intf)
466MACHINE_CONFIG_END
467
468#define MCFG_SMSSDISP_CARTSLOT_ADD(_tag) \
469   MCFG_CARTSLOT_ADD(_tag) \
470   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin") \
471   MCFG_CARTSLOT_NOT_MANDATORY \
472   MCFG_CARTSLOT_INTERFACE("sms_cart") \
473   MCFG_CARTSLOT_START(sms_cart) \
474   MCFG_CARTSLOT_LOAD(sms_cart)
475
476static MACHINE_CONFIG_DERIVED( sms_sdisp, sms2_ntsc )
477
478   MCFG_DEVICE_REMOVE("sms_vdp")
479   MCFG_SMSVDP_ADD("sms_vdp", sms_store_intf)
480
481   MCFG_CPU_ADD("control", Z80, XTAL_53_693175MHz/15)
482   MCFG_CPU_PROGRAM_MAP(sms_store_mem)
483   /* Both CPUs seem to communicate with the VDP etc? */
484   MCFG_CPU_IO_MAP(sms_io)
485
486   MCFG_CARTSLOT_MODIFY("cart1")
487   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin")
488   MCFG_CARTSLOT_MANDATORY
489   MCFG_CARTSLOT_INTERFACE("sms_cart")
490   MCFG_CARTSLOT_START(sms_cart)
491   MCFG_CARTSLOT_LOAD(sms_cart)
492
493   MCFG_SMSSDISP_CARTSLOT_ADD("cart2")
494   MCFG_SMSSDISP_CARTSLOT_ADD("cart3")
495   MCFG_SMSSDISP_CARTSLOT_ADD("cart4")
496   MCFG_SMSSDISP_CARTSLOT_ADD("cart5")
497   MCFG_SMSSDISP_CARTSLOT_ADD("cart6")
498   MCFG_SMSSDISP_CARTSLOT_ADD("cart7")
499   MCFG_SMSSDISP_CARTSLOT_ADD("cart8")
500   MCFG_SMSSDISP_CARTSLOT_ADD("cart9")
501   MCFG_SMSSDISP_CARTSLOT_ADD("cart10")
502   MCFG_SMSSDISP_CARTSLOT_ADD("cart11")
503   MCFG_SMSSDISP_CARTSLOT_ADD("cart12")
504   MCFG_SMSSDISP_CARTSLOT_ADD("cart13")
505   MCFG_SMSSDISP_CARTSLOT_ADD("cart14")
506   MCFG_SMSSDISP_CARTSLOT_ADD("cart15")
507   MCFG_SMSSDISP_CARTSLOT_ADD("cart16")
508MACHINE_CONFIG_END
509
510static MACHINE_CONFIG_START( sms_pal_base, sms_state )
511   /* basic machine hardware */
512   MCFG_CPU_ADD("maincpu", Z80, MASTER_CLOCK_PAL/15)
513   MCFG_CPU_PROGRAM_MAP(sms_mem)
514   MCFG_CPU_IO_MAP(sms_io)
515
516   MCFG_QUANTUM_TIME(attotime::from_hz(60))
517
518   MCFG_MACHINE_START(sms)
519   MCFG_MACHINE_RESET(sms)
520
521   /* sound hardware */
522   MCFG_SPEAKER_STANDARD_MONO("mono")
523   MCFG_SOUND_ADD("smsiii", SMSIII, MASTER_CLOCK_PAL/15)
524   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
525
526   MCFG_FRAGMENT_ADD( sms_cartslot )
527MACHINE_CONFIG_END
528
529static MACHINE_CONFIG_DERIVED( sms2_pal, sms_pal_base )
530
531   /* video hardware */
532   MCFG_SCREEN_ADD("screen", RASTER)
533   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
534   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
535   MCFG_SCREEN_UPDATE(sms)
536
537   MCFG_PALETTE_LENGTH(64+16)
538   MCFG_PALETTE_INIT(sms)
539
540   MCFG_SMSVDP_ADD("sms_vdp", _315_5246_intf)
541MACHINE_CONFIG_END
542
543static MACHINE_CONFIG_DERIVED( sms1_pal, sms_pal_base )
544
545   MCFG_CPU_MODIFY("maincpu")
546   MCFG_CPU_PROGRAM_MAP(sms1_mem)   // This adds the SegaScope handlers for 3-D glasses
547   MCFG_CPU_IO_MAP(sms_io)
548
549   /* video hardware */
550   MCFG_SCREEN_ADD("screen", RASTER)
551   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
552   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
553   MCFG_SCREEN_UPDATE(sms1)
554
555   MCFG_SCREEN_ADD("left_lcd", LCD)   // This is needed for SegaScope Left LCD
556   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
557   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
558   MCFG_SCREEN_UPDATE(sms1)
559
560   MCFG_SCREEN_ADD("right_lcd", LCD)   // This is needed for SegaScope Right LCD
561   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
562   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
563   MCFG_SCREEN_UPDATE(sms1)
564
565   MCFG_PALETTE_LENGTH(64+16)
566   MCFG_PALETTE_INIT(sms)
567
568   MCFG_DEFAULT_LAYOUT(layout_sms1)
569
570   MCFG_SMSVDP_ADD("sms_vdp", _315_5124_intf)
571MACHINE_CONFIG_END
572
573static MACHINE_CONFIG_DERIVED( sms_fm, sms1_ntsc )
574
575   MCFG_SOUND_ADD("ym2413", YM2413, XTAL_53_693175MHz/15)
576   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
577MACHINE_CONFIG_END
578
579static MACHINE_CONFIG_DERIVED( sg1000m3, sms_fm )
580
581   MCFG_CARTSLOT_MODIFY("cart1")
582   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin,sg")
583   MCFG_CARTSLOT_MANDATORY
584   MCFG_CARTSLOT_START(sms_cart)
585   MCFG_CARTSLOT_LOAD(sms_cart)
586MACHINE_CONFIG_END
587
588static MACHINE_CONFIG_DERIVED( sms2_fm, sms2_ntsc )
589
590   MCFG_SOUND_ADD("ym2413", YM2413, XTAL_53_693175MHz/15)
591   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
592MACHINE_CONFIG_END
593
594static MACHINE_CONFIG_START( gamegear, sms_state )
595   /* basic machine hardware */
596   MCFG_CPU_ADD("maincpu", Z80, XTAL_53_693175MHz/15)
597   MCFG_CPU_PROGRAM_MAP(sms_mem)
598   MCFG_CPU_IO_MAP(gg_io)
599
600   MCFG_QUANTUM_TIME(attotime::from_hz(60))
601
602   MCFG_MACHINE_START(sms)
603   MCFG_MACHINE_RESET(sms)
604
605   /* video hardware */
606   MCFG_SCREEN_ADD("screen", LCD)
607   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
608   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS + 6*8, LBORDER_START + LBORDER_X_PIXELS + 26*8, NTSC_Y_PIXELS, TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 3*8, TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 21*8 )
609   MCFG_SCREEN_UPDATE(gamegear)
610
611   MCFG_PALETTE_LENGTH(4096)
612   MCFG_PALETTE_INIT(gamegear)
613
614   MCFG_VIDEO_START(gamegear)
615
616   MCFG_SMSVDP_ADD("sms_vdp", _315_5378_intf)
617
618   /* sound hardware */
619   MCFG_SPEAKER_STANDARD_STEREO("lspeaker","rspeaker")
620   MCFG_SOUND_ADD("gamegear", GAMEGEAR, XTAL_53_693175MHz/15)
621   MCFG_SOUND_ROUTE(0, "lspeaker", 1.00)
622   MCFG_SOUND_ROUTE(1, "rspeaker", 1.00)
623
624   /* cartridge */
625   MCFG_FRAGMENT_ADD( gg_cartslot )
626MACHINE_CONFIG_END
627
628
629ROM_START(sms1)
630   ROM_REGION(0x4000, "maincpu", 0)
631   ROM_FILL(0x0000, 0x4000, 0xff)
632
633   ROM_REGION(0x20000, "user1", 0)
634   ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
635   ROMX_LOAD("bios13fx.rom", 0x0000, 0x2000, CRC(0072ED54) SHA1(c315672807d8ddb8d91443729405c766dd95cae7), ROM_BIOS(1))
636   ROM_SYSTEM_BIOS( 1, "hangonsh", "US/European BIOS v2.4 with Hang On and Safari Hunt (1988)" )
637   ROMX_LOAD("hshbios.rom", 0x0000, 0x20000, CRC(91E93385) SHA1(9e179392cd416af14024d8f31c981d9ee9a64517), ROM_BIOS(2))
638   ROM_SYSTEM_BIOS( 2, "hangon", "US/European BIOS v3.4 with Hang On (1988)" )
639   ROMX_LOAD("hangbios.rom", 0x0000, 0x20000, CRC(8EDF7AC6) SHA1(51fd6d7990f62cd9d18c9ecfc62ed7936169107e), ROM_BIOS(3))
640   ROM_SYSTEM_BIOS( 3, "missiled", "US/European BIOS v4.4 with Missile Defense 3D (1988)" )
641   ROMX_LOAD("missiled.rom", 0x0000, 0x20000, CRC(E79BB689) SHA1(aa92ae576ca670b00855e278378d89e9f85e0351), ROM_BIOS(4))
642   ROM_SYSTEM_BIOS( 4, "proto", "US Master System Prototype BIOS" )
643   ROMX_LOAD("m404prot.rom", 0x0000, 0x2000, CRC(1a15dfcc) SHA1(4a06c8e66261611dce0305217c42138b71331701), ROM_BIOS(5))
644ROM_END
645
646ROM_START(sms)
647   ROM_REGION(0x4000, "maincpu", 0)
648   ROM_FILL(0x0000, 0x4000, 0xff)
649
650   ROM_REGION(0x20000, "user1", 0)
651   ROM_SYSTEM_BIOS( 0, "alexkidd", "US/European BIOS with Alex Kidd in Miracle World (1990)" )
652   ROMX_LOAD("akbios.rom", 0x0000, 0x20000, CRC(CF4A09EA) SHA1(3af7b66248d34eb26da40c92bf2fa4c73a46a051), ROM_BIOS(1))
653ROM_END
654
655ROM_START(smssdisp)
656   ROM_REGION(0x4000, "maincpu", 0)
657   ROM_FILL(0x0000, 0x4000, 0x00)
658
659   ROM_REGION(0x4000, "user1", 0)
660   ROM_FILL(0x0000, 0x4000, 0xff)
661
662   ROM_REGION(0x4000, "control", 0)
663   ROM_LOAD("smssdisp.rom", 0x0000, 0x4000, CRC(ee2c29ba) SHA1(fc465122134d95363112eb51b9ab71db3576cefd))
664ROM_END
665
666ROM_START(sms1pal)
667   ROM_REGION(0x4000, "maincpu", 0)
668   ROM_FILL(0x0000, 0x4000, 0xff)
669
670   ROM_REGION(0x20000, "user1", 0)
671   ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
672   ROMX_LOAD("bios13fx.rom", 0x0000, 0x2000, CRC(0072ED54) SHA1(c315672807d8ddb8d91443729405c766dd95cae7), ROM_BIOS(1))
673   ROM_SYSTEM_BIOS( 1, "hangonsh", "US/European BIOS v2.4 with Hang On and Safari Hunt (1988)" )
674   ROMX_LOAD("hshbios.rom", 0x0000, 0x20000, CRC(91E93385) SHA1(9e179392cd416af14024d8f31c981d9ee9a64517), ROM_BIOS(2))
675   ROM_SYSTEM_BIOS( 2, "hangon", "Sega Master System - US/European BIOS v3.4 with Hang On (1988)" )
676   ROMX_LOAD("hangbios.rom", 0x0000, 0x20000, CRC(8EDF7AC6) SHA1(51fd6d7990f62cd9d18c9ecfc62ed7936169107e), ROM_BIOS(3))
677   ROM_SYSTEM_BIOS( 3, "missiled", "US/European BIOS v4.4 with Missile Defense 3D (1988)" )
678   ROMX_LOAD("missiled.rom", 0x0000, 0x20000, CRC(E79BB689) SHA1(aa92ae576ca670b00855e278378d89e9f85e0351), ROM_BIOS(4))
679ROM_END
680
681ROM_START(smspal)
682   ROM_REGION(0x4000, "maincpu", 0)
683   ROM_FILL(0x0000, 0x4000, 0xff)
684
685   ROM_REGION(0x40000, "user1", 0)
686   ROM_SYSTEM_BIOS( 0, "alexkidd", "US/European BIOS with Alex Kidd in Miracle World (1990)" )
687   ROMX_LOAD("akbios.rom", 0x0000, 0x20000, CRC(CF4A09EA) SHA1(3af7b66248d34eb26da40c92bf2fa4c73a46a051), ROM_BIOS(1))
688   ROM_SYSTEM_BIOS( 1, "sonic", "European/Brazilian BIOS with Sonic the Hedgehog (1991)" )
689   ROMX_LOAD("sonbios.rom", 0x0000, 0x40000, CRC(81C3476B) SHA1(6aca0e3dffe461ba1cb11a86cd4caf5b97e1b8df), ROM_BIOS(2))
690ROM_END
691
692ROM_START(sg1000m3)
693   ROM_REGION(0x4000, "maincpu", 0)
694   ROM_FILL(0x0000, 0x4000, 0x00)
695ROM_END
696
697ROM_START(smsj)
698   ROM_REGION(0x4000, "maincpu", 0)
699   ROM_FILL(0x0000, 0x4000, 0xff)
700
701   ROM_REGION(0x4000, "user1", 0)
702   ROM_SYSTEM_BIOS( 0, "jbios21", "Japanese BIOS v2.1 (1987)" )
703   ROMX_LOAD("jbios21.rom", 0x0000, 0x2000, CRC(48D44A13) SHA1(a8c1b39a2e41137835eda6a5de6d46dd9fadbaf2), ROM_BIOS(1))
704ROM_END
705
706ROM_START(sms2kr)
707   ROM_REGION(0x4000, "maincpu", 0)
708   ROM_FILL(0x0000, 0x4000, 0xff)
709
710   ROM_REGION(0x20000, "user1", 0)
711   ROM_SYSTEM_BIOS( 0, "akbioskr", "Samsung Gam*Boy II with Alex Kidd in Miracle World (1990)" )
712   ROMX_LOAD("akbioskr.rom", 0x000, 0x20000, CRC(9c5bad91) SHA1(2feafd8f1c40fdf1bd5668f8c5c02e5560945b17), ROM_BIOS(1))
713ROM_END
714
715ROM_START(gamegear)
716   ROM_REGION(0x4000, "maincpu", 0)
717   ROM_FILL(0x0000, 0x4000, 0x00)
718
719   ROM_REGION(0x0400, "user1", 0)
720   ROM_SYSTEM_BIOS( 0, "none", "No BIOS" ) /* gamegear */
721   ROM_SYSTEM_BIOS( 1, "majesco", "Majesco BIOS" ) /* gamg */
722   ROMX_LOAD("majbios.rom", 0x0000, 0x0400, CRC(0EBEA9D4) SHA1(914aa165e3d879f060be77870d345b60cfeb4ede), ROM_BIOS(2))
723ROM_END
724
725#define rom_gamegeaj rom_gamegear
726
727/***************************************************************************
728
729  Game driver(s)
730
731  US
732   - Sega Master System I (sms1)
733     - prototype bios - 1986
734     - without built-in game v1.3 - 1986
735     - built-in Hang On/Safari Hunt v2.4 - 1988
736     - built-in Hang On v3.4 - 1988
737     - built-in Missile Defense 3-D v4.4 - 1988
738     - built-in Hang On/Astro Warrior ????
739   - Sega Master System II (sms/sms2)
740     - built-in Alex Kidd in Miracle World - 1990
741
742  JP
743   - Sega SG-1000 Mark III (smsm3)
744     - no bios
745   - Sega Master System (I) (smsj)
746     - without built-in game v2.1 - 1987
747
748  KR
749   - Sega Master System II (sms2kr)
750     - built-in Alex Kidd in Miracle World (Korean)
751
752  EU
753   - Sega Master System I (sms1pal)
754     - without built-in game v1.3 - 1986
755     - built-in Hang On/Safari Hunt v2.4 - 1988
756     - built-in Hang On v3.4 - 1988
757     - built-in Missile Defense 3-D v4.4 - 1988
758     - built-in Hang On/Astro Warrior ????
759   - Sega Master System II (sms2pal)
760     - built-in Alex Kidd in Miracle World - 1990
761     - built-in Sonic the Hedgehog - 1991
762
763  BR
764   - Sega Master System I - 1987
765   - Sega Master System II???
766   - Sega Master System III - Tec Toy, 1987
767   - Sega Master System Compact - Tec Toy, 1992
768   - Sega Master System Girl - Tec Toy, 1992
769
770***************************************************************************/
771
772/*    YEAR  NAME        PARENT      COMPAT  MACHINE      INPUT   INIT      COMPANY     FULLNAME                            FLAGS */
773CONS( 1984, sg1000m3,   sms,        0,      sg1000m3,    smsj,   sg1000m3, "Sega",     "SG-1000 Mark III",                 0 )
774CONS( 1986, sms1,       sms,        0,      sms1_ntsc,   sms1,   sms1,     "Sega",     "Master System I",                  0 )
775CONS( 1986, sms1pal,    sms,        0,      sms1_pal,    sms1,   sms1,     "Sega",     "Master System I (PAL)" ,           0 )
776CONS( 1986, smssdisp,   sms,        0,      sms_sdisp,   sms,    smssdisp, "Sega",     "Master System Store Display Unit", GAME_NOT_WORKING )
777CONS( 1987, smsj,       sms,        0,      sms_fm,      smsj,   smsj,     "Sega",     "Master System (Japan)",            0 )
778CONS( 1990, sms,        0,          0,      sms2_ntsc,   sms,    sms1,     "Sega",     "Master System II",                 0 )
779CONS( 1990, smspal,     sms,        0,      sms2_pal,    sms,    sms1,     "Sega",     "Master System II (PAL)",           0 )
780CONS( 1990, sms2kr,     sms,        0,      sms2_fm,     sms,    sms2kr,   "Samsung",  "Gam*Boy II (Korea)",               0 )
781
782CONS( 1990, gamegear,   0,          sms,    gamegear,    gg,     gamegear, "Sega",     "Game Gear (Europe/America)",       0 )
783CONS( 1990, gamegeaj,   gamegear,   0,      gamegear,    gg,     gamegeaj, "Sega",     "Game Gear (Japan)",                0 )
src/mess/machine/segasms.c
r0r11573
1#include "emu.h"
2#include "crsshair.h"
3#include "image.h"
4#include "includes/segasms.h"
5#include "video/smsvdp.h"
6#include "sound/2413intf.h"
7#include "imagedev/cartslot.h"
8#include "hashfile.h"
9 
10#define VERBOSE 0
11#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
12 
13#define CF_CODEMASTERS_MAPPER    0x01
14#define CF_KOREAN_MAPPER         0x02
15#define CF_KOREAN_ZEMINA_MAPPER  0x04
16#define CF_KOREAN_NOBANK_MAPPER  0x08
17#define CF_93C46_EEPROM          0x10
18#define CF_ONCART_RAM            0x20
19#define CF_GG_SMS_MODE           0x40
20 
21#define LGUN_RADIUS           6
22#define LGUN_X_INTERVAL       4
23 
24 
25static void setup_rom(address_space *space);
26 
27 
28static TIMER_CALLBACK( rapid_fire_callback )
29{
30   sms_state *state = machine.driver_data<sms_state>();
31   state->m_rapid_fire_state_1 ^= 0xff;
32   state->m_rapid_fire_state_2 ^= 0xff;
33}
34 
35 
36static WRITE8_HANDLER( sms_input_write )
37{
38   sms_state *state = space->machine().driver_data<sms_state>();
39 
40   switch (offset)
41   {
42   case 0:
43      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) & 0x0f)
44      {
45      case 0x03:   /* Sports Pad */
46         if (data != state->m_sports_pad_last_data_1)
47         {
48            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
49 
50            state->m_sports_pad_last_data_1 = data;
51            if (cpu_cycles - state->m_last_sports_pad_time_1 > 512)
52            {
53               state->m_sports_pad_state_1 = 3;
54               state->m_sports_pad_1_x = input_port_read(space->machine(), "SPORT0");
55               state->m_sports_pad_1_y = input_port_read(space->machine(), "SPORT1");
56            }
57            state->m_last_sports_pad_time_1 = cpu_cycles;
58            state->m_sports_pad_state_1 = (state->m_sports_pad_state_1 + 1) & 3;
59         }
60         break;
61      }
62      break;
63 
64   case 1:
65      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) >> 4)
66      {
67      case 0x03:   /* Sports Pad */
68         if (data != state->m_sports_pad_last_data_2)
69         {
70            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
71 
72            state->m_sports_pad_last_data_2 = data;
73            if (cpu_cycles - state->m_last_sports_pad_time_2 > 2048)
74            {
75               state->m_sports_pad_state_2 = 3;
76               state->m_sports_pad_2_x = input_port_read(space->machine(), "SPORT2");
77               state->m_sports_pad_2_y = input_port_read(space->machine(), "SPORT3");
78            }
79            state->m_last_sports_pad_time_2 = cpu_cycles;
80            state->m_sports_pad_state_2 = (state->m_sports_pad_state_2 + 1) & 3;
81         }
82         break;
83      }
84      break;
85   }
86}
87 
88/* FIXME: this function is a hack for Light Phaser emulation. Theoretically
89   sms_vdp_hcount_latch() should be used instead, but it returns incorrect
90   position for unknown reason (timing?) */
91static void sms_vdp_hcount_lphaser( running_machine &machine, int hpos )
92{
93   sms_state *state = machine.driver_data<sms_state>();
94   int hpos_tmp = hpos + state->m_lphaser_x_offs;
95   UINT8 tmp = ((hpos_tmp - 46) >> 1) & 0xff;
96 
97   //printf ("sms_vdp_hcount_lphaser: hpos %3d hpos_tmp %3d => hcount %2X\n", hpos, hpos_tmp, tmp);
98   sms_vdp_hcount_latch_w(state->m_vdp, 0, tmp);
99}
100 
101 
102/*
103    Light Phaser (light gun) emulation notes:
104    - The sensor is activated based on color brightness of some individual
105      pixels being drawn by the beam, at circular area where the gun is aiming.
106    - Currently, brightness is calculated based only on single pixels.
107    - In general, after the trigger is pressed, games draw the next frame using
108      a light color pattern, to make sure sensor will be activated. If emulation
109      skips that frame, sensor may stay deactivated. Frameskip set to 0 (no skip) is
110      recommended to avoid problems.
111    - When sensor switches from off to on, a value is latched for HCount register
112      and a flag is set. The emulation uses the flag to avoid sequential latches and
113      to signal that TH line is activated when the status of the input port is read.
114      When the status is read, the flag is cleared, or else it is cleared later when
115      the Pause status is read (end of a frame). This is necessary because the
116      "Color & Switch Test" ROM only reads the TH bit after the VINT line.
117    - The gun test of "Color & Switch Test" is an example that requires checks
118      of sensor status independent of other events, like trigger press or TH bit
119      reads. Another example is the title screen of "Hang-On & Safari Hunt", where
120      the game only reads HCount register in a loop, expecting a latch by the gun.
121    - The whole procedure is managed by a timer callback, that always reschedule
122      itself to run in some intervals when the beam is at the circular area.
123*/
124static int lgun_bright_aim_area( running_machine &machine, emu_timer *timer, int lgun_x, int lgun_y )
125{
126   sms_state *state = machine.driver_data<sms_state>();
127   const int r_x_r = LGUN_RADIUS * LGUN_RADIUS;
128   screen_device *screen = machine.first_screen();
129   const rectangle &visarea = screen->visible_area();
130   int beam_x = screen->hpos();
131   int beam_y = screen->vpos();
132   int dx, dy;
133   int result = 0;
134   int pos_changed = 0;
135   double dx_circ;
136 
137    while (1)
138   {
139      dy = abs(beam_y - lgun_y);
140 
141      if (dy > LGUN_RADIUS || beam_y < visarea.min_y || beam_y > visarea.max_y)
142      {
143         beam_y = lgun_y - LGUN_RADIUS;
144         if (beam_y < visarea.min_y)
145            beam_y = visarea.min_y;
146         dy = abs(beam_y - lgun_y);
147         pos_changed = 1;
148      }
149      /* step 1: r^2 = dx^2 + dy^2 */
150      /* step 2: dx^2 = r^2 - dy^2 */
151      /* step 3: dx = sqrt(r^2 - dy^2) */
152      dx_circ = ceil((float) sqrt((float) (r_x_r - (dy * dy))));
153      dx = abs(beam_x - lgun_x);
154 
155      if (dx > dx_circ || beam_x < visarea.min_x || beam_x > visarea.max_x)
156      {
157         if (beam_x > lgun_x)
158         {
159            beam_x = 0;
160            beam_y++;
161            continue;
162         }
163         beam_x = lgun_x - dx_circ;
164         if (beam_x < visarea.min_x)
165            beam_x = visarea.min_x;
166         pos_changed = 1;
167      }
168 
169      if (!pos_changed)
170      {
171         result = sms_vdp_check_brightness(state->m_vdp, beam_x, beam_y);
172         /* next check at same line */
173         beam_x += LGUN_X_INTERVAL;
174         pos_changed = 1;
175      }
176      else
177         break;
178   }
179   timer->adjust(machine.first_screen()->time_until_pos(beam_y, beam_x));
180 
181   return result;
182}
183 
184static UINT8 sms_vdp_hcount( running_machine &machine )
185{
186   UINT8 tmp;
187   screen_device *screen = machine.first_screen();
188   int hpos = screen->hpos();
189 
190   /* alternative method: pass HCounter test, but some others fail */
191   //int hpos_tmp = hpos;
192   //if ((hpos + 2) % 6 == 0) hpos_tmp--;
193   //tmp = ((hpos_tmp - 46) >> 1) & 0xff;
194 
195   UINT64 calc_cycles;
196   attotime time_end;
197   int vpos = screen->vpos();
198   int max_hpos = screen->width() - 1;
199 
200   if (hpos == max_hpos)
201      time_end = attotime::zero;
202   else
203      time_end = screen->time_until_pos(vpos, max_hpos);
204   calc_cycles = machine.device<cpu_device>("maincpu")->attotime_to_clocks(time_end);
205 
206   /* equation got from SMSPower forum, posted by Flubba. */
207   tmp = ((590 - (calc_cycles * 3)) / 4) & 0xff;
208 
209   //printf ("sms_vdp_hcount: hpos %3d => hcount %2X\n", hpos, tmp);
210   return tmp;
211}
212 
213 
214static void sms_vdp_hcount_latch( running_machine &machine )
215{
216   sms_state *state = machine.driver_data<sms_state>();
217   UINT8 value = sms_vdp_hcount(machine);
218 
219   sms_vdp_hcount_latch_w(state->m_vdp, 0, value);
220}
221 
222 
223static UINT16 screen_hpos_nonscaled( screen_device *screen, int scaled_hpos )
224{
225   const rectangle &visarea = screen->visible_area();
226   int offset_x = (scaled_hpos * (visarea.max_x - visarea.min_x)) / 255;
227   return visarea.min_x + offset_x;
228}
229 
230 
231static UINT16 screen_vpos_nonscaled( screen_device *screen, int scaled_vpos )
232{
233   const rectangle &visarea = screen->visible_area();
234   int offset_y = (scaled_vpos * (visarea.max_y - visarea.min_y)) / 255;
235   return visarea.min_y + offset_y;
236}
237 
238 
239static void lphaser1_sensor_check( running_machine &machine )
240{
241   sms_state *state = machine.driver_data<sms_state>();
242   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER0"));
243   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER1"));
244 
245   if (lgun_bright_aim_area(machine, state->m_lphaser_1_timer, x, y))
246   {
247      if (state->m_lphaser_1_latch == 0)
248      {
249         state->m_lphaser_1_latch = 1;
250         sms_vdp_hcount_lphaser(machine, x);
251      }
252   }
253}
254 
255static void lphaser2_sensor_check( running_machine &machine )
256{
257   sms_state *state = machine.driver_data<sms_state>();
258   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER2"));
259   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER3"));
260 
261   if (lgun_bright_aim_area(machine, state->m_lphaser_2_timer, x, y))
262   {
263      if (state->m_lphaser_2_latch == 0)
264      {
265         state->m_lphaser_2_latch = 1;
266         sms_vdp_hcount_lphaser(machine, x);
267      }
268   }
269}
270 
271 
272// at each input port read we check if lightguns are enabled in one of the ports:
273// if so, we turn on crosshair and the lightgun timer
274static TIMER_CALLBACK( lightgun_tick )
275{
276   sms_state *state = machine.driver_data<sms_state>();
277 
278   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f) == 0x01)
279   {
280      /* enable crosshair */
281      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
282      if (!state->m_lphaser_1_timer->enabled())
283         lphaser1_sensor_check(machine);
284   }
285   else
286   {
287      /* disable crosshair */
288      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
289      state->m_lphaser_1_timer->enable(0);
290   }
291 
292   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0) == 0x10)
293   {
294      /* enable crosshair */
295      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_ALL);
296      if (!state->m_lphaser_2_timer->enabled())
297         lphaser2_sensor_check(machine);
298   }
299   else
300   {
301      /* disable crosshair */
302      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_NONE);
303      state->m_lphaser_2_timer->enable(0);
304   }
305}
306 
307 
308static TIMER_CALLBACK( lphaser_1_callback )
309{
310   lphaser1_sensor_check(machine);
311}
312 
313 
314static TIMER_CALLBACK( lphaser_2_callback )
315{
316   lphaser2_sensor_check(machine);
317}
318 
319 
320INPUT_CHANGED( lgun1_changed )
321{
322   sms_state *state = field.machine().driver_data<sms_state>();
323   if (!state->m_lphaser_1_timer ||
324      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0x0f) != 0x01)
325      return;
326 
327   if (newval != oldval)
328      lphaser1_sensor_check(field.machine());
329}
330 
331INPUT_CHANGED( lgun2_changed )
332{
333   sms_state *state = field.machine().driver_data<sms_state>();
334   if (!state->m_lphaser_2_timer ||
335      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0xf0) != 0x10)
336      return;
337 
338   if (newval != oldval)
339      lphaser2_sensor_check(field.machine());
340}
341 
342 
343static void sms_get_inputs( address_space *space )
344{
345   sms_state *state = space->machine().driver_data<sms_state>();
346   UINT8 data = 0x00;
347   UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
348   running_machine &machine = space->machine();
349 
350   state->m_input_port0 = 0xff;
351   state->m_input_port1 = 0xff;
352 
353   if (cpu_cycles - state->m_last_paddle_read_time > 256)
354   {
355      state->m_paddle_read_state ^= 0xff;
356      state->m_last_paddle_read_time = cpu_cycles;
357   }
358 
359   /* Check if lightgun has been chosen as input: if so, enable crosshair */
360   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
361 
362   /* Player 1 */
363   switch (input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f)
364   {
365   case 0x00:  /* Joystick */
366      data = input_port_read(machine, "PORT_DC");
367      /* Check Rapid Fire setting for Button A */
368      if (!(data & 0x10) && (input_port_read(machine, "RFU") & 0x01))
369         data |= state->m_rapid_fire_state_1 & 0x10;
370 
371      /* Check Rapid Fire setting for Button B */
372      if (!(data & 0x20) && (input_port_read(machine, "RFU") & 0x02))
373         data |= state->m_rapid_fire_state_1 & 0x20;
374 
375      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
376      break;
377 
378   case 0x01:  /* Light Phaser */
379      data = (input_port_read(machine, "CTRLIPT") & 0x01) << 4;
380      if (!(data & 0x10))
381      {
382         if (input_port_read(machine, "RFU") & 0x01)
383            data |= state->m_rapid_fire_state_1 & 0x10;
384      }
385      /* just consider the button (trigger) bit */
386      data |= ~0x10;
387      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
388      break;
389 
390   case 0x02:  /* Paddle Control */
391      /* Get button A state */
392      data = input_port_read(machine, "PADDLE0");
393 
394      if (state->m_paddle_read_state)
395         data = data >> 4;
396 
397      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x0f) | (state->m_paddle_read_state & 0x20)
398                      | ((input_port_read(machine, "CTRLIPT") & 0x02) << 3);
399      break;
400 
401   case 0x03:   /* Sega Sports Pad */
402      switch (state->m_sports_pad_state_1)
403      {
404      case 0:
405         data = (state->m_sports_pad_1_x >> 4) & 0x0f;
406         break;
407      case 1:
408         data = state->m_sports_pad_1_x & 0x0f;
409         break;
410      case 2:
411         data = (state->m_sports_pad_1_y >> 4) & 0x0f;
412         break;
413      case 3:
414         data = state->m_sports_pad_1_y & 0x0f;
415         break;
416      }
417      state->m_input_port0 = (state->m_input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
418      break;
419   }
420 
421   /* Player 2 */
422   switch (input_port_read_safe(machine, "CTRLSEL", 0x00)  & 0xf0)
423   {
424   case 0x00:   /* Joystick */
425      data = input_port_read(machine, "PORT_DC");
426      state->m_input_port0 = (state->m_input_port0 & 0x3f) | (data & 0xc0);
427 
428      data = input_port_read(machine, "PORT_DD");
429      /* Check Rapid Fire setting for Button A */
430      if (!(data & 0x04) && (input_port_read(machine, "RFU") & 0x04))
431         data |= state->m_rapid_fire_state_2 & 0x04;
432 
433      /* Check Rapid Fire setting for Button B */
434      if (!(data & 0x08) && (input_port_read(machine, "RFU") & 0x08))
435         data |= state->m_rapid_fire_state_2 & 0x08;
436 
437      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
438      break;
439 
440   case 0x10:   /* Light Phaser */
441      data = (input_port_read(machine, "CTRLIPT") & 0x10) >> 2;
442      if (!(data & 0x04))
443      {
444         if (input_port_read(machine, "RFU") & 0x04)
445            data |= state->m_rapid_fire_state_2 & 0x04;
446      }
447      /* just consider the button (trigger) bit */
448      data |= ~0x04;
449      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
450      break;
451 
452   case 0x20:   /* Paddle Control */
453      /* Get button A state */
454      data = input_port_read(machine, "PADDLE1");
455      if (state->m_paddle_read_state)
456         data = data >> 4;
457 
458      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
459      state->m_input_port1 = (state->m_input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (state->m_paddle_read_state & 0x08)
460                      | ((input_port_read(machine, "CTRLIPT") & 0x20) >> 3);
461      break;
462 
463   case 0x30:   /* Sega Sports Pad */
464      switch (state->m_sports_pad_state_2)
465      {
466      case 0:
467         data = state->m_sports_pad_2_x & 0x0f;
468         break;
469      case 1:
470         data = (state->m_sports_pad_2_x >> 4) & 0x0f;
471         break;
472      case 2:
473         data = state->m_sports_pad_2_y & 0x0f;
474         break;
475      case 3:
476         data = (state->m_sports_pad_2_y >> 4) & 0x0f;
477         break;
478      }
479      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
480      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
481      break;
482   }
483}
484 
485 
486WRITE8_HANDLER( sms_fm_detect_w )
487{
488   sms_state *state = space->machine().driver_data<sms_state>();
489 
490   if (state->m_has_fm)
491      state->m_fm_detect = (data & 0x01);
492}
493 
494 
495READ8_HANDLER( sms_fm_detect_r )
496{
497   sms_state *state = space->machine().driver_data<sms_state>();
498 
499   if (state->m_has_fm)
500   {
501      return state->m_fm_detect;
502   }
503   else
504   {
505      if (state->m_bios_port & IO_CHIP)
506      {
507         return 0xff;
508      }
509      else
510      {
511         sms_get_inputs(space);
512         return state->m_input_port0;
513      }
514   }
515}
516 
517WRITE8_HANDLER( sms_io_control_w )
518{
519   sms_state *state = space->machine().driver_data<sms_state>();
520 
521   if (data & 0x08)
522   {
523      /* check if TH pin level is high (1) and was low last time */
524      if (data & 0x80 && !(state->m_ctrl_reg & 0x80))
525      {
526         sms_vdp_hcount_latch(space->machine());
527      }
528      sms_input_write(space, 0, (data & 0x20) >> 5);
529   }
530 
531   if (data & 0x02)
532   {
533      if (data & 0x20 && !(state->m_ctrl_reg & 0x20))
534      {
535         sms_vdp_hcount_latch(space->machine());
536      }
537      sms_input_write(space, 1, (data & 0x80) >> 7);
538   }
539 
540   state->m_ctrl_reg = data;
541}
542 
543 
544READ8_HANDLER( sms_count_r )
545{
546   sms_state *state = space->machine().driver_data<sms_state>();
547 
548   if (offset & 0x01)
549      return sms_vdp_hcount_latch_r(state->m_vdp, offset);
550   else
551      return sms_vdp_vcount_r(state->m_vdp, offset);
552}
553 
554 
555/*
556 Check if the pause button is pressed.
557 If the gamegear is in sms mode, check if the start button is pressed.
558 */
559void sms_pause_callback( running_machine &machine )
560{
561   sms_state *state = machine.driver_data<sms_state>();
562 
563   if (state->m_is_gamegear && !(state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE))
564      return;
565 
566   if (!(input_port_read(machine, state->m_is_gamegear ? "START" : "PAUSE") & 0x80))
567   {
568      if (!state->m_paused)
569      {
570         cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE);
571      }
572      state->m_paused = 1;
573   }
574   else
575   {
576      state->m_paused = 0;
577   }
578 
579   /* clear Light Phaser latch flags for next frame */
580   state->m_lphaser_1_latch = 0;
581   state->m_lphaser_2_latch = 0;
582}
583 
584READ8_HANDLER( sms_input_port_0_r )
585{
586   sms_state *state = space->machine().driver_data<sms_state>();
587 
588   if (state->m_bios_port & IO_CHIP)
589   {
590      return 0xff;
591   }
592   else
593   {
594      sms_get_inputs(space);
595      return state->m_input_port0;
596   }
597}
598 
599 
600READ8_HANDLER( sms_input_port_1_r )
601{
602   sms_state *state = space->machine().driver_data<sms_state>();
603 
604   if (state->m_bios_port & IO_CHIP)
605      return 0xff;
606 
607   sms_get_inputs(space);
608 
609   /* Reset Button */
610   state->m_input_port1 = (state->m_input_port1 & 0xef) | (input_port_read_safe(space->machine(), "RESET", 0x01) & 0x01) << 4;
611 
612   /* Do region detection if TH of ports A and B are set to output (0) */
613   if (!(state->m_ctrl_reg & 0x0a))
614   {
615      /* Move bits 7,5 of IO control port into bits 7, 6 */
616      state->m_input_port1 = (state->m_input_port1 & 0x3f) | (state->m_ctrl_reg & 0x80) | (state->m_ctrl_reg & 0x20) << 1;
617 
618      /* Inverse region detect value for Japanese machines */
619      if (state->m_is_region_japan)
620         state->m_input_port1 ^= 0xc0;
621   }
622   else
623   {
624      if (state->m_ctrl_reg & 0x02 && state->m_lphaser_1_latch)
625      {
626         state->m_input_port1 &= ~0x40;
627         state->m_lphaser_1_latch = 0;
628      }
629 
630      if (state->m_ctrl_reg & 0x08 && state->m_lphaser_2_latch)
631      {
632         state->m_input_port1 &= ~0x80;
633         state->m_lphaser_2_latch = 0;
634      }
635   }
636 
637   return state->m_input_port1;
638}
639 
640 
641 
642WRITE8_HANDLER( sms_ym2413_register_port_0_w )
643{
644   sms_state *state = space->machine().driver_data<sms_state>();
645 
646   if (state->m_has_fm)
647      ym2413_w(state->m_ym, 0, (data & 0x3f));
648}
649 
650 
651WRITE8_HANDLER( sms_ym2413_data_port_0_w )
652{
653   sms_state *state = space->machine().driver_data<sms_state>();
654 
655   if (state->m_has_fm)
656   {
657      logerror("data_port_0_w %x %x\n", offset, data);
658      ym2413_w(state->m_ym, 1, data);
659   }
660}
661 
662 
663READ8_HANDLER( gg_input_port_2_r )
664{
665   sms_state *state = space->machine().driver_data<sms_state>();
666 
667   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
668   return ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine(), "START") & 0x80));
669}
670 
671 
672READ8_HANDLER( sms_sscope_r )
673{
674   sms_state *state = space->machine().driver_data<sms_state>();
675   return state->m_sscope_state;
676}
677 
678 
679WRITE8_HANDLER( sms_sscope_w )
680{
681   sms_state *state = space->machine().driver_data<sms_state>();
682   state->m_sscope_state = data;
683}
684 
685 
686READ8_HANDLER( sms_mapper_r )
687{
688   sms_state *state = space->machine().driver_data<sms_state>();
689   return state->m_mapper[offset];
690}
691 
692/* Terebi Oekaki */
693/* The following code comes from sg1000.c. We should eventually merge these TV Draw implementations */
694static WRITE8_HANDLER( sms_tvdraw_axis_w )
695{
696   sms_state *state = space->machine().driver_data<sms_state>();
697   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
698 
699   if (data & 0x01)
700   {
701      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_X") : 0x80;
702 
703      if (state->m_tvdraw_data < 4) state->m_tvdraw_data = 4;
704      if (state->m_tvdraw_data > 251) state->m_tvdraw_data = 251;
705   }
706   else
707   {
708      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_Y") + 0x20 : 0x80;
709   }
710}
711 
712static READ8_HANDLER( sms_tvdraw_status_r )
713{
714   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
715   return tvboard_on ? input_port_read(space->machine(), "TVDRAW_PEN") : 0x01;
716}
717 
718static READ8_HANDLER( sms_tvdraw_data_r )
719{
720   sms_state *state = space->machine().driver_data<sms_state>();
721   return state->m_tvdraw_data;
722}
723 
724 
725WRITE8_HANDLER( sms_mapper_w )
726{
727   sms_state *state = space->machine().driver_data<sms_state>();
728   int page;
729   UINT8 *SOURCE_BIOS;
730   UINT8 *SOURCE_CART;
731   UINT8 *SOURCE;
732   UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
733 
734   offset &= 3;
735 
736   state->m_mapper[offset] = data;
737   state->m_mapper_ram[offset] = data;
738 
739   if (state->m_cartridge[state->m_current_cartridge].ROM)
740   {
741      SOURCE_CART = state->m_cartridge[state->m_current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
742   }
743   else
744   {
745      SOURCE_CART = state->m_banking_none[1];
746   }
747 
748   if (state->m_BIOS)
749   {
750      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
751   }
752   else
753   {
754      SOURCE_BIOS = state->m_banking_none[1];
755   }
756 
757   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
758   {
759      if (!(state->m_bios_port & IO_CARTRIDGE) || (state->m_is_gamegear && state->m_BIOS == NULL))
760      {
761         page = (rom_page_count > 0) ? data % rom_page_count : 0;
762         if (!state->m_cartridge[state->m_current_cartridge].ROM)
763            return;
764         SOURCE = SOURCE_CART;
765      }
766      else
767      {
768         /* nothing to page in */
769         return;
770      }
771   }
772   else
773   {
774      page = (state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0;
775      if (!state->m_BIOS)
776         return;
777      SOURCE = SOURCE_BIOS;
778   }
779 
780   switch (offset)
781   {
782   case 0: /* Control */
783      /* Is it ram or rom? */
784      if (data & 0x08) /* it's ram */
785      {
786         state->m_cartridge[state->m_current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
787         if (data & 0x04)
788         {
789            LOG(("ram 1 paged.\n"));
790            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM + 0x4000;
791         }
792         else
793         {
794            LOG(("ram 0 paged.\n"));
795            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM;
796         }
797         memory_set_bankptr(space->machine(),  "bank5", SOURCE);
798         memory_set_bankptr(space->machine(),  "bank6", SOURCE + 0x2000);
799      }
800      else /* it's rom */
801      {
802         if (state->m_bios_port & IO_BIOS_ROM || ! state->m_has_bios)
803         {
804            page = (rom_page_count > 0) ? state->m_mapper[3] % rom_page_count : 0;
805            SOURCE = state->m_banking_cart[5];
806         }
807         else
808         {
809            page = (state->m_bios_page_count > 0) ? state->m_mapper[3] % state->m_bios_page_count : 0;
810            SOURCE = state->m_banking_bios[5];
811         }
812         LOG(("rom 2 paged in %x.\n", page));
813         memory_set_bankptr(space->machine(), "bank5", SOURCE);
814         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
815      }
816      break;
817 
818   case 1: /* Select 16k ROM bank for 0400-3FFF */
819      LOG(("rom 0 paged in %x.\n", page));
820      state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
821      state->m_banking_cart[2] = SOURCE_CART + 0x0400;
822      if (state->m_is_gamegear)
823         SOURCE = SOURCE_CART;
824 
825      memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
826      break;
827 
828   case 2: /* Select 16k ROM bank for 4000-7FFF */
829      LOG(("rom 1 paged in %x.\n", page));
830      state->m_banking_bios[3] = SOURCE_BIOS;
831      state->m_banking_cart[3] = SOURCE_CART;
832      if (state->m_is_gamegear)
833         SOURCE = SOURCE_CART;
834 
835      memory_set_bankptr(space->machine(), "bank3", SOURCE);
836      memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
837      break;
838 
839   case 3: /* Select 16k ROM bank for 8000-BFFF */
840      state->m_banking_bios[5] = SOURCE_BIOS;
841      if (state->m_is_gamegear)
842         SOURCE = SOURCE_CART;
843 
844      if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
845      {
846         if (SOURCE == SOURCE_CART)
847         {
848            SOURCE = state->m_banking_cart[5];
849         }
850      }
851      else
852      {
853         state->m_banking_cart[5] = SOURCE_CART;
854      }
855 
856      if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
857      {
858         LOG(("rom 2 paged in %x.\n", page));
859         memory_set_bankptr(space->machine(), "bank5", SOURCE);
860         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
861      }
862      break;
863   }
864}
865 
866#ifdef MESS
867static WRITE8_HANDLER( sms_korean_zemina_banksw_w )
868{
869   sms_state *state = space->machine().driver_data<sms_state>();
870 
871   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
872   {
873      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x2000;
874      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
875 
876      if (!state->m_cartridge[state->m_current_cartridge].ROM)
877         return;
878 
879      switch (offset & 3)
880      {
881         case 0:
882            state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
883            memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
884            break;
885         case 1:
886            state->m_banking_cart[6] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
887            memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[6]);
888            break;
889         case 2:
890            state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
891            memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
892            break;
893         case 3:
894            state->m_banking_cart[4] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
895            memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[4]);
896            break;
897      }
898      LOG(("Zemina mapper write: offset %x data %x.\n", offset, page));
899   }
900}
901 
902static WRITE8_HANDLER( sms_codemasters_page0_w )
903{
904   sms_state *state = space->machine().driver_data<sms_state>();
905 
906   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
907   {
908      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
909      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
910      state->m_banking_cart[2] = state->m_banking_cart[1] + 0x0400;
911      memory_set_bankptr(space->machine(), "bank1", state->m_banking_cart[1]);
912      memory_set_bankptr(space->machine(), "bank2", state->m_banking_cart[2]);
913   }
914}
915 
916 
917static WRITE8_HANDLER( sms_codemasters_page1_w )
918{
919   sms_state *state = space->machine().driver_data<sms_state>();
920 
921   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
922   {
923      /* Check if we need to switch in some RAM */
924      if (data & 0x80)
925      {
926         state->m_cartridge[state->m_current_cartridge].ram_page = data & 0x07;
927         memory_set_bankptr(space->machine(), "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM + state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000);
928      }
929      else
930      {
931         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
932         state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
933         memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
934         memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[3] + 0x2000);
935         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
936      }
937   }
938}
939#endif
940 
941static READ8_HANDLER( sms_kor_nobank_r )
942{
943//  printf("read %x\n", offset);
944   return space->read_byte(0xdffc + offset);
945}
946 
947static WRITE8_HANDLER( sms_kor_nobank_w )
948{
949   sms_state *state = space->machine().driver_data<sms_state>();
950   UINT8 *SOURCE_BIOS;
951   UINT8 *SOURCE = NULL;
952 
953   space->write_byte(0xdffc + offset, data);
954   state->m_mapper[offset] = data;
955 
956   // FIXME: the code below (which only handles the BIOS bankswitch at start) should be cleaned up
957   // currently, it's just a stripped down version of sms_mapper_w
958   if (state->m_BIOS)
959      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
960   else
961      SOURCE_BIOS = state->m_banking_none[1];
962 
963   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
964      return;
965   else
966   {
967      if (!state->m_BIOS)
968         return;
969      SOURCE = SOURCE_BIOS;
970   }
971 
972//  printf("write %x, %x\n", offset, data);
973 
974   switch (offset)
975   {
976      case 0: /* Control */
977         if (!(data & 0x08)) /* it's not ram */
978         {
979            if (!(state->m_bios_port & IO_BIOS_ROM) && state->m_has_bios)
980            {
981               SOURCE = state->m_banking_bios[5];
982               memory_set_bankptr(space->machine(), "bank5", SOURCE);
983               memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
984            }
985         }
986         break;
987 
988      case 1: /* Select 16k ROM bank for 0400-3FFF */
989         state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
990         memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
991         break;
992 
993      case 2: /* Select 16k ROM bank for 4000-7FFF */
994         state->m_banking_bios[3] = SOURCE_BIOS;
995         memory_set_bankptr(space->machine(), "bank3", SOURCE);
996         memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
997         break;
998 
999      case 3: /* Select 16k ROM bank for 8000-BFFF */
1000         state->m_banking_bios[5] = SOURCE_BIOS;
1001         if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
1002         {
1003            memory_set_bankptr(space->machine(), "bank5", SOURCE);
1004            memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
1005         }
1006         break;
1007   }
1008}
1009 
1010WRITE8_HANDLER( sms_bios_w )
1011{
1012   sms_state *state = space->machine().driver_data<sms_state>();
1013   state->m_bios_port = data;
1014 
1015   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(&space->device()));
1016 
1017   setup_rom(space);
1018}
1019 
1020 
1021WRITE8_HANDLER( sms_cartram2_w )
1022{
1023   sms_state *state = space->machine().driver_data<sms_state>();
1024 
1025   if (state->m_mapper[0] & 0x08)
1026   {
1027      logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000);
1028      if (state->m_mapper[0] & 0x04)
1029      {
1030         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x6000] = data;
1031      }
1032      else
1033      {
1034         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x2000] = data;
1035      }
1036   }
1037 
1038   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1039   {
1040      state->m_cartridge[state->m_current_cartridge].cartRAM[state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000 + offset] = data;
1041   }
1042 
1043   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
1044   {
1045      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1046      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
1047 
1048      if (!state->m_cartridge[state->m_current_cartridge].ROM)
1049         return;
1050 
1051      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1052      memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1053      memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1054      LOG(("rom 2 paged in %x (Korean mapper).\n", page));
1055   }
1056}
1057 
1058 
1059WRITE8_HANDLER( sms_cartram_w )
1060{
1061   sms_state *state = space->machine().driver_data<sms_state>();
1062   int page;
1063 
1064   if (state->m_mapper[0] & 0x08)
1065   {
1066      logerror("write %02X to cartram at offset #%04X\n", data, offset);
1067      if (state->m_mapper[0] & 0x04)
1068      {
1069         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x4000] = data;
1070      }
1071      else
1072      {
1073         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset] = data;
1074      }
1075   }
1076   else
1077   {
1078      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
1079      {
1080         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1081         page = (rom_page_count > 0) ? data % rom_page_count : 0;
1082         if (!state->m_cartridge[state->m_current_cartridge].ROM)
1083            return;
1084         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1085         memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1086         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1087         LOG(("rom 2 paged in %x (Codemasters mapper).\n", page));
1088      }
1089      else if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1090      {
1091         state->m_cartridge[state->m_current_cartridge].cartRAM[offset & (state->m_cartridge[state->m_current_cartridge].ram_size - 1)] = data;
1092      }
1093      else
1094      {
1095         logerror("INVALID write %02X to cartram at offset #%04X\n", data, offset);
1096      }
1097   }
1098}
1099 
1100 
1101WRITE8_HANDLER( gg_sio_w )
1102{
1103   sms_state *state = space->machine().driver_data<sms_state>();
1104   logerror("*** write %02X to SIO register #%d\n", data, offset);
1105 
1106   state->m_gg_sio[offset & 0x07] = data;
1107   switch (offset & 7)
1108   {
1109      case 0x00: /* Parallel Data */
1110         break;
1111 
1112      case 0x01: /* Data Direction/ NMI Enable */
1113         break;
1114 
1115      case 0x02: /* Serial Output */
1116         break;
1117 
1118      case 0x03: /* Serial Input */
1119         break;
1120 
1121      case 0x04: /* Serial Control / Status */
1122         break;
1123   }
1124}
1125 
1126 
1127READ8_HANDLER( gg_sio_r )
1128{
1129   sms_state *state = space->machine().driver_data<sms_state>();
1130   logerror("*** read SIO register #%d\n", offset);
1131 
1132   switch (offset & 7)
1133   {
1134      case 0x00: /* Parallel Data */
1135         break;
1136 
1137      case 0x01: /* Data Direction/ NMI Enable */
1138         break;
1139 
1140      case 0x02: /* Serial Output */
1141         break;
1142 
1143      case 0x03: /* Serial Input */
1144         break;
1145 
1146      case 0x04: /* Serial Control / Status */
1147         break;
1148   }
1149 
1150   return state->m_gg_sio[offset];
1151}
1152 
1153static void sms_machine_stop( running_machine &machine )
1154{
1155   sms_state *state = machine.driver_data<sms_state>();
1156 
1157   /* Does the cartridge have SRAM that should be saved? */
1158   if (state->m_cartridge[state->m_current_cartridge].sram_save) {
1159      device_image_interface *image = dynamic_cast<device_image_interface *>(machine.device("cart1"));
1160      image->battery_save(state->m_cartridge[state->m_current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
1161   }
1162}
1163 
1164 
1165static void setup_rom( address_space *space )
1166{
1167   sms_state *state = space->machine().driver_data<sms_state>();
1168   running_machine &machine = space->machine();
1169 
1170   /* 1. set up bank pointers to point to nothing */
1171   memory_set_bankptr(machine, "bank1", state->m_banking_none[1]);
1172   memory_set_bankptr(machine, "bank2", state->m_banking_none[2]);
1173   memory_set_bankptr(machine, "bank3", state->m_banking_none[3]);
1174   memory_set_bankptr(machine, "bank4", state->m_banking_none[3] + 0x2000);
1175   memory_set_bankptr(machine, "bank5", state->m_banking_none[5]);
1176   memory_set_bankptr(machine, "bank6", state->m_banking_none[5] + 0x2000);
1177 
1178   /* 2. check and set up expansion port */
1179   if (!(state->m_bios_port & IO_EXPANSION) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_CARD))
1180   {
1181      /* TODO: Implement me */
1182      logerror("Switching to unsupported expansion port.\n");
1183   }
1184 
1185   /* 3. check and set up card rom */
1186   if (!(state->m_bios_port & IO_CARD) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_EXPANSION))
1187   {
1188      /* TODO: Implement me */
1189      logerror("Switching to unsupported card rom port.\n");
1190   }
1191 
1192   /* 4. check and set up cartridge rom */
1193   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || state->m_is_gamegear) { */
1194   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
1195   if ((!(state->m_bios_port & IO_CARTRIDGE)) || state->m_is_gamegear)
1196   {
1197      memory_set_bankptr(machine, "bank1", state->m_banking_cart[1]);
1198      memory_set_bankptr(machine, "bank2", state->m_banking_cart[2]);
1199      memory_set_bankptr(machine, "bank3", state->m_banking_cart[3]);
1200      memory_set_bankptr(machine, "bank4", state->m_banking_cart[3] + 0x2000);
1201      memory_set_bankptr(machine, "bank5", state->m_banking_cart[5]);
1202      memory_set_bankptr(machine, "bank6", state->m_banking_cart[5] + 0x2000);
1203      logerror("Switched in cartridge rom.\n");
1204   }
1205 
1206   /* 5. check and set up bios rom */
1207   if (!(state->m_bios_port & IO_BIOS_ROM))
1208   {
1209      /* 0x0400 bioses */
1210      if (state->m_has_bios_0400)
1211      {
1212         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1213         logerror("Switched in 0x0400 bios.\n");
1214      }
1215      /* 0x2000 bioses */
1216      if (state->m_has_bios_2000)
1217      {
1218         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1219         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1220         logerror("Switched in 0x2000 bios.\n");
1221      }
1222      if (state->m_has_bios_full)
1223      {
1224         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1225         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1226         memory_set_bankptr(machine, "bank3", state->m_banking_bios[3]);
1227         memory_set_bankptr(machine, "bank4", state->m_banking_bios[3] + 0x2000);
1228         memory_set_bankptr(machine, "bank5", state->m_banking_bios[5]);
1229         memory_set_bankptr(machine, "bank6", state->m_banking_bios[5] + 0x2000);
1230         logerror("Switched in full bios.\n");
1231      }
1232   }
1233 
1234   if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1235   {
1236      memory_set_bankptr(machine, "bank5", state->m_cartridge[state->m_current_cartridge].cartRAM);
1237      memory_set_bankptr(machine, "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM);
1238   }
1239}
1240 
1241 
1242static int sms_verify_cart( UINT8 *magic, int size )
1243{
1244   int retval;
1245 
1246   retval = IMAGE_VERIFY_FAIL;
1247 
1248   /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */
1249   if (size >= 0x8000)
1250   {
1251      if (!strncmp((char*)&magic[0x7ff0], "TMR SEGA", 8))
1252      {
1253#if 0
1254         /* Technically, it should be this, but remove for now until verified: */
1255         if (!strcmp(sysname, "gamegear"))
1256         {
1257            if ((unsigned char)magic[0x7ffd] < 0x50)
1258               retval = IMAGE_VERIFY_PASS;
1259         }
1260         if (!strcmp(sysname, "sms"))
1261         {
1262            if ((unsigned char)magic[0x7ffd] >= 0x50)
1263               retval = IMAGE_VERIFY_PASS;
1264         }
1265#endif
1266         retval = IMAGE_VERIFY_PASS;
1267      }
1268 
1269#if 0
1270      /* Check at $81f0 also */
1271      if (!retval)
1272      {
1273         if (!strncmp(&magic[0x81f0], "TMR SEGA", 8))
1274         {
1275#if 0
1276            /* Technically, it should be this, but remove for now until verified: */
1277            if (!strcmp(sysname, "gamegear"))
1278            {
1279               if ((unsigned char)magic[0x81fd] < 0x50)
1280                  retval = IMAGE_VERIFY_PASS;
1281            }
1282            if (!strcmp(sysname, "sms"))
1283            {
1284               if ((unsigned char)magic[0x81fd] >= 0x50)
1285                  retval = IMAGE_VERIFY_PASS;
1286            }
1287#endif
1288            retval = IMAGE_VERIFY_PASS;
1289         }
1290      }
1291#endif
1292 
1293   }
1294 
1295   return retval;
1296}
1297 
1298#ifdef UNUSED_FUNCTION
1299// For the moment we switch to a different detection routine which allows to detect
1300// in a single run Codemasters mapper, Korean mapper (including Jang Pung 3 which
1301// uses a diff signature then the one below here) and Zemina mapper (used by Wonsiin, etc.).
1302// I leave these here to document alt. detection routines and in the case these functions
1303// can be updated
1304 
1305/* Check for Codemasters mapper
1306  0x7FE3 - 93 - sms Cosmis Spacehead
1307              - sms Dinobasher
1308              - sms The Excellent Dizzy Collection
1309              - sms Fantastic Dizzy
1310              - sms Micro Machines
1311              - gamegear Cosmic Spacehead
1312              - gamegear Micro Machines
1313         - 94 - gamegear Dropzone
1314              - gamegear Ernie Els Golf (also has 64KB additional RAM on the cartridge)
1315              - gamegear Pete Sampras Tennis
1316              - gamegear S.S. Lucifer
1317         - 95 - gamegear Micro Machines 2 - Turbo Tournament
1318 
1319The Korean game Jang Pung II also seems to use a codemasters style mapper.
1320 */
1321static int detect_codemasters_mapper( UINT8 *rom )
1322{
1323   static const UINT8 jang_pung2[16] = { 0x00, 0xba, 0x38, 0x0d, 0x00, 0xb8, 0x38, 0x0c, 0x00, 0xb6, 0x38, 0x0b, 0x00, 0xb4, 0x38, 0x0a };
1324 
1325   if (((rom[0x7fe0] & 0x0f ) <= 9) && (rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95) &&  rom[0x7fef] == 0x00)
1326      return 1;
1327 
1328   if (!memcmp(&rom[0x7ff0], jang_pung2, 16))
1329      return 1;
1330 
1331   return 0;
1332}
1333 
1334 
1335static int detect_korean_mapper( UINT8 *rom )
1336{
1337   static const UINT8 signatures[2][16] =
1338   {
1339      { 0x3e, 0x11, 0x32, 0x00, 0xa0, 0x78, 0xcd, 0x84, 0x85, 0x3e, 0x02, 0x32, 0x00, 0xa0, 0xc9, 0xff }, /* Dodgeball King */
1340      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
1341   };
1342   int i;
1343 
1344   for (i = 0; i < 2; i++)
1345   {
1346      if (!memcmp(&rom[0x7ff0], signatures[i], 16))
1347      {
1348         return 1;
1349      }
1350   }
1351   return 0;
1352}
1353#endif
1354 
1355 
1356static int detect_tvdraw( UINT8 *rom )
1357{
1358   static const UINT8 terebi_oekaki[7] = { 0x61, 0x6e, 0x6e, 0x61, 0x6b, 0x6d, 0x6e };   // "annakmn"
1359 
1360   if (!memcmp(&rom[0x13b3], terebi_oekaki, 7))
1361      return 1;
1362 
1363   return 0;
1364}
1365 
1366 
1367static int detect_lphaser_xoffset( running_machine &machine, UINT8 *rom )
1368{
1369   sms_state *state = machine.driver_data<sms_state>();
1370 
1371   static const UINT8 signatures[6][16] =
1372   {
1373      /* Spacegun */
1374      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9d, 0x99, 0x10, 0x90, 0x00, 0x40 },
1375      /* Gangster Town */
1376      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x19, 0x87, 0x1b, 0xc9, 0x74, 0x50, 0x00, 0x4f },
1377      /* Shooting Gallery */
1378      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0x8a, 0x3a, 0x72, 0x50, 0x00, 0x4f },
1379      /* Rescue Mission */
1380      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0xfb, 0xd3, 0x06, 0x51, 0x00, 0x4f },
1381      /* Laser Ghost */
1382      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x00, 0x00, 0xb7, 0x55, 0x74, 0x70, 0x00, 0x40 },
1383      /* Assault City */
1384      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9f, 0x74, 0x34, 0x70, 0x00, 0x40 },
1385   };
1386 
1387   if (!(state->m_bios_port & IO_CARTRIDGE) && state->m_cartridge[state->m_current_cartridge].size >= 0x8000)
1388   {
1389      if (!memcmp(&rom[0x7ff0], signatures[0], 16) || !memcmp(&rom[0x7ff0], signatures[1], 16))
1390         return 40;
1391 
1392      if (!memcmp(&rom[0x7ff0], signatures[2], 16))
1393         return 49;
1394 
1395      if (!memcmp(&rom[0x7ff0], signatures[3], 16))
1396         return 47;
1397 
1398      if (!memcmp(&rom[0x7ff0], signatures[4], 16))
1399         return 44;
1400 
1401      if (!memcmp(&rom[0x7ff0], signatures[5], 16))
1402         return 53;
1403 
1404   }
1405   return 50;
1406}
1407 
1408 
1409DEVICE_START( sms_cart )
1410{
1411   sms_state *state = device->machine().driver_data<sms_state>();
1412   int i;
1413 
1414   for (i = 0; i < MAX_CARTRIDGES; i++)
1415   {
1416      state->m_cartridge[i].ROM = NULL;
1417      state->m_cartridge[i].size = 0;
1418      state->m_cartridge[i].features = 0;
1419      state->m_cartridge[i].cartSRAM = NULL;
1420      state->m_cartridge[i].sram_save = 0;
1421      state->m_cartridge[i].cartRAM = NULL;
1422      state->m_cartridge[i].ram_size = 0;
1423      state->m_cartridge[i].ram_page = 0;
1424   }
1425   state->m_current_cartridge = 0;
1426 
1427   state->m_bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1428   if (!state->m_is_gamegear && !state->m_has_bios)
1429   {
1430      state->m_bios_port &= ~(IO_CARTRIDGE);
1431      state->m_bios_port |= IO_BIOS_ROM;
1432   }
1433}
1434 
1435 
1436DEVICE_IMAGE_LOAD( sms_cart )
1437{
1438   running_machine &machine = image.device().machine();
1439   sms_state *state = machine.driver_data<sms_state>();
1440   int size, index = 0, offset = 0;
1441   const char *extrainfo = NULL;
1442 
1443   if (strcmp(image.device().tag(), "cart1") == 0)
1444      index = 0;
1445   if (strcmp(image.device().tag(), "cart2") == 0)
1446      index = 1;
1447   if (strcmp(image.device().tag(), "cart3") == 0)
1448      index = 2;
1449   if (strcmp(image.device().tag(), "cart4") == 0)
1450      index = 3;
1451   if (strcmp(image.device().tag(), "cart5") == 0)
1452      index = 4;
1453   if (strcmp(image.device().tag(), "cart6") == 0)
1454      index = 5;
1455   if (strcmp(image.device().tag(), "cart7") == 0)
1456      index = 6;
1457   if (strcmp(image.device().tag(), "cart8") == 0)
1458      index = 7;
1459   if (strcmp(image.device().tag(), "cart9") == 0)
1460      index = 8;
1461   if (strcmp(image.device().tag(), "cart10") == 0)
1462      index = 9;
1463   if (strcmp(image.device().tag(), "cart11") == 0)
1464      index = 10;
1465   if (strcmp(image.device().tag(), "cart12") == 0)
1466      index = 11;
1467   if (strcmp(image.device().tag(), "cart13") == 0)
1468      index = 12;
1469   if (strcmp(image.device().tag(), "cart14") == 0)
1470      index = 13;
1471   if (strcmp(image.device().tag(), "cart15") == 0)
1472      index = 14;
1473   if (strcmp(image.device().tag(), "cart16") == 0)
1474      index = 15;
1475 
1476   state->m_cartridge[index].features = 0;
1477 
1478   if (image.software_entry() == NULL)
1479   {
1480      size = image.length();
1481      extrainfo = hashfile_extrainfo(image);
1482   }
1483   else
1484      size = image.get_software_region_length("rom");
1485 
1486   /* Check for 512-byte header */
1487   if ((size / 512) & 1)
1488   {
1489      offset = 512;
1490      size -= 512;
1491   }
1492 
1493   if (!size)
1494   {
1495      image.seterror(IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small");
1496      return IMAGE_INIT_FAIL;
1497   }
1498 
1499   /* Create a new memory region to hold the ROM. */
1500   /* Make sure the region holds only complete (0x4000) rom banks */
1501   state->m_cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1502   state->m_cartridge[index].ROM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].size);
1503   state->m_cartridge[index].cartSRAM = auto_alloc_array(machine, UINT8, NVRAM_SIZE);
1504 
1505   /* Load ROM banks */
1506   if (image.software_entry() == NULL)
1507   {
1508      image.fseek(offset, SEEK_SET);
1509 
1510      if (image.fread( state->m_cartridge[index].ROM, size) != size)
1511         return IMAGE_INIT_FAIL;
1512   }
1513   else
1514   {
1515      memcpy(state->m_cartridge[index].ROM, image.get_software_region("rom") + offset, size);
1516 
1517      // check for mapper feature (currently only KOREAN_NOBANK, but eventually we will add the other mappers as well)
1518      if (image.get_feature("pcb") != NULL)
1519      {
1520         const char *mapper = image.get_feature("pcb");
1521         if (!strcmp(mapper, "korean_nobank"))
1522            state->m_cartridge[index].features |= CF_KOREAN_NOBANK_MAPPER;
1523      }
1524   }
1525 
1526   /* check the image */
1527   if (!state->m_has_bios)
1528   {
1529      if (sms_verify_cart(state->m_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
1530      {
1531         logerror("Warning loading image: sms_verify_cart failed\n");
1532      }
1533   }
1534 
1535   /* Detect special features from the extrainfo field */
1536   if (extrainfo)
1537   {
1538      /* Check for codemasters mapper */
1539      if (strstr(extrainfo, "CODEMASTERS"))
1540         state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1541 
1542      /* Check for korean mapper */
1543      if (strstr(extrainfo, "KOREAN"))
1544         state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1545 
1546      /* Check for special SMS Compatibility mode gamegear cartridges */
1547      if (state->m_is_gamegear && strstr(extrainfo, "GGSMS"))
1548         state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1549 
1550      /* Check for 93C46 eeprom */
1551      if (strstr(extrainfo, "93C46"))
1552         state->m_cartridge[index].features |= CF_93C46_EEPROM;
1553 
1554      /* Check for 8KB on-cart RAM */
1555      if (strstr(extrainfo, "8KB_CART_RAM"))
1556      {
1557         state->m_cartridge[index].features |= CF_ONCART_RAM;
1558         state->m_cartridge[index].ram_size = 0x2000;
1559         state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1560      }
1561   }
1562   else
1563   {
1564      /* If no extrainfo information is available try to find special information out on our own */
1565      /* Check for special cartridge features (new routine, courtesy of Omar Cornut, from MEKA)  */
1566      if (size >= 0x8000)
1567      {
1568         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, i;
1569         for (i = 0; i < 0x8000; i++)
1570         {
1571            if (state->m_cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
1572            {
1573               UINT16 addr = (state->m_cartridge[index].ROM[i + 2] << 8) | state->m_cartridge[index].ROM[i + 1];
1574               if (addr == 0xFFFF)
1575               { i += 2; cFFFF++; continue; }
1576               if (addr == 0x0002 || addr == 0x0003 || addr == 0x0004)
1577               { i += 2; c0002++; continue; }
1578               if (addr == 0x8000)
1579               { i += 2; c8000++; continue; }
1580               if (addr == 0xA000)
1581               { i += 2; cA000++; continue; }
1582            }
1583         }
1584 
1585         LOG(("Mapper test: c002 = %d, c8000 = %d, cA000 = %d, cFFFF = %d\n", c0002, c8000, cA000, cFFFF));
1586 
1587         // 2 is a security measure, although tests on existing ROM showed it was not needed
1588         if (c0002 > cFFFF + 2 || (c0002 > 0 && cFFFF == 0))
1589            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1590         else if (c8000 > cFFFF + 2 || (c8000 > 0 && cFFFF == 0))
1591            state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1592         else if (cA000 > cFFFF + 2 || (cA000 > 0 && cFFFF == 0))
1593            state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1594 
1595      }
1596 
1597      /* Check for special SMS Compatibility mode gamegear cartridges */
1598      if (state->m_is_gamegear && image.software_entry() == NULL)   // not sure about how to handle this with softlists
1599      {
1600         /* Just in case someone passes us an sms file */
1601         if (!mame_stricmp (image.filetype(), "sms"))
1602            state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1603      }
1604   }
1605 
1606   if (state->m_cartridge[index].features & CF_CODEMASTERS_MAPPER)
1607   {
1608      state->m_cartridge[index].ram_size = 0x10000;
1609      state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1610      state->m_cartridge[index].ram_page = 0;
1611   }
1612 
1613   /* For Light Phaser games, we have to detect the x offset */
1614   state->m_lphaser_x_offs = detect_lphaser_xoffset(machine, state->m_cartridge[index].ROM);
1615 
1616   /* Terebi Oekaki (TV Draw) is a SG1000 game with special input device which is compatible with SG1000 Mark III */
1617   if ((detect_tvdraw(state->m_cartridge[index].ROM)) && state->m_is_region_japan)
1618   {
1619      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1620      program->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_tvdraw_axis_w));
1621      program->install_legacy_read_handler(0x8000, 0x8000, FUNC(sms_tvdraw_status_r));
1622      program->install_legacy_read_handler(0xa000, 0xa000, FUNC(sms_tvdraw_data_r));
1623      program->nop_write(0xa000, 0xa000);
1624   }
1625 
1626   if (state->m_cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
1627   {
1628      // FIXME: we should have by default FFFD-FFFF to be only a mirror for DFFD-DFFF (with no bankswitch logic)
1629      // and then the handlers should be installed here for all but the KOREAN_NOBANK carts
1630      // However, to avoid memory map breakage, we currently go the other way around
1631      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1632      program->install_legacy_readwrite_handler(0xfffc, 0xffff, FUNC(sms_kor_nobank_r), FUNC(sms_kor_nobank_w));
1633   }
1634 
1635   LOG(("Cart Features: %x\n", state->m_cartridge[index].features));
1636 
1637   /* Load battery backed RAM, if available */
1638   image.battery_load(state->m_cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE, 0x00);
1639 
1640   return IMAGE_INIT_PASS;
1641}
1642 
1643 
1644static void setup_cart_banks( running_machine &machine )
1645{
1646   sms_state *state = machine.driver_data<sms_state>();
1647   if (state->m_cartridge[state->m_current_cartridge].ROM)
1648   {
1649      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1650      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM;
1651      state->m_banking_cart[2] = state->m_cartridge[state->m_current_cartridge].ROM + 0x0400;
1652      state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1653      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
1654      /* Codemasters mapper points to bank 0 for page 2 */
1655      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1656      {
1657         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM;
1658      }
1659   }
1660   else
1661   {
1662      state->m_banking_cart[1] = state->m_banking_none[1];
1663      state->m_banking_cart[2] = state->m_banking_none[2];
1664      state->m_banking_cart[3] = state->m_banking_none[3];
1665      state->m_banking_cart[5] = state->m_banking_none[5];
1666   }
1667}
1668 
1669#ifdef MESS
1670static void setup_banks( running_machine &machine )
1671{
1672   sms_state *state = machine.driver_data<sms_state>();
1673   UINT8 *mem = machine.region("maincpu")->base();
1674   state->m_banking_bios[1] = state->m_banking_cart[1] = state->m_banking_none[1] = mem;
1675   state->m_banking_bios[2] = state->m_banking_cart[2] = state->m_banking_none[2] = mem;
1676   state->m_banking_bios[3] = state->m_banking_cart[3] = state->m_banking_none[3] = mem;
1677   state->m_banking_bios[4] = state->m_banking_cart[4] = state->m_banking_none[4] = mem;
1678   state->m_banking_bios[5] = state->m_banking_cart[5] = state->m_banking_none[5] = mem;
1679   state->m_banking_bios[6] = state->m_banking_cart[6] = state->m_banking_none[6] = mem;
1680 
1681   state->m_BIOS = machine.region("user1")->base();
1682 
1683   state->m_bios_page_count = (state->m_BIOS ? machine.region("user1")->bytes() / 0x4000 : 0);
1684 
1685   setup_cart_banks(machine);
1686 
1687   if (state->m_BIOS == NULL || state->m_BIOS[0] == 0x00)
1688   {
1689      state->m_BIOS = NULL;
1690      state->m_bios_port |= IO_BIOS_ROM;
1691   }
1692 
1693   if (state->m_BIOS)
1694   {
1695      state->m_banking_bios[1] = state->m_BIOS;
1696      state->m_banking_bios[2] = state->m_BIOS + 0x0400;
1697      state->m_banking_bios[3] = state->m_BIOS + ((1 < state->m_bios_page_count) ? 0x4000 : 0);
1698      state->m_banking_bios[5] = state->m_BIOS + ((2 < state->m_bios_page_count) ? 0x8000 : 0);
1699   }
1700}
1701#endif
1702 
1703MACHINE_START( sms )
1704{
1705   sms_state *state = machine.driver_data<sms_state>();
1706 
1707   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(sms_machine_stop),&machine));
1708   state->m_rapid_fire_timer = machine.scheduler().timer_alloc(FUNC(rapid_fire_callback));
1709   state->m_rapid_fire_timer->adjust(attotime::from_hz(10), 0, attotime::from_hz(10));
1710 
1711   state->m_lphaser_1_timer = machine.scheduler().timer_alloc(FUNC(lphaser_1_callback));
1712   state->m_lphaser_2_timer = machine.scheduler().timer_alloc(FUNC(lphaser_2_callback));
1713 
1714   state->m_main_cpu = machine.device("maincpu");
1715   state->m_control_cpu = machine.device("control");
1716   state->m_vdp = machine.device("sms_vdp");
1717   state->m_ym = machine.device("ym2413");
1718   state->m_main_scr = machine.device("screen");
1719   state->m_left_lcd = machine.device("left_lcd");
1720   state->m_right_lcd = machine.device("right_lcd");
1721 
1722   /* Check if lightgun has been chosen as input: if so, enable crosshair */
1723   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
1724}
1725 
1726#ifdef MESS
1727MACHINE_RESET( sms )
1728{
1729   address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
1730   sms_state *state = machine.driver_data<sms_state>();
1731 
1732   state->m_ctrl_reg = 0xff;
1733   if (state->m_has_fm)
1734      state->m_fm_detect = 0x01;
1735 
1736   state->m_mapper_ram = (UINT8*)space->get_write_ptr(0xdffc);
1737 
1738   state->m_bios_port = 0;
1739 
1740   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1741   {
1742      /* Install special memory handlers */
1743      space->install_legacy_write_handler(0x0000, 0x0000, FUNC(sms_codemasters_page0_w));
1744      space->install_legacy_write_handler(0x4000, 0x4000, FUNC(sms_codemasters_page1_w));
1745   }
1746 
1747   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
1748      space->install_legacy_write_handler(0x0000, 0x0003, FUNC(sms_korean_zemina_banksw_w));
1749 
1750   if (state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE)
1751      sms_vdp_set_ggsmsmode(state->m_vdp, 1);
1752 
1753   /* Initialize SIO stuff for GG */
1754   state->m_gg_sio[0] = 0x7f;
1755   state->m_gg_sio[1] = 0xff;
1756   state->m_gg_sio[2] = 0x00;
1757   state->m_gg_sio[3] = 0xff;
1758   state->m_gg_sio[4] = 0x00;
1759 
1760   state->m_store_control = 0;
1761 
1762   setup_banks(machine);
1763 
1764   setup_rom(space);
1765 
1766   state->m_rapid_fire_state_1 = 0;
1767   state->m_rapid_fire_state_2 = 0;
1768 
1769   state->m_last_paddle_read_time = 0;
1770   state->m_paddle_read_state = 0;
1771 
1772   state->m_last_sports_pad_time_1 = 0;
1773   state->m_last_sports_pad_time_2 = 0;
1774   state->m_sports_pad_state_1 = 0;
1775   state->m_sports_pad_state_2 = 0;
1776   state->m_sports_pad_last_data_1 = 0;
1777   state->m_sports_pad_last_data_2 = 0;
1778   state->m_sports_pad_1_x = 0;
1779   state->m_sports_pad_1_y = 0;
1780   state->m_sports_pad_2_x = 0;
1781   state->m_sports_pad_2_y = 0;
1782 
1783   state->m_lphaser_1_latch = 0;
1784   state->m_lphaser_2_latch = 0;
1785 
1786   state->m_sscope_state = 0;
1787 
1788   state->m_tvdraw_data = 0;
1789}
1790#endif
1791 
1792READ8_HANDLER( sms_store_cart_select_r )
1793{
1794   return 0xff;
1795}
1796 
1797 
1798WRITE8_HANDLER( sms_store_cart_select_w )
1799{
1800   sms_state *state = space->machine().driver_data<sms_state>();
1801   UINT8 slot = data >> 4;
1802   UINT8 slottype = data & 0x08;
1803 
1804   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
1805   /* cartridge? slot #0 */
1806   if (slottype == 0)
1807      state->m_current_cartridge = slot;
1808 
1809   setup_cart_banks(space->machine());
1810   memory_set_bankptr(space->machine(), "bank10", state->m_banking_cart[3] + 0x2000);
1811   setup_rom(space);
1812}
1813 
1814 
1815READ8_HANDLER( sms_store_select1 )
1816{
1817   return 0xff;
1818}
1819 
1820 
1821READ8_HANDLER( sms_store_select2 )
1822{
1823   return 0xff;
1824}
1825 
1826 
1827READ8_HANDLER( sms_store_control_r )
1828{
1829   sms_state *state = space->machine().driver_data<sms_state>();
1830   return state->m_store_control;
1831}
1832 
1833 
1834WRITE8_HANDLER( sms_store_control_w )
1835{
1836   sms_state *state = space->machine().driver_data<sms_state>();
1837   logerror("0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(&space->device()), data);
1838   if (data & 0x02)
1839   {
1840      space->machine().device<cpu_device>("maincpu")->resume(SUSPEND_REASON_HALT);
1841   }
1842   else
1843   {
1844      /* Pull reset line of CPU #0 low */
1845      space->machine().device("maincpu")->reset();
1846      space->machine().device<cpu_device>("maincpu")->suspend(SUSPEND_REASON_HALT, 1);
1847   }
1848   state->m_store_control = data;
1849}
1850 
1851void sms_store_int_callback( running_machine &machine, int state )
1852{
1853   sms_state *driver_state = machine.driver_data<sms_state>();
1854   device_set_input_line(driver_state->m_store_control & 0x01 ? driver_state->m_control_cpu : driver_state->m_main_cpu, 0, state);
1855}
1856 
1857 
1858static void sms_set_zero_flag( running_machine &machine )
1859{
1860   sms_state *state = machine.driver_data<sms_state>();
1861   state->m_is_gamegear = 0;
1862   state->m_is_region_japan = 0;
1863   state->m_has_bios_0400 = 0;
1864   state->m_has_bios_2000 = 0;
1865   state->m_has_bios_full = 0;
1866   state->m_has_bios = 0;
1867   state->m_has_fm = 0;
1868}
1869 
1870DRIVER_INIT( sg1000m3 )
1871{
1872   sms_state *state = machine.driver_data<sms_state>();
1873   sms_set_zero_flag(machine);
1874   state->m_is_region_japan = 1;
1875   state->m_has_fm = 1;
1876}
1877 
1878 
1879DRIVER_INIT( sms1 )
1880{
1881   sms_state *state = machine.driver_data<sms_state>();
1882   sms_set_zero_flag(machine);
1883   state->m_has_bios_full = 1;
1884}
1885 
1886 
1887DRIVER_INIT( smsj )
1888{
1889   sms_state *state = machine.driver_data<sms_state>();
1890   sms_set_zero_flag(machine);
1891   state->m_is_region_japan = 1;
1892   state->m_has_bios_2000 = 1;
1893   state->m_has_fm = 1;
1894}
1895 
1896 
1897DRIVER_INIT( sms2kr )
1898{
1899   sms_state *state = machine.driver_data<sms_state>();
1900   sms_set_zero_flag(machine);
1901   state->m_is_region_japan = 1;
1902   state->m_has_bios_full = 1;
1903   state->m_has_fm = 1;
1904}
1905 
1906 
1907DRIVER_INIT( smssdisp )
1908{
1909   sms_set_zero_flag(machine);
1910}
1911 
1912 
1913DRIVER_INIT( gamegear )
1914{
1915   sms_state *state = machine.driver_data<sms_state>();
1916   sms_set_zero_flag(machine);
1917   state->m_is_gamegear = 1;
1918}
1919 
1920 
1921DRIVER_INIT( gamegeaj )
1922{
1923   sms_state *state = machine.driver_data<sms_state>();
1924   sms_set_zero_flag(machine);
1925   state->m_is_region_japan = 1;
1926   state->m_is_gamegear = 1;
1927   state->m_has_bios_0400 = 1;
1928}
1929 
1930 
1931static void sms_black_bitmap( const screen_device *screen, bitmap_t *bitmap )
1932{
1933   const int width = screen->width();
1934   const int height = screen->height();
1935   int x, y;
1936 
1937   for (y = 0; y < height; y++)
1938      for (x = 0; x < width; x++)
1939         *BITMAP_ADDR32(bitmap, y, x) = MAKE_RGB(0,0,0);
1940}
1941 
1942VIDEO_START( sms1 )
1943{
1944   sms_state *state = machine.driver_data<sms_state>();
1945   screen_device *screen = machine.first_screen();
1946   int width = screen->width();
1947   int height = screen->height();
1948 
1949   state->m_prevleft_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1950   state->m_prevright_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1951   state->save_item(NAME(*state->m_prevleft_bitmap));
1952   state->save_item(NAME(*state->m_prevright_bitmap));
1953}
1954 
1955SCREEN_UPDATE( sms1 )
1956{
1957   sms_state *state = screen->machine().driver_data<sms_state>();
1958   UINT8 sscope = input_port_read_safe(screen->machine(), "SEGASCOPE", 0x00);
1959   UINT8 sscope_binocular_hack = input_port_read_safe(screen->machine(), "SSCOPE_BINOCULAR", 0x00);
1960   UINT8 occluded_view = 0;
1961 
1962   // without SegaScope, both LCDs for glasses go black
1963   if ((screen != state->m_main_scr) && !sscope)
1964      occluded_view = 1;
1965 
1966   // with SegaScope, sscope_state 0 = left screen OFF, right screen ON
1967   if (!(state->m_sscope_state & 0x01) && (screen == state->m_left_lcd))
1968      occluded_view = 1;
1969 
1970   // with SegaScope, sscope_state 1 = left screen ON, right screen OFF
1971   if ((state->m_sscope_state & 0x01) && (screen == state->m_right_lcd))
1972      occluded_view = 1;
1973 
1974   if (!occluded_view)
1975   {
1976      sms_vdp_update(state->m_vdp, bitmap, cliprect);
1977 
1978      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1979      // save a copy of current bitmap for the binocular hack
1980      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1981         copybitmap(state->m_prevleft_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1982      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1983         copybitmap(state->m_prevright_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1984   }
1985   else
1986   {
1987      sms_black_bitmap(screen, bitmap);
1988 
1989      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1990      // use the copied bitmap for the binocular hack
1991      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1992         copybitmap(bitmap, state->m_prevleft_bitmap, 0, 0, 0, 0, cliprect);
1993      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1994         copybitmap(bitmap, state->m_prevright_bitmap, 0, 0, 0, 0, cliprect);
1995   }
1996 
1997   return 0;
1998}
1999 
2000SCREEN_UPDATE( sms )
2001{
2002   sms_state *state = screen->machine().driver_data<sms_state>();
2003   sms_vdp_update(state->m_vdp, bitmap, cliprect);
2004   return 0;
2005}
2006 
2007VIDEO_START( gamegear )
2008{
2009   sms_state *state = machine.driver_data<sms_state>();
2010   screen_device *screen = machine.first_screen();
2011   int width = screen->width();
2012   int height = screen->height();
2013 
2014   state->m_prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2015   state->m_tmp_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2016   state->save_item(NAME(*state->m_prev_bitmap));
2017}
2018 
2019SCREEN_UPDATE( gamegear )
2020{
2021   sms_state *state = screen->machine().driver_data<sms_state>();
2022   int width = screen->width();
2023   int height = screen->height();
2024   int x, y;
2025 
2026   sms_vdp_update(state->m_vdp, state->m_tmp_bitmap, cliprect);
2027 
2028   // HACK: fake LCD persistence effect
2029   // (it would be better to generalize this in the core, to be used for all LCD systems)
2030   for (y = 0; y < height; y++)
2031   {
2032      UINT32 *line0 = BITMAP_ADDR32(state->m_tmp_bitmap, y, 0);
2033      UINT32 *line1 = BITMAP_ADDR32(state->m_prev_bitmap, y, 0);
2034      for (x = 0; x < width; x++)
2035      {
2036         UINT32 color0 = line0[x];
2037         UINT32 color1 = line1[x];
2038         UINT16 r0 = (color0 >> 16) & 0x000000ff;
2039         UINT16 g0 = (color0 >>  8) & 0x000000ff;
2040         UINT16 b0 = (color0 >>  0) & 0x000000ff;
2041         UINT16 r1 = (color1 >> 16) & 0x000000ff;
2042         UINT16 g1 = (color1 >>  8) & 0x000000ff;
2043         UINT16 b1 = (color1 >>  0) & 0x000000ff;
2044         UINT8 r = (UINT8)((r0 + r1) >> 1);
2045         UINT8 g = (UINT8)((g0 + g1) >> 1);
2046         UINT8 b = (UINT8)((b0 + b1) >> 1);
2047         *BITMAP_ADDR32(bitmap, y, x) = (r << 16) | (g << 8) | b;
2048      }
2049   }
2050   copybitmap(state->m_prev_bitmap, state->m_tmp_bitmap, 0, 0, 0, 0, cliprect);
2051 
2052   return 0;
2053}
src/mess/includes/segasms.h
r0r11573
1/*****************************************************************************
2 *
3 * includes/sms.h
4 *
5 ****************************************************************************/
6
7#ifndef SMS_H_
8#define SMS_H_
9
10#define LOG_REG
11#define LOG_PAGING
12#define LOG_COLOR
13
14#define NVRAM_SIZE             (0x08000)
15#define CPU_ADDRESSABLE_SIZE   (0x10000)
16
17#define MAX_CARTRIDGES        16
18
19class sms_state : public driver_device
20{
21public:
22   sms_state(const machine_config &mconfig, device_type type, const char *tag)
23   : driver_device(mconfig, type, tag) { }
24
25   // device_ts
26   device_t *m_main_cpu;
27   device_t *m_control_cpu;
28   device_t *m_vdp;
29   device_t *m_ym;
30   device_t *m_main_scr;
31   device_t *m_left_lcd;
32   device_t *m_right_lcd;
33
34   UINT8 m_bios_page_count;
35   UINT8 m_fm_detect;
36   UINT8 m_ctrl_reg;
37   int m_paused;
38   UINT8 m_bios_port;
39   UINT8 *m_BIOS;
40   UINT8 *m_mapper_ram;
41   UINT8 m_mapper[4];
42   // we are going to use 1-6, same as bank numbers. Notice, though, that most mappers
43   // only work on 16K banks and, hence, banks 4-6 are not always directly set
44   // (they often use bank3 + 0x2000 and bank5 + 0x2000)
45   UINT8 *m_banking_bios[7];
46   UINT8 *m_banking_cart[7];
47   UINT8 *m_banking_none[7];
48   UINT8 m_gg_sio[5];
49   UINT8 m_store_control;
50   UINT8 m_input_port0;
51   UINT8 m_input_port1;
52
53   // for gamegear LCD persistence hack
54   bitmap_t *m_tmp_bitmap;
55   bitmap_t *m_prev_bitmap;
56
57   // for 3D glass binocular hack
58   bitmap_t *m_prevleft_bitmap;
59   bitmap_t *m_prevright_bitmap;
60
61   /* Model identifiers */
62   UINT8 m_is_gamegear;
63   UINT8 m_is_region_japan;
64   UINT8 m_has_bios_0400;
65   UINT8 m_has_bios_2000;
66   UINT8 m_has_bios_full;
67   UINT8 m_has_bios;
68   UINT8 m_has_fm;
69
70   /* Data needed for Rapid Fire Unit support */
71   emu_timer *m_rapid_fire_timer;
72   UINT8 m_rapid_fire_state_1;
73   UINT8 m_rapid_fire_state_2;
74
75   /* Data needed for Paddle Control controller */
76   UINT32 m_last_paddle_read_time;
77   UINT8 m_paddle_read_state;
78
79   /* Data needed for Sports Pad controller */
80   UINT32 m_last_sports_pad_time_1;
81   UINT32 m_last_sports_pad_time_2;
82   UINT8 m_sports_pad_state_1;
83   UINT8 m_sports_pad_state_2;
84   UINT8 m_sports_pad_last_data_1;
85   UINT8 m_sports_pad_last_data_2;
86   UINT8 m_sports_pad_1_x;
87   UINT8 m_sports_pad_1_y;
88   UINT8 m_sports_pad_2_x;
89   UINT8 m_sports_pad_2_y;
90
91   /* Data needed for Light Phaser */
92   emu_timer *m_lphaser_1_timer;
93   emu_timer *m_lphaser_2_timer;
94   UINT8 m_lphaser_1_latch;
95   UINT8 m_lphaser_2_latch;
96   int m_lphaser_x_offs;   /* Needed to 'calibrate' lphaser; set at cart loading */
97
98   /* Data needed for SegaScope (3D glasses) */
99   UINT8 m_sscope_state;
100
101   /* Data needed for Terebi Oekaki (TV Draw) */
102   UINT8 m_tvdraw_data;
103
104   /* Cartridge slot info */
105   UINT8 m_current_cartridge;
106   struct
107   {
108      UINT8 *ROM;        /* Pointer to ROM image data */
109      UINT32 size;       /* Size of the ROM image */
110      UINT8 features;    /* on-cartridge special hardware */
111      UINT8 *cartSRAM;   /* on-cartridge SRAM */
112      UINT8 sram_save;   /* should be the contents of the on-cartridge SRAM be saved */
113      UINT8 *cartRAM;    /* additional on-cartridge RAM (64KB for Ernie Els Golf) */
114      UINT32 ram_size;   /* size of the on-cartridge RAM */
115      UINT8 ram_page;    /* currently swapped in cartridge RAM */
116   } m_cartridge[MAX_CARTRIDGES];
117};
118
119
120/*----------- defined in machine/sms.c -----------*/
121
122/* Function prototypes */
123WRITE8_HANDLER( sms_cartram_w );
124WRITE8_HANDLER( sms_cartram2_w );
125WRITE8_HANDLER( sms_fm_detect_w );
126READ8_HANDLER( sms_fm_detect_r );
127READ8_HANDLER( sms_input_port_0_r );
128READ8_HANDLER( sms_input_port_1_r );
129WRITE8_HANDLER( sms_ym2413_register_port_0_w );
130WRITE8_HANDLER( sms_ym2413_data_port_0_w );
131WRITE8_HANDLER( sms_io_control_w );
132READ8_HANDLER( sms_count_r );
133WRITE8_HANDLER( sms_sscope_w );
134READ8_HANDLER( sms_sscope_r );
135WRITE8_HANDLER( sms_mapper_w );
136READ8_HANDLER( sms_mapper_r );
137WRITE8_HANDLER( sms_bios_w );
138WRITE8_HANDLER( gg_sio_w );
139READ8_HANDLER( gg_sio_r );
140READ8_HANDLER( gg_input_port_2_r );
141
142INPUT_CHANGED( lgun1_changed );
143INPUT_CHANGED( lgun2_changed );
144
145void sms_pause_callback( running_machine &machine );
146void sms_store_int_callback( running_machine &machine, int state );
147
148DEVICE_START( sms_cart );
149DEVICE_IMAGE_LOAD( sms_cart );
150
151MACHINE_START( sms );
152MACHINE_RESET( sms );
153
154READ8_HANDLER( sms_store_cart_select_r );
155WRITE8_HANDLER( sms_store_cart_select_w );
156READ8_HANDLER( sms_store_select1 );
157READ8_HANDLER( sms_store_select2 );
158READ8_HANDLER( sms_store_control_r );
159WRITE8_HANDLER( sms_store_control_w );
160
161#define IO_EXPANSION    (0x80)   /* Expansion slot enable (1= disabled, 0= enabled) */
162#define IO_CARTRIDGE    (0x40)   /* Cartridge slot enable (1= disabled, 0= enabled) */
163#define IO_CARD         (0x20)   /* Card slot disabled (1= disabled, 0= enabled) */
164#define IO_WORK_RAM     (0x10)   /* Work RAM disabled (1= disabled, 0= enabled) */
165#define IO_BIOS_ROM     (0x08)   /* BIOS ROM disabled (1= disabled, 0= enabled) */
166#define IO_CHIP         (0x04)   /* I/O chip disabled (1= disabled, 0= enabled) */
167
168
169DRIVER_INIT( sg1000m3 );
170DRIVER_INIT( sms1 );
171DRIVER_INIT( smsj );
172DRIVER_INIT( sms2kr );
173DRIVER_INIT( smssdisp );
174DRIVER_INIT( gamegear );
175DRIVER_INIT( gamegeaj );
176
177VIDEO_START( sms1 );
178VIDEO_START( gamegear );
179SCREEN_UPDATE( sms1 );
180SCREEN_UPDATE( sms );
181SCREEN_UPDATE( gamegear );
182
183#endif /* SMS_H_ */
src/mess/video/smsvdp.c
r0r11573
1/*********************************************************************
2
3    smsvdp.c
4
5    Implementation of video hardware chip used by Sega Master System
6
7**********************************************************************/
8
9/*
10    For more information, please see:
11    - http://cgfm2.emuviews.com/txt/msvdp.txt
12    - http://www.smspower.org/forums/viewtopic.php?p=44198
13
14A scanline contains the following sections:
15  - horizontal sync     1  ED      => HSYNC high/increment line counter/generate interrupts/etc
16  - left blanking       2  ED-EE
17  - color burst        14  EE-EF
18  - left blanking       8  F5-F9
19  - left border        13  F9-FF
20  - active display    256  00-7F
21  - right border       15  80-87
22  - right blanking      8  87-8B
23  - horizontal sync    25  8B-97   => HSYNC low
24
25
26NTSC frame timing
27                       256x192         256x224        256x240 (doesn't work on real hardware)
28  - vertical blanking   3  D5-D7        3  E5-E7       3  EE-F0
29  - top blanking       13  D8-E4       13  E8-F4      13  F1-FD
30  - top border         27  E5-FF       11  F5-FF       3  FD-FF
31  - active display    192  00-BF      224  00-DF     240  00-EF
32  - bottom border      24  C0-D7        8  E0-E7       0  F0-F0
33  - bottom blanking     3  D8-DA        3  E8-EA       3  F0-F2
34
35
36PAL frame timing
37                       256x192         256x224        256x240
38  - vertical blanking   3  BA-BC        3  CA-CC       3  D2-D4
39  - top blanking       13  BD-C9       13  CD-D9      13  D5-E1
40  - top border         54  CA-FF       38  DA-FF      30  E2-FF
41  - active display    192  00-BF      224  00-DF     240  00-EF
42  - bottom border      48  C0-EF       32  E0-FF      24  F0-07
43  - bottom blanking     3  F0-F2        3  00-02       3  08-0A
44
45  TODO:
46    - implement differences between SMS1_VDP & SMS_VDP/GG_VDP
47
48*/
49
50#include "emu.h"
51#include "video/smsvdp.h"
52
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)
56
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 */
61
62#define VINT_HPOS             23
63#define HINT_HPOS             23
64#define VCOUNT_CHANGE_HPOS    22
65#define VINT_FLAG_HPOS        7
66#define SPROVR_HPOS           6
67#define SPRCOL_BASEHPOS       42
68#define DISPLAY_CB_HPOS       5  /* fix X-Scroll latchtime (Flubba's VDPTest) */
69
70#define GG_CRAM_SIZE          0x40   /* 32 colors x 2 bytes per color = 64 bytes */
71#define SMS_CRAM_SIZE         0x20   /* 32 colors x 1 bytes per color = 32 bytes */
72#define MAX_CRAM_SIZE         0x40
73
74#define VRAM_SIZE             0x4000
75
76#define PRIORITY_BIT          0x1000
77#define BACKDROP_COLOR        ((smsvdp->vdp_mode == 4 ? 0x10 : 0x00) + (smsvdp->reg[0x07] & 0x0f))
78
79#define NUM_OF_REGISTER       0x10  /* 16 registers */
80
81#define INIT_VCOUNT           0
82#define VERTICAL_BLANKING     1
83#define TOP_BLANKING          2
84#define TOP_BORDER            3
85#define ACTIVE_DISPLAY_V      4
86#define BOTTOM_BORDER         5
87#define BOTTOM_BLANKING       6
88
89static const UINT8 sms_ntsc_192[7] = { 0xd5, 3, 13, 27, 192, 24, 3 };
90static const UINT8 sms_ntsc_224[7] = { 0xe5, 3, 13, 11, 224,  8, 3 };
91static const UINT8 sms_ntsc_240[7] = { 0xee, 3, 13,  3, 240,  0, 3 };
92static const UINT8 sms_pal_192[7]  = { 0xba, 3, 13, 54, 192, 48, 3 };
93static const UINT8 sms_pal_224[7]  = { 0xca, 3, 13, 38, 224, 32, 3 };
94static const UINT8 sms_pal_240[7]  = { 0xd2, 3, 13, 30, 240, 24, 3 };
95
96
97typedef struct _smsvdp_t smsvdp_t;
98struct _smsvdp_t
99{
100   UINT32           features;
101   UINT8            reg[NUM_OF_REGISTER];     /* All the registers */
102   UINT8            status;                   /* Status register */
103   UINT8            reg9copy;                 /* Internal copy of register 9 */
104   UINT8            addrmode;                 /* Type of VDP action */
105   UINT16           addr;                     /* Contents of internal VDP address register */
106   UINT8            cram_mask;                /* Mask to switch between SMS and GG CRAM sizes */
107   int              cram_dirty;               /* Have there been any changes to the CRAM area */
108   int              pending;
109   UINT8            buffer;
110   int              gg_sms_mode;              /* Shrunk SMS screen on GG lcd mode flag */
111   int              irq_state;                /* The status of the IRQ line of the VDP */
112   int              vdp_mode;                 /* Current mode of the VDP: 0,1,2,3,4 */
113   int              y_pixels;                 /* 192, 224, 240 */
114   UINT8            line_counter;
115   UINT8            hcounter;
116   memory_region            *VRAM;                    /* Pointer to VRAM */
117   memory_region            *CRAM;                    /* Pointer to CRAM */
118   const UINT8      *sms_frame_timing;
119   bitmap_t         *tmpbitmap;
120   UINT8            *collision_buffer;
121
122   /* line_buffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
123       Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
124       sms compatibility mode. */
125   int              *line_buffer;
126   int              current_palette[32];
127   smsvdp_int_cb    int_callback;
128   smsvdp_pause_cb  pause_callback;
129   emu_timer        *smsvdp_display_timer;
130};
131
132static TIMER_CALLBACK( smsvdp_display_callback );
133static void sms_refresh_line( running_machine &machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int offsetx, int offsety, int line );
134static void sms_update_palette( smsvdp_t *smsvdp );
135
136
137/*****************************************************************************
138    INLINE FUNCTIONS
139*****************************************************************************/
140
141INLINE smsvdp_t *get_safe_token(device_t *device)
142{
143   assert(device != NULL);
144   assert(device->type() == SMSVDP);
145
146   return (smsvdp_t *)downcast<legacy_device_base *>(device)->token();
147}
148
149INLINE const smsvdp_interface *get_interface(device_t *device)
150{
151   assert(device != NULL);
152   assert((device->type() == SMSVDP));
153   return (const smsvdp_interface *) device->static_config();
154}
155
156/*************************************
157 *
158 *  Utilities
159 *
160 *************************************/
161
162static void set_display_settings( device_t *device )
163{
164   smsvdp_t *smsvdp = get_safe_token(device);
165
166   screen_device *screen = device->machine().first_screen();
167   int height = screen->height();
168   int M1, M2, M3, M4;
169   M1 = smsvdp->reg[0x01] & 0x10;
170   M2 = smsvdp->reg[0x00] & 0x02;
171   M3 = smsvdp->reg[0x01] & 0x08;
172   M4 = smsvdp->reg[0x00] & 0x04;
173   smsvdp->y_pixels = 192;
174   if (M4)
175   {
176      /* mode 4 */
177      smsvdp->vdp_mode = 4;
178      if (M2 && (IS_SMS2_VDP || IS_GAMEGEAR_VDP))
179      {
180         if (M1 && !M3)
181            smsvdp->y_pixels = 224;   /* 224-line display */
182         else if (!M1 && M3)
183            smsvdp->y_pixels = 240;   /* 240-line display */
184      }
185   }
186   else
187   {
188      /* original TMS9918 mode */
189      if (!M1 && !M2 && !M3)
190      {
191         smsvdp->vdp_mode = 0;
192      }
193      else
194//      if (M1 && !M2 && !M3)
195//      {
196//          smsvdp->vdp_mode = 1;
197//      }
198//      else
199      if (!M1 && M2 && !M3)
200      {
201         smsvdp->vdp_mode = 2;
202//      }
203//      else
204//      if (!M1 && !M2 && M3)
205//      {
206//          smsvdp->vdp_mode = 3;
207      }
208      else
209      {
210         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');
211      }
212   }
213
214   switch (smsvdp->y_pixels)
215   {
216   case 192:
217      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_192 : sms_ntsc_192;
218      break;
219
220   case 224:
221      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_224 : sms_ntsc_224;
222      break;
223
224   case 240:
225      smsvdp->sms_frame_timing = (height == PAL_Y_PIXELS) ? sms_pal_240 : sms_ntsc_240;
226      break;
227   }
228   smsvdp->cram_dirty = 1;
229}
230
231
232READ8_DEVICE_HANDLER( sms_vdp_vcount_r )
233{
234   smsvdp_t *smsvdp = get_safe_token(device);
235   int vpos = device->machine().primary_screen->vpos();
236
237   if (device->machine().primary_screen->hpos() < VCOUNT_CHANGE_HPOS)
238   {
239      vpos--;
240      if (vpos < 0)
241         vpos += device->machine().primary_screen->height();
242   }
243
244   return (smsvdp->sms_frame_timing[INIT_VCOUNT] + vpos) & 0xff;
245}
246
247
248READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r )
249{
250   smsvdp_t *smsvdp = get_safe_token(device);
251
252   return smsvdp->hcounter;
253}
254
255WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w )
256{
257   smsvdp_t *smsvdp = get_safe_token(device);
258
259   smsvdp->hcounter = data;
260}
261
262
263void sms_vdp_set_ggsmsmode( device_t *device, int mode )
264{
265   smsvdp_t *smsvdp = get_safe_token(device);
266
267   smsvdp->gg_sms_mode = mode;
268   smsvdp->cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
269}
270
271
272static TIMER_CALLBACK( smsvdp_set_status )
273{
274   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
275
276   smsvdp->status |= (UINT8) param;
277}
278
279
280static TIMER_CALLBACK( smsvdp_check_hint )
281{
282   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
283
284   if (smsvdp->line_counter == 0x00)
285   {
286      smsvdp->line_counter = smsvdp->reg[0x0a];
287      smsvdp->status |= STATUS_HINT;
288   }
289   else
290   {
291      smsvdp->line_counter--;
292   }
293
294   if ((smsvdp->status & STATUS_HINT) && (smsvdp->reg[0x00] & 0x10))
295   {
296      smsvdp->irq_state = 1;
297
298      if (smsvdp->int_callback)
299         smsvdp->int_callback(machine, ASSERT_LINE);
300   }
301}
302
303
304static TIMER_CALLBACK( smsvdp_check_vint )
305{
306   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
307
308   if ((smsvdp->status & STATUS_VINT) && (smsvdp->reg[0x01] & 0x20))
309   {
310      smsvdp->irq_state = 1;
311
312      if (smsvdp->int_callback)
313         smsvdp->int_callback(machine, ASSERT_LINE);
314   }
315}
316
317
318static TIMER_CALLBACK( smsvdp_display_callback )
319{
320   smsvdp_t *smsvdp = (smsvdp_t *) ptr;
321
322   rectangle rec;
323   int vpos = machine.primary_screen->vpos();
324   int vpos_limit = smsvdp->sms_frame_timing[VERTICAL_BLANKING] + smsvdp->sms_frame_timing[TOP_BLANKING]
325                  + smsvdp->sms_frame_timing[TOP_BORDER] + smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V]
326                  + smsvdp->sms_frame_timing[BOTTOM_BORDER] + smsvdp->sms_frame_timing[BOTTOM_BLANKING];
327
328   rec.min_y = rec.max_y = vpos;
329
330   /* Check if we're on the last line of a frame */
331   if (vpos == vpos_limit - 1)
332   {
333      smsvdp->line_counter = smsvdp->reg[0x0a];
334      if (smsvdp->pause_callback)
335         smsvdp->pause_callback(machine);
336
337      return;
338   }
339
340   vpos_limit -= smsvdp->sms_frame_timing[BOTTOM_BLANKING];
341
342   /* Check if we're below the bottom border */
343   if (vpos >= vpos_limit)
344   {
345      smsvdp->line_counter = smsvdp->reg[0x0a];
346      return;
347   }
348
349   vpos_limit -= smsvdp->sms_frame_timing[BOTTOM_BORDER];
350
351   /* Check if we're in the bottom border area */
352   if (vpos >= vpos_limit)
353   {
354      if (vpos == vpos_limit)
355      {
356         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, HINT_HPOS), FUNC(smsvdp_check_hint),0 ,smsvdp);
357      }
358      else
359      {
360         smsvdp->line_counter = smsvdp->reg[0x0a];
361      }
362
363      if (vpos == vpos_limit + 1)
364      {
365         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, VINT_FLAG_HPOS), FUNC(smsvdp_set_status), (int)STATUS_VINT, smsvdp);
366         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, VINT_HPOS), FUNC(smsvdp_check_vint), 0, smsvdp);
367      }
368
369      sms_update_palette(smsvdp);
370
371      /* Draw left border */
372      rec.min_x = LBORDER_START;
373      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
374      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
375
376      /* Draw right border */
377      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
378      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
379      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
380
381      /* Draw middle of the border */
382      /* We need to do this through the regular drawing function so it will */
383      /* be included in the gamegear scaling functions */
384      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]));
385      return;
386   }
387
388   vpos_limit -= smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V];
389
390   /* Check if we're in the active display area */
391   if (vpos >= vpos_limit)
392   {
393      if (vpos == vpos_limit)
394      {
395         smsvdp->reg9copy = smsvdp->reg[0x09];
396      }
397
398      machine.scheduler().timer_set(machine.primary_screen->time_until_pos(vpos, HINT_HPOS), FUNC(smsvdp_check_hint),0, smsvdp);
399
400      sms_update_palette(smsvdp);
401
402      /* Draw left border */
403      rec.min_x = LBORDER_START;
404      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
405      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
406
407      /* Draw right border */
408      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
409      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
410      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
411
412      sms_refresh_line(machine, smsvdp, smsvdp->tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit);
413
414      return;
415   }
416
417   vpos_limit -= smsvdp->sms_frame_timing[TOP_BORDER];
418
419   /* Check if we're in the top border area */
420   if (vpos >= vpos_limit)
421   {
422      smsvdp->line_counter = smsvdp->reg[0x0a];
423      sms_update_palette(smsvdp);
424
425      /* Draw left border */
426      rec.min_x = LBORDER_START;
427      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
428      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
429
430      /* Draw right border */
431      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
432      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
433      bitmap_fill(smsvdp->tmpbitmap, &rec, machine.pens[smsvdp->current_palette[BACKDROP_COLOR]]);
434
435      /* Draw middle of the border */
436      /* We need to do this through the regular drawing function so it will */
437      /* be included in the gamegear scaling functions */
438      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]));
439      return;
440   }
441
442   /* we're in the vertical or top blanking area */
443   smsvdp->line_counter = smsvdp->reg[0x0a];
444}
445
446
447READ8_DEVICE_HANDLER( sms_vdp_data_r )
448{
449   smsvdp_t *smsvdp = get_safe_token(device);
450   UINT8 temp;
451
452   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
453   /* to the address register when in the middle of doing a command.               */
454   /* Cosmic Spacehead needs this, among others                                    */
455   /* Clear pending write flag */
456   smsvdp->pending = 0;
457
458   /* Return read buffer contents */
459   temp = smsvdp->buffer;
460
461   /* Load read buffer */
462   smsvdp->buffer = smsvdp->VRAM->u8((smsvdp->addr & 0x3fff));
463
464   /* Bump internal address register */
465   smsvdp->addr += 1;
466   return temp;
467}
468
469
470READ8_DEVICE_HANDLER( sms_vdp_ctrl_r )
471{
472   smsvdp_t *smsvdp = get_safe_token(device);
473
474   UINT8 temp = smsvdp->status;
475
476   /* Clear pending write flag */
477   smsvdp->pending = 0;
478
479   smsvdp->status &= ~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT);
480
481   if (smsvdp->irq_state == 1)
482   {
483      smsvdp->irq_state = 0;
484
485      if (smsvdp->int_callback)
486         smsvdp->int_callback(device->machine(), CLEAR_LINE);
487   }
488
489   /* low 5 bits return non-zero data (it fixes PGA Tour Golf course map introduction) */
490   return temp | 0x1f;
491}
492
493
494WRITE8_DEVICE_HANDLER( sms_vdp_data_w )
495{
496   smsvdp_t *smsvdp = get_safe_token(device);
497   int address;
498
499   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
500   /* to the address register when in the middle of doing a command.               */
501   /* Cosmic Spacehead needs this, among others                                    */
502   /* Clear pending write flag */
503   smsvdp->pending = 0;
504
505   switch(smsvdp->addrmode)
506   {
507      case 0x00:
508      case 0x01:
509      case 0x02:
510         address = (smsvdp->addr & 0x3fff);
511         smsvdp->VRAM->u8(address) = data;
512         break;
513
514      case 0x03:
515         address = smsvdp->addr & smsvdp->cram_mask;
516         if (data != smsvdp->CRAM->u8(address))
517         {
518            smsvdp->CRAM->u8(address) = data;
519            smsvdp->cram_dirty = 1;
520         }
521         break;
522   }
523
524   smsvdp->buffer = data;
525   smsvdp->addr += 1;
526}
527
528
529WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w )
530{
531   smsvdp_t *smsvdp = get_safe_token(device);
532
533   int reg_num;
534
535   if (smsvdp->pending == 0)
536   {
537      smsvdp->addr = (smsvdp->addr & 0xff00) | data;
538      smsvdp->pending = 1;
539   }
540   else
541   {
542      /* Clear pending write flag */
543      smsvdp->pending = 0;
544
545      smsvdp->addrmode = (data >> 6) & 0x03;
546      smsvdp->addr = (data << 8) | (smsvdp->addr & 0xff);
547      switch (smsvdp->addrmode)
548      {
549      case 0:      /* VRAM reading mode */
550         smsvdp->buffer = smsvdp->VRAM->u8(smsvdp->addr & 0x3fff);
551         smsvdp->addr += 1;
552         break;
553
554      case 1:      /* VRAM writing mode */
555         break;
556
557      case 2:      /* VDP register write */
558         reg_num = data & 0x0f;
559         smsvdp->reg[reg_num] = smsvdp->addr & 0xff;
560         if (reg_num == 0 && smsvdp->addr & 0x02)
561            logerror("overscan enabled.\n");
562
563         if (reg_num == 0 || reg_num == 1)
564            set_display_settings(device);
565
566         if (reg_num == 1)
567         {
568            device->machine().scheduler().timer_set(device->machine().primary_screen->time_until_pos(device->machine().primary_screen->vpos(), VINT_HPOS), FUNC(smsvdp_check_vint),0 ,smsvdp);
569         }
570         smsvdp->addrmode = 0;
571         break;
572
573      case 3:      /* CRAM writing mode */
574         break;
575      }
576   }
577}
578
579
580static void sms_refresh_line_mode4( smsvdp_t *smsvdp, int *line_buffer, int *priority_selected, int line )
581{
582   int tile_column;
583   int x_scroll, y_scroll, x_scroll_start_column;
584   int pixel_x, pixel_plot_x;
585   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
586   int scroll_mod;
587   UINT16 name_table_address;
588   UINT8 *name_table;
589
590   if (smsvdp->y_pixels != 192)
591   {
592      name_table_address = ((smsvdp->reg[0x02] & 0x0c) << 10) | 0x0700;
593      scroll_mod = 256;
594   }
595   else
596   {
597      name_table_address = (smsvdp->reg[0x02] << 10) & 0x3800;
598      scroll_mod = 224;
599   }
600
601   if (IS_SMS1_VDP)
602      name_table_address = name_table_address & (((smsvdp->reg[0x02] & 0x01) << 10) | 0x3bff);
603
604   /* if top 2 rows of screen not affected by horizontal scrolling, then x_scroll = 0 */
605   /* else x_scroll = reg[0x08]                                                       */
606   x_scroll = (((smsvdp->reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - smsvdp->reg[0x08]);
607
608   x_scroll_start_column = (x_scroll >> 3);          /* x starting column tile */
609
610   /* Draw background layer */
611   for (tile_column = 0; tile_column < 33; tile_column++)
612   {
613      UINT16 tile_data;
614      int tile_selected, palette_selected, horiz_selected, vert_selected, priority_select;
615      int tile_line;
616
617      /* Rightmost 8 columns for SMS (or 2 columns for GG) not affected by */
618      /* vertical scrolling when bit 7 of reg[0x00] is set */
619      y_scroll = ((smsvdp->reg[0x00] & 0x80) && (tile_column > 23)) ? 0 : smsvdp->reg9copy;
620
621      name_table = smsvdp->VRAM->base() + name_table_address + ((((line + y_scroll) % scroll_mod) >> 3) << 6);
622
623      tile_line = ((tile_column + x_scroll_start_column) & 0x1f) * 2;
624      tile_data = name_table[tile_line] | (name_table[tile_line + 1] << 8);
625
626      tile_selected = (tile_data & 0x01ff);
627      priority_select = tile_data & PRIORITY_BIT;
628      palette_selected = (tile_data >> 11) & 0x01;
629      vert_selected = (tile_data >> 10) & 0x01;
630      horiz_selected = (tile_data >> 9) & 0x01;
631
632      tile_line = line - ((0x07 - (y_scroll & 0x07)) + 1);
633      if (vert_selected)
634         tile_line = 0x07 - tile_line;
635
636      bit_plane_0 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x00);
637      bit_plane_1 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x01);
638      bit_plane_2 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x02);
639      bit_plane_3 = smsvdp->VRAM->u8(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x03);
640
641      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
642      {
643         UINT8 pen_bit_0, pen_bit_1, pen_bit_2, pen_bit_3;
644         UINT8 pen_selected;
645
646         pen_bit_0 = (bit_plane_0 >> (7 - pixel_x)) & 0x01;
647         pen_bit_1 = (bit_plane_1 >> (7 - pixel_x)) & 0x01;
648         pen_bit_2 = (bit_plane_2 >> (7 - pixel_x)) & 0x01;
649         pen_bit_3 = (bit_plane_3 >> (7 - pixel_x)) & 0x01;
650
651         pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0);
652         if (palette_selected)
653            pen_selected |= 0x10;
654
655
656         if (!horiz_selected)
657         {
658            pixel_plot_x = pixel_x;
659         }
660         else
661         {
662            pixel_plot_x = 7 - pixel_x;
663         }
664         pixel_plot_x = (0 - (x_scroll & 0x07) + (tile_column << 3) + pixel_plot_x);
665         if (pixel_plot_x >= 0 && pixel_plot_x < 256)
666         {
667//              logerror("%x %x\n", pixel_plot_x + pixel_offset_x, pixel_plot_y);
668            line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
669            priority_selected[pixel_plot_x] = priority_select | (pen_selected & 0x0f);
670         }
671      }
672   }
673}
674
675static void sms_refresh_mode4_sprites( running_machine &machine, smsvdp_t *smsvdp, int *line_buffer, int *priority_selected, int pixel_plot_y, int line )
676{
677   int sprite_index;
678   int pixel_x, pixel_plot_x;
679   int sprite_x, sprite_y, sprite_line, sprite_tile_selected, sprite_height, sprite_zoom;
680   int sprite_col_occurred, sprite_col_x;
681   int sprite_buffer[8], sprite_buffer_count, sprite_buffer_index;
682   int bit_plane_0, bit_plane_1, bit_plane_2, bit_plane_3;
683   UINT8 *sprite_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x05] << 7) & 0x3f00);
684
685   /* Draw sprite layer */
686   sprite_height = (smsvdp->reg[0x01] & 0x02 ? 16 : 8);
687   sprite_zoom = 1;
688
689   if (smsvdp->reg[0x01] & 0x01)      /* sprite doubling */
690      sprite_zoom = 2;
691
692   sprite_buffer_count = 0;
693   for (sprite_index = 0; (sprite_index < 64) && (sprite_table[sprite_index] != 0xd0 || smsvdp->y_pixels != 192) && (sprite_buffer_count < 9); sprite_index++)
694   {
695      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
696
697      if (sprite_y > 240)
698         sprite_y -= 256; /* wrap from top if y position is > 240 */
699
700      if ((line >= sprite_y) && (line < (sprite_y + sprite_height * sprite_zoom)))
701      {
702         if (sprite_buffer_count < 8)
703         {
704            sprite_buffer[sprite_buffer_count] = sprite_index;
705         }
706         else if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
707         {
708            /* Too many sprites per line */
709            machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPROVR_HPOS), FUNC(smsvdp_set_status), (int)STATUS_SPROVR, smsvdp);
710         }
711         sprite_buffer_count++;
712      }
713   }
714
715   /* Check if display is disabled */
716   if (!(smsvdp->reg[0x01] & 0x40))
717      return;
718
719   if (sprite_buffer_count > 8)
720      sprite_buffer_count = 8;
721
722   memset(smsvdp->collision_buffer, 0, SMS_X_PIXELS);
723   sprite_buffer_count--;
724
725   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
726   {
727      sprite_index = sprite_buffer[sprite_buffer_index];
728      sprite_y = sprite_table[sprite_index] + 1; /* sprite y position starts at line 1 */
729
730      if (sprite_y > 240)
731         sprite_y -= 256; /* wrap from top if y position is > 240 */
732
733      sprite_x = sprite_table[0x80 + (sprite_index << 1)];
734
735      if (smsvdp->reg[0x00] & 0x08)
736         sprite_x -= 0x08;    /* sprite shift */
737
738      sprite_tile_selected = sprite_table[0x81 + (sprite_index << 1)];
739
740      if (smsvdp->reg[0x06] & 0x04)
741         sprite_tile_selected += 256; /* pattern table select */
742
743      if (smsvdp->reg[0x01] & 0x02)
744         sprite_tile_selected &= 0x01fe; /* force even index */
745
746      sprite_line = (line - sprite_y) / sprite_zoom;
747
748      if (sprite_line > 0x07)
749         sprite_tile_selected += 1;
750
751      bit_plane_0 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x00);
752      bit_plane_1 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x01);
753      bit_plane_2 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x02);
754      bit_plane_3 = smsvdp->VRAM->u8(((sprite_tile_selected << 5) + ((sprite_line & 0x07) << 2)) + 0x03);
755
756      sprite_col_occurred = 0;
757      sprite_col_x = 0;
758
759      for (pixel_x = 0; pixel_x < 8 ; pixel_x++)
760      {
761         UINT8 pen_bit_0, pen_bit_1, pen_bit_2, pen_bit_3;
762         int pen_selected;
763
764         pen_bit_0 = (bit_plane_0 >> (7 - pixel_x)) & 0x01;
765         pen_bit_1 = (bit_plane_1 >> (7 - pixel_x)) & 0x01;
766         pen_bit_2 = (bit_plane_2 >> (7 - pixel_x)) & 0x01;
767         pen_bit_3 = (bit_plane_3 >> (7 - pixel_x)) & 0x01;
768
769         pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0) | 0x10;
770
771         if (pen_selected == 0x10)      /* Transparent palette so skip draw */
772         {
773            continue;
774         }
775
776         if (smsvdp->reg[0x01] & 0x01)
777         {
778            /* sprite doubling is enabled */
779            pixel_plot_x = sprite_x + (pixel_x << 1);
780
781            /* check to prevent going outside of active display area */
782            if (pixel_plot_x < 0 || pixel_plot_x > 255)
783            {
784               continue;
785            }
786
787            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
788            {
789               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
790               line_buffer[pixel_plot_x + 1] = smsvdp->current_palette[pen_selected];
791            }
792            else
793            {
794               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
795               {
796                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
797               }
798               if (priority_selected[pixel_plot_x + 1] == PRIORITY_BIT)
799               {
800                  line_buffer[pixel_plot_x + 1] = smsvdp->current_palette[pen_selected];
801               }
802            }
803            if (smsvdp->collision_buffer[pixel_plot_x] != 1)
804            {
805               smsvdp->collision_buffer[pixel_plot_x] = 1;
806            }
807            else
808            {
809               if (!sprite_col_occurred)
810               {
811                  sprite_col_occurred = 1;
812                  sprite_col_x = pixel_plot_x;
813               }
814            }
815            if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
816            {
817               smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
818            }
819            else
820            {
821               if (!sprite_col_occurred)
822               {
823                  sprite_col_occurred = 1;
824                  sprite_col_x = pixel_plot_x;
825               }
826            }
827         }
828         else
829         {
830            pixel_plot_x = sprite_x + pixel_x;
831
832            /* check to prevent going outside of active display area */
833            if (pixel_plot_x < 0 || pixel_plot_x > 255)
834            {
835               continue;
836            }
837
838            if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
839            {
840               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
841            }
842            else
843            {
844               if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
845               {
846                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
847               }
848            }
849            if (smsvdp->collision_buffer[pixel_plot_x] != 1)
850            {
851               smsvdp->collision_buffer[pixel_plot_x] = 1;
852            }
853            else
854            {
855               if (!sprite_col_occurred)
856               {
857                  sprite_col_occurred = 1;
858                  sprite_col_x = pixel_plot_x;
859               }
860            }
861         }
862      }
863      if (sprite_col_occurred)
864         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x), FUNC(smsvdp_set_status), (int)STATUS_SPRCOL, smsvdp);
865   }
866
867   /* Fill column 0 with overscan color from reg[0x07] */
868   if (smsvdp->reg[0x00] & 0x20)
869   {
870      line_buffer[0] = smsvdp->current_palette[BACKDROP_COLOR];
871      line_buffer[1] = smsvdp->current_palette[BACKDROP_COLOR];
872      line_buffer[2] = smsvdp->current_palette[BACKDROP_COLOR];
873      line_buffer[3] = smsvdp->current_palette[BACKDROP_COLOR];
874      line_buffer[4] = smsvdp->current_palette[BACKDROP_COLOR];
875      line_buffer[5] = smsvdp->current_palette[BACKDROP_COLOR];
876      line_buffer[6] = smsvdp->current_palette[BACKDROP_COLOR];
877      line_buffer[7] = smsvdp->current_palette[BACKDROP_COLOR];
878   }
879}
880
881
882static void sms_refresh_tms9918_sprites( running_machine &machine, smsvdp_t *smsvdp, int *line_buffer, int pixel_plot_y, int line )
883{
884   int pixel_plot_x;
885   int sprite_col_occurred, sprite_col_x;
886   int sprite_height, sprite_buffer_count, sprite_index, sprite_buffer[5], sprite_buffer_index;
887   UINT8 *sprite_table, *sprite_pattern_table;
888
889   /* Draw sprite layer */
890   sprite_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x05] & 0x7f) << 7);
891   sprite_pattern_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x06] & 0x07) << 11);
892   sprite_height = 8;
893
894   if (smsvdp->reg[0x01] & 0x02)                         /* Check if SI is set */
895      sprite_height = sprite_height * 2;
896   if (smsvdp->reg[0x01] & 0x01)                         /* Check if MAG is set */
897      sprite_height = sprite_height * 2;
898
899   sprite_buffer_count = 0;
900   for (sprite_index = 0; (sprite_index < 32 * 4) && (sprite_table[sprite_index] != 0xd0) && (sprite_buffer_count < 5); sprite_index += 4)
901   {
902      int sprite_y = sprite_table[sprite_index] + 1;
903
904      if (sprite_y > 240)
905         sprite_y -= 256;
906
907      if ((line >= sprite_y) && (line < (sprite_y + sprite_height)))
908      {
909         if (sprite_buffer_count < 5)
910         {
911            sprite_buffer[sprite_buffer_count] = sprite_index;
912         }
913         else if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
914         {
915            /* Too many sprites per line */
916            machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPROVR_HPOS), FUNC(smsvdp_set_status), (int)STATUS_SPROVR, smsvdp);
917         }
918         sprite_buffer_count++;
919      }
920   }
921
922   /* Check if display is disabled */
923   if (!(smsvdp->reg[0x01] & 0x40))
924      return;
925
926   if (sprite_buffer_count > 4)
927      sprite_buffer_count = 4;
928
929   memset(smsvdp->collision_buffer, 0, SMS_X_PIXELS);
930   sprite_buffer_count--;
931
932   for (sprite_buffer_index = sprite_buffer_count; sprite_buffer_index >= 0; sprite_buffer_index--)
933   {
934      int pen_selected;
935      int sprite_line, pixel_x, sprite_x, sprite_tile_selected;
936      int sprite_y;
937      UINT8 pattern;
938
939      sprite_index = sprite_buffer[sprite_buffer_index];
940      sprite_y = sprite_table[sprite_index] + 1;
941
942      if (sprite_y > 240)
943         sprite_y -= 256;
944
945      sprite_x = sprite_table[sprite_index + 1];
946      pen_selected = sprite_table[sprite_index + 3] & 0x0f;
947
948      if (IS_GAMEGEAR_VDP)
949         pen_selected |= 0x10;
950
951      if (sprite_table[sprite_index + 3] & 0x80)
952         sprite_x -= 32;
953
954      sprite_tile_selected = sprite_table[sprite_index + 2];
955      sprite_line = line - sprite_y;
956
957      if (smsvdp->reg[0x01] & 0x01)
958         sprite_line >>= 1;
959
960      if (smsvdp->reg[0x01] & 0x02)
961      {
962         sprite_tile_selected &= 0xfc;
963
964         if (sprite_line > 0x07)
965         {
966            sprite_tile_selected += 1;
967            sprite_line -= 8;
968         }
969      }
970
971      pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
972
973      sprite_col_occurred = 0;
974      sprite_col_x = 0;
975
976      for (pixel_x = 0; pixel_x < 8; pixel_x++)
977      {
978         if (smsvdp->reg[0x01] & 0x01)
979         {
980            pixel_plot_x = sprite_x + pixel_x * 2;
981            if (pixel_plot_x < 0 || pixel_plot_x > 255)
982            {
983               continue;
984            }
985
986            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
987            {
988               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
989
990               if (smsvdp->collision_buffer[pixel_plot_x] != 1)
991               {
992                  smsvdp->collision_buffer[pixel_plot_x] = 1;
993               }
994               else
995               {
996                  if (!sprite_col_occurred)
997                  {
998                     sprite_col_occurred = 1;
999                     sprite_col_x = pixel_plot_x;
1000                  }
1001               }
1002
1003               line_buffer[pixel_plot_x+1] = smsvdp->current_palette[pen_selected];
1004
1005               if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
1006               {
1007                  smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
1008               }
1009               else
1010               {
1011                  if (!sprite_col_occurred)
1012                  {
1013                     sprite_col_occurred = 1;
1014                     sprite_col_x = pixel_plot_x;
1015                  }
1016               }
1017            }
1018         }
1019         else
1020         {
1021            pixel_plot_x = sprite_x + pixel_x;
1022
1023            if (pixel_plot_x < 0 || pixel_plot_x > 255)
1024            {
1025               continue;
1026            }
1027
1028            if (pen_selected && (pattern & (1 << (7 - pixel_x))))
1029            {
1030               line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1031
1032               if (smsvdp->collision_buffer[pixel_plot_x] != 1)
1033               {
1034                  smsvdp->collision_buffer[pixel_plot_x] = 1;
1035               }
1036               else
1037               {
1038                  if (!sprite_col_occurred)
1039                  {
1040                     sprite_col_occurred = 1;
1041                     sprite_col_x = pixel_plot_x;
1042                  }
1043               }
1044            }
1045         }
1046      }
1047
1048      if (smsvdp->reg[0x01] & 0x02)
1049      {
1050         sprite_tile_selected += 2;
1051         pattern = sprite_pattern_table[sprite_tile_selected * 8 + sprite_line];
1052         sprite_x += (smsvdp->reg[0x01] & 0x01 ? 16 : 8);
1053
1054         for (pixel_x = 0; pixel_x < 8; pixel_x++)
1055         {
1056            if (smsvdp->reg[0x01] & 0x01)
1057            {
1058               pixel_plot_x = sprite_x + pixel_x * 2;
1059
1060               if (pixel_plot_x < 0 || pixel_plot_x > 255)
1061               {
1062                  continue;
1063               }
1064
1065               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
1066               {
1067                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1068
1069                  if (smsvdp->collision_buffer[pixel_plot_x] != 1)
1070                  {
1071                     smsvdp->collision_buffer[pixel_plot_x] = 1;
1072                  }
1073                  else
1074                  {
1075                     if (!sprite_col_occurred)
1076                     {
1077                        sprite_col_occurred = 1;
1078                        sprite_col_x = pixel_plot_x;
1079                     }
1080                  }
1081
1082                  line_buffer[pixel_plot_x+1] = smsvdp->current_palette[pen_selected];
1083
1084                  if (smsvdp->collision_buffer[pixel_plot_x + 1] != 1)
1085                  {
1086                     smsvdp->collision_buffer[pixel_plot_x + 1] = 1;
1087                  }
1088                  else
1089                  {
1090                     if (!sprite_col_occurred)
1091                     {
1092                        sprite_col_occurred = 1;
1093                        sprite_col_x = pixel_plot_x;
1094                     }
1095                  }
1096               }
1097            }
1098            else
1099            {
1100               pixel_plot_x = sprite_x + pixel_x;
1101
1102               if (pixel_plot_x < 0 || pixel_plot_x > 255)
1103               {
1104                  continue;
1105               }
1106
1107               if (pen_selected && (pattern & (1 << (7 - pixel_x))))
1108               {
1109                  line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1110
1111                  if (smsvdp->collision_buffer[pixel_plot_x] != 1)
1112                  {
1113                     smsvdp->collision_buffer[pixel_plot_x] = 1;
1114                  }
1115                  else
1116                  {
1117                     if (!sprite_col_occurred)
1118                     {
1119                        sprite_col_occurred = 1;
1120                        sprite_col_x = pixel_plot_x;
1121                     }
1122                  }
1123               }
1124            }
1125         }
1126      }
1127      if (sprite_col_occurred)
1128         machine.scheduler().timer_set(machine.primary_screen->time_until_pos(pixel_plot_y + line, SPRCOL_BASEHPOS + sprite_col_x), FUNC(smsvdp_set_status), (int)STATUS_SPRCOL, smsvdp);
1129   }
1130}
1131
1132
1133static void sms_refresh_line_mode2( smsvdp_t *smsvdp, int *line_buffer, int line )
1134{
1135   int tile_column;
1136   int pixel_x, pixel_plot_x;
1137   UINT8 *name_table, *color_table, *pattern_table;
1138   int pattern_mask, color_mask, pattern_offset;
1139
1140   /* Draw background layer */
1141   name_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1142   color_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x03] & 0x80) << 6);
1143   color_mask = ((smsvdp->reg[0x03] & 0x7f) << 3) | 0x07;
1144   pattern_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x04] & 0x04) << 11);
1145   pattern_mask = ((smsvdp->reg[0x04] & 0x03) << 8) | 0xff;
1146   pattern_offset = (line & 0xc0) << 2;
1147
1148   for (tile_column = 0; tile_column < 32; tile_column++)
1149   {
1150      UINT8 pattern;
1151      UINT8 colors;
1152
1153      pattern = pattern_table[(((pattern_offset + name_table[tile_column]) & pattern_mask) * 8) + (line & 0x07)];
1154      colors = color_table[(((pattern_offset + name_table[tile_column]) & color_mask) * 8) + (line & 0x07)];
1155
1156      for (pixel_x = 0; pixel_x < 8; pixel_x++)
1157      {
1158         UINT8 pen_selected;
1159
1160         if (pattern & (1 << (7 - pixel_x)))
1161         {
1162            pen_selected = colors >> 4;
1163         }
1164         else
1165         {
1166            pen_selected = colors & 0x0f;
1167         }
1168
1169         if (!pen_selected)
1170            pen_selected = BACKDROP_COLOR;
1171
1172         pixel_plot_x = (tile_column << 3) + pixel_x;
1173
1174         if (IS_GAMEGEAR_VDP)
1175            pen_selected |= 0x10;
1176
1177         line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1178      }
1179   }
1180}
1181
1182
1183static void sms_refresh_line_mode0( smsvdp_t *smsvdp, int *line_buffer, int line)
1184{
1185   int tile_column;
1186   int pixel_x, pixel_plot_x;
1187   UINT8 *name_table, *color_table, *pattern_table;
1188
1189   /* Draw background layer */
1190   name_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
1191   color_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x03] << 6) & (VRAM_SIZE - 1));
1192   pattern_table = smsvdp->VRAM->base() + ((smsvdp->reg[0x04] << 11) & (VRAM_SIZE - 1));
1193
1194   for (tile_column = 0; tile_column < 32; tile_column++)
1195   {
1196      UINT8 pattern;
1197      UINT8 colors;
1198
1199      pattern = pattern_table[(name_table[tile_column] * 8) + (line & 0x07)];
1200      colors = color_table[name_table[tile_column] >> 3];
1201
1202      for (pixel_x = 0; pixel_x < 8; pixel_x++)
1203      {
1204         int pen_selected;
1205
1206         if (pattern & (1 << (7 - pixel_x)))
1207            pen_selected = colors >> 4;
1208         else
1209            pen_selected = colors & 0x0f;
1210
1211         if (IS_GAMEGEAR_VDP)
1212            pen_selected |= 0x10;
1213
1214         pixel_plot_x = (tile_column << 3) + pixel_x;
1215         line_buffer[pixel_plot_x] = smsvdp->current_palette[pen_selected];
1216      }
1217   }
1218}
1219
1220
1221static void sms_refresh_line( running_machine &machine, smsvdp_t *smsvdp, bitmap_t *bitmap, int pixel_offset_x, int pixel_plot_y, int line )
1222{
1223   int x;
1224   int *blitline_buffer = smsvdp->line_buffer;
1225   int priority_selected[256];
1226
1227   switch( smsvdp->vdp_mode )
1228   {
1229   case 0:
1230      if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1231         sms_refresh_line_mode0(smsvdp, blitline_buffer, line);
1232      sms_refresh_tms9918_sprites(machine, smsvdp, blitline_buffer, pixel_plot_y, line);
1233      break;
1234
1235   case 2:
1236      if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1237         sms_refresh_line_mode2(smsvdp, blitline_buffer, line);
1238      sms_refresh_tms9918_sprites(machine, smsvdp, blitline_buffer, pixel_plot_y, line);
1239      break;
1240
1241   case 4:
1242   default:
1243      memset(priority_selected, 0, sizeof(priority_selected));
1244      if (line >= 0 && line < smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1245         sms_refresh_line_mode4(smsvdp, blitline_buffer, priority_selected, line);
1246      sms_refresh_mode4_sprites(machine, smsvdp, blitline_buffer, priority_selected, pixel_plot_y, line);
1247      break;
1248   }
1249
1250   /* Check if display is disabled or we're below/above active area */
1251   if (!(smsvdp->reg[0x01] & 0x40) || line < 0 || line >= smsvdp->sms_frame_timing[ACTIVE_DISPLAY_V])
1252   {
1253      for (x = 0; x < 256; x++)
1254      {
1255         blitline_buffer[x] = smsvdp->current_palette[BACKDROP_COLOR];
1256      }
1257   }
1258
1259   if (IS_GAMEGEAR_VDP && smsvdp->gg_sms_mode)
1260   {
1261      int *combineline_buffer = smsvdp->line_buffer + ((line & 0x03) + 1) * 256;
1262      int plot_x = 48;
1263
1264      /* Do horizontal scaling */
1265      for (x = 8; x < 248;)
1266      {
1267         int combined;
1268
1269         /* Take red and green from first pixel, and blue from second pixel */
1270         combined = (blitline_buffer[x] & 0x00ff) | (blitline_buffer[x + 1] & 0x0f00);
1271         combineline_buffer[plot_x] = combined;
1272
1273         /* Take red from second pixel, and green and blue from third pixel */
1274         combined = (blitline_buffer[x + 1] & 0x000f) | (blitline_buffer[x + 2] & 0x0ff0);
1275         combineline_buffer[plot_x + 1] = combined;
1276         x += 3;
1277         plot_x += 2;
1278      }
1279
1280      /* Do vertical scaling for a screen with 192 or 224 lines
1281           Lines 0-2 and 221-223 have no effect on the output on the GG screen.
1282           We will calculate the gamegear lines as follows:
1283           GG_0 = 1/6 * SMS_3 + 1/3 * SMS_4 + 1/3 * SMS_5 + 1/6 * SMS_6
1284           GG_1 = 1/6 * SMS_4 + 1/3 * SMS_5 + 1/3 * SMS_6 + 1/6 * SMS_7
1285           GG_2 = 1/6 * SMS_6 + 1/3 * SMS_7 + 1/3 * SMS_8 + 1/6 * SMS_9
1286           GG_3 = 1/6 * SMS_7 + 1/3 * SMS_8 + 1/3 * SMS_9 + 1/6 * SMS_10
1287           GG_4 = 1/6 * SMS_9 + 1/3 * SMS_10 + 1/3 * SMS_11 + 1/6 * SMS_12
1288           .....
1289           GG_142 = 1/6 * SMS_216 + 1/3 * SMS_217 + 1/3 * SMS_218 + 1/6 * SMS_219
1290           GG_143 = 1/6 * SMS_217 + 1/3 * SMS_218 + 1/3 * SMS_219 + 1/6 * SMS_220
1291        */
1292      {
1293         int gg_line;
1294         int my_line = pixel_plot_y + line - (TBORDER_START + NTSC_224_TBORDER_Y_PIXELS);
1295         int *line1, *line2, *line3, *line4;
1296
1297         /* First make sure there's enough data to draw anything */
1298         /* We need one more line of data if we're on line 8, 11, 14, 17, etc */
1299         if (my_line < 6 || my_line > 220 || ((my_line - 8) % 3 == 0))
1300         {
1301            return;
1302         }
1303
1304         gg_line = ((my_line - 6) / 3) * 2;
1305
1306         /* If we're on SMS line 7, 10, 13, etc we're on an odd GG line */
1307         if (my_line % 3)
1308         {
1309            gg_line++;
1310         }
1311
1312         /* Calculate the line we will be drawing on */
1313         pixel_plot_y = TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 24 + gg_line;
1314
1315         /* Setup our source lines */
1316         line1 = smsvdp->line_buffer + (((my_line - 3) & 0x03) + 1) * 256;
1317         line2 = smsvdp->line_buffer + (((my_line - 2) & 0x03) + 1) * 256;
1318         line3 = smsvdp->line_buffer + (((my_line - 1) & 0x03) + 1) * 256;
1319         line4 = smsvdp->line_buffer + (((my_line - 0) & 0x03) + 1) * 256;
1320
1321         for (x = 0 + 48; x < 160 + 48; x++)
1322         {
1323            rgb_t   c1 = machine.pens[line1[x]];
1324            rgb_t   c2 = machine.pens[line2[x]];
1325            rgb_t   c3 = machine.pens[line3[x]];
1326            rgb_t   c4 = machine.pens[line4[x]];
1327            *BITMAP_ADDR32(bitmap, pixel_plot_y, pixel_offset_x + x) =
1328               MAKE_RGB((RGB_RED(c1) / 6 + RGB_RED(c2) / 3 + RGB_RED(c3) / 3 + RGB_RED(c4) / 6 ),
1329                  (RGB_GREEN(c1) / 6 + RGB_GREEN(c2) / 3 + RGB_GREEN(c3) / 3 + RGB_GREEN(c4) / 6 ),
1330                  (RGB_BLUE(c1) / 6 + RGB_BLUE(c2) / 3 + RGB_BLUE(c3) / 3 + RGB_BLUE(c4) / 6 ) );
1331         }
1332         return;
1333      }
1334      blitline_buffer = combineline_buffer;
1335   }
1336
1337   for (x = 0; x < 256; x++)
1338   {
1339      *BITMAP_ADDR32(bitmap, pixel_plot_y + line, pixel_offset_x + x) = machine.pens[blitline_buffer[x]];
1340   }
1341}
1342
1343
1344// This is only used by Light Phaser. Should it be moved elsewhere?
1345int sms_vdp_check_brightness(device_t *device, int x, int y)
1346{
1347   /* brightness of the lightgray color in the frame drawn by Light Phaser games */
1348   const UINT8 sensor_min_brightness = 0x7f;
1349
1350   /* TODO: Check how Light Phaser behaves for border areas. For Gangster Town, should */
1351   /* a shot at right border (HC~=0x90) really appear at active scr, near to left border? */
1352   if (x < LBORDER_START + LBORDER_X_PIXELS || x >= LBORDER_START + LBORDER_X_PIXELS + 256)
1353      return 0;
1354
1355   smsvdp_t *smsvdp = get_safe_token(device);
1356   rgb_t color = *BITMAP_ADDR32(smsvdp->tmpbitmap, y, x);
1357
1358   /* reference: http://www.w3.org/TR/AERT#color-contrast */
1359   UINT8 brightness = (RGB_RED(color) * 0.299) + (RGB_GREEN(color) * 0.587) + (RGB_BLUE(color) * 0.114);
1360   //printf ("color brightness: %2X for x %d y %d\n", brightness, x, y);
1361
1362   return (brightness >= sensor_min_brightness) ? 1 : 0;
1363}
1364
1365
1366static void sms_update_palette( smsvdp_t *smsvdp )
1367{
1368   int i;
1369
1370   /* Exit if palette is has no changes */
1371   if (smsvdp->cram_dirty == 0)
1372   {
1373      return;
1374   }
1375   smsvdp->cram_dirty = 0;
1376
1377   if (smsvdp->vdp_mode != 4 && ! IS_GAMEGEAR_VDP)
1378   {
1379      for(i = 0; i < 16; i++)
1380      {
1381         smsvdp->current_palette[i] = 64 + i;
1382      }
1383      return;
1384   }
1385
1386   if (IS_GAMEGEAR_VDP)
1387   {
1388      if (smsvdp->gg_sms_mode)
1389      {
1390         for (i = 0; i < 32; i++)
1391         {
1392            smsvdp->current_palette[i] = ((smsvdp->CRAM->u8(i) & 0x30) << 6) | ((smsvdp->CRAM->u8(i) & 0x0c ) << 4) | ((smsvdp->CRAM->u8(i) & 0x03) << 2);
1393         }
1394      }
1395      else
1396      {
1397         for (i = 0; i < 32; i++)
1398         {
1399            smsvdp->current_palette[i] = ((smsvdp->CRAM->u8(i * 2 + 1) << 8) | smsvdp->CRAM->u8(i * 2)) & 0x0fff;
1400         }
1401      }
1402   }
1403   else
1404   {
1405      for (i = 0; i < 32; i++)
1406      {
1407         smsvdp->current_palette[i] = smsvdp->CRAM->u8(i) & 0x3f;
1408      }
1409   }
1410}
1411
1412
1413UINT32 sms_vdp_update( device_t *device, bitmap_t *bitmap, const rectangle *cliprect )
1414{
1415   smsvdp_t *smsvdp = get_safe_token(device);
1416   copybitmap(bitmap, smsvdp->tmpbitmap, 0, 0, 0, 0, cliprect);
1417   return 0;
1418}
1419
1420
1421/*****************************************************************************
1422    DEVICE INTERFACE
1423*****************************************************************************/
1424
1425static DEVICE_START( smsvdp )
1426{
1427   smsvdp_t *smsvdp = get_safe_token(device);
1428   const smsvdp_interface *intf = get_interface(device);
1429
1430   screen_device *screen = device->machine().first_screen();
1431   int width = screen->width();
1432   int height = screen->height();
1433
1434   smsvdp->features = intf->model;
1435   smsvdp->int_callback = intf->int_callback;
1436   smsvdp->pause_callback = intf->pause_callback;
1437
1438   /* Allocate video RAM */
1439   smsvdp->VRAM = device->machine().region_alloc("vdp_vram", VRAM_SIZE, 1, ENDIANNESS_LITTLE);
1440   smsvdp->CRAM = device->machine().region_alloc("vdp_cram", MAX_CRAM_SIZE, 1, ENDIANNESS_LITTLE);
1441   smsvdp->line_buffer = auto_alloc_array(device->machine(), int, 256 * 5);
1442
1443   smsvdp->collision_buffer = auto_alloc_array(device->machine(), UINT8, SMS_X_PIXELS);
1444   smsvdp->sms_frame_timing = auto_alloc_array(device->machine(), UINT8, 7);
1445
1446   /* Make temp bitmap for rendering */
1447   smsvdp->tmpbitmap = auto_bitmap_alloc(device->machine(), width, height, BITMAP_FORMAT_INDEXED32);
1448
1449   smsvdp->smsvdp_display_timer = device->machine().scheduler().timer_alloc(FUNC(smsvdp_display_callback), smsvdp);
1450   smsvdp->smsvdp_display_timer->adjust(screen->time_until_pos(0, DISPLAY_CB_HPOS), 0, screen->scan_period());
1451
1452   device->save_item(NAME(smsvdp->status));
1453   device->save_item(NAME(smsvdp->reg9copy));
1454   device->save_item(NAME(smsvdp->addrmode));
1455   device->save_item(NAME(smsvdp->addr));
1456   device->save_item(NAME(smsvdp->cram_mask));
1457   device->save_item(NAME(smsvdp->cram_dirty));
1458   device->save_item(NAME(smsvdp->pending));
1459   device->save_item(NAME(smsvdp->buffer));
1460   device->save_item(NAME(smsvdp->gg_sms_mode));
1461   device->save_item(NAME(smsvdp->irq_state));
1462   device->save_item(NAME(smsvdp->vdp_mode));
1463   device->save_item(NAME(smsvdp->y_pixels));
1464   device->save_item(NAME(smsvdp->line_counter));
1465   device->save_item(NAME(smsvdp->hcounter));
1466   device->save_item(NAME(smsvdp->reg));
1467   device->save_item(NAME(smsvdp->current_palette));
1468   device->save_pointer(NAME(smsvdp->line_buffer), 256 * 5);
1469   device->save_pointer(NAME(smsvdp->collision_buffer), SMS_X_PIXELS);
1470   device->save_item(NAME(*smsvdp->tmpbitmap));
1471}
1472
1473static DEVICE_RESET( smsvdp )
1474{
1475   smsvdp_t *smsvdp = get_safe_token(device);
1476   int i;
1477
1478   /* Most register are 0x00 at power-up */
1479   for (i = 0; i < NUM_OF_REGISTER; i++)
1480      smsvdp->reg[i] = 0x00;
1481
1482   smsvdp->reg[0x02] = 0x0e;
1483   smsvdp->reg[0x0a] = 0xff;
1484
1485   smsvdp->status = 0;
1486   smsvdp->reg9copy = 0;
1487   smsvdp->addrmode = 0;
1488   smsvdp->addr = 0;
1489   smsvdp->gg_sms_mode = 0;
1490   smsvdp->cram_mask = (IS_GAMEGEAR_VDP && !smsvdp->gg_sms_mode) ? (GG_CRAM_SIZE - 1) : (SMS_CRAM_SIZE - 1);
1491   smsvdp->cram_dirty = 1;
1492   smsvdp->pending = 0;
1493   smsvdp->buffer = 0;
1494   smsvdp->irq_state = 0;
1495   smsvdp->line_counter = 0;
1496   smsvdp->hcounter = 0;
1497
1498   for (i = 0; i < 0x20; i++)
1499      smsvdp->current_palette[i] = 0;
1500
1501   set_display_settings(device);
1502
1503   /* Clear RAM */
1504   memset(smsvdp->VRAM->base(), 0, VRAM_SIZE);
1505   memset(smsvdp->CRAM->base(), 0, MAX_CRAM_SIZE);
1506   memset(smsvdp->line_buffer, 0, 256 * 5 * sizeof(int));
1507}
1508
1509DEVICE_GET_INFO( smsvdp )
1510{
1511   switch (state)
1512   {
1513      /* --- the following bits of info are returned as 64-bit signed integers --- */
1514      case DEVINFO_INT_TOKEN_BYTES:         info->i = sizeof(smsvdp_t);               break;
1515
1516      /* --- the following bits of info are returned as pointers to data or functions --- */
1517      case DEVINFO_FCT_START:               info->start = DEVICE_START_NAME(smsvdp);      break;
1518      case DEVINFO_FCT_STOP:               /* Nothing */                           break;
1519      case DEVINFO_FCT_RESET:               info->reset = DEVICE_RESET_NAME(smsvdp);      break;
1520
1521      /* --- the following bits of info are returned as NULL-terminated strings --- */
1522      case DEVINFO_STR_NAME:               strcpy(info->s, "Sega Master SYstem / Game Gear VDP");            break;
1523      case DEVINFO_STR_FAMILY:            strcpy(info->s, "Sega MS VDP");               break;
1524      case DEVINFO_STR_VERSION:            strcpy(info->s, "1.0");                     break;
1525      case DEVINFO_STR_SOURCE_FILE:         strcpy(info->s, __FILE__);                  break;
1526      case DEVINFO_STR_CREDITS:            strcpy(info->s, "Copyright MAME / MESS Team");         break;
1527   }
1528}
1529
1530DEFINE_LEGACY_DEVICE(SMSVDP, smsvdp);
src/mess/video/smsvdp.h
r0r11573
1/*************************************************************************
2
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
20#define MODEL_315_5124         0x0001
21#define MODEL_315_5246         0x0002
22#define MODEL_315_5378         0x0004
23
24#define SMS_X_PIXELS         342      /* 342 pixels */
25#define NTSC_Y_PIXELS         262      /* 262 lines */
26#define PAL_Y_PIXELS         313      /* 313 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 */
32#define NTSC_192_BBORDER_Y_PIXELS   (0x18)      /* 24 lines */
33#define NTSC_224_TBORDER_Y_PIXELS   (0x0b)      /* 11 lines */
34#define NTSC_224_BBORDER_Y_PIXELS   (0x08)      /* 8 lines */
35#define PAL_192_TBORDER_Y_PIXELS   (0x36)      /* 54 lines */
36#define PAL_192_BBORDER_Y_PIXELS   (0x30)      /* 48 lines */
37#define PAL_224_TBORDER_Y_PIXELS   (0x26)      /* 38 lines */
38#define PAL_224_BBORDER_Y_PIXELS   (0x20)      /* 32 lines */
39#define PAL_240_TBORDER_Y_PIXELS   (0x1e)      /* 30 lines */
40#define PAL_240_BBORDER_Y_PIXELS   (0x18)      /* 24 lines */
41
42
43/***************************************************************************
44    TYPE DEFINITIONS
45***************************************************************************/
46
47typedef void (*smsvdp_int_cb)( running_machine &machine, int state );
48typedef void (*smsvdp_pause_cb)( running_machine &machine );
49
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    DEVICE CONFIGURATION MACROS
60***************************************************************************/
61
62DECLARE_LEGACY_DEVICE(SMSVDP, smsvdp);
63
64#define MCFG_SMSVDP_ADD(_tag, _interface) \
65   MCFG_DEVICE_ADD(_tag, SMSVDP, 0) \
66   MCFG_DEVICE_CONFIG(_interface)
67
68
69/***************************************************************************
70    DEVICE I/O FUNCTIONS
71***************************************************************************/
72
73/* prototypes */
74
75UINT32 sms_vdp_update( device_t *device, bitmap_t *bitmap, const rectangle *cliprect );
76READ8_DEVICE_HANDLER( sms_vdp_vcount_r );
77READ8_DEVICE_HANDLER( sms_vdp_hcount_latch_r );
78WRITE8_DEVICE_HANDLER( sms_vdp_hcount_latch_w );
79READ8_DEVICE_HANDLER( sms_vdp_data_r );
80WRITE8_DEVICE_HANDLER( sms_vdp_data_w );
81READ8_DEVICE_HANDLER( sms_vdp_ctrl_r );
82WRITE8_DEVICE_HANDLER( sms_vdp_ctrl_w );
83void sms_vdp_set_ggsmsmode( device_t *device, int mode );
84int sms_vdp_check_brightness( device_t *device, int x, int y );
85
86#endif /* __SMSVDP_H__ */
src/mess/drivers/segasms.c
r0r11573
1/******************************************************************************
2 Contributors:
3
4    Marat Fayzullin (MG source)
5    Charles Mac Donald
6    Mathis Rosenhauer
7    Brad Oliver
8    Michael Luong
9
10 To do:
11
12 - PSG control for Game Gear (needs custom SN76489 with stereo output for each channel)
13 - SIO interface for Game Gear (needs netplay, I guess)
14 - SMS lightgun support
15 - LCD persistence emulation for GG
16 - SMS 3D glass support
17
18 The Game Gear SIO and PSG hardware are not emulated but have some
19 placeholders in 'machine/sms.c'
20
21 Changes:
22    Apr 02 - Added raster palette effects for SMS & GG (ML)
23                 - Added sprite collision (ML)
24                 - Added zoomed sprites (ML)
25    May 02 - Fixed paging bug (ML)
26                 - Fixed sprite and tile priority bug (ML)
27                 - Fixed bug #66 (ML)
28                 - Fixed bug #78 (ML)
29                 - try to implement LCD persistence emulation for GG (ML)
30    Jun 10, 02 - Added bios emulation (ML)
31    Jun 12, 02 - Added PAL & NTSC systems (ML)
32    Jun 25, 02 - Added border emulation (ML)
33    Jun 27, 02 - Version bits for Game Gear (bits 6 of port 00) (ML)
34    Nov-Dec, 05 - Numerous cleanups, fixes, updates (WP)
35    Mar, 07 - More cleanups, fixes, mapper additions, etc (WP)
36
37SMS Store Unit memory map for the second CPU:
38
390000-3FFF - BIOS
404000-47FF - RAM
418000      - System Control Register (R/W)
42            Reading:
43            bit7      - ready (0 = ready, 1 = not ready)
44            bit6-bit5 - unknown
45            bit4-bit3 - timer selection bit switches
46            bit2-bit0 - unknown
47            Writing:
48            bit7-bit4 - unknown, maybe led of selected game to set?
49            bit3      - unknown, 1 seems to be written all the time
50            bit2      - unknown, 1 seems to be written all the time
51            bit1      - reset signal for sms cpu, 0 = reset low, 1 = reset high
52            bit0      - which cpu receives interrupt signals, 0 = sms cpu, 1 = controlling cpu
53C000      - Card/Cartridge selction register (W)
54            bit7-bit4 - slot to select
55            bit3      - slot type (0 = cartridge, 1 = card ?)
56            bit2-bit0 - unknown
57C400      - ???? (used once)
58D800      - Selection buttons #1, 1-8 (R)
59DC00      - Selection buttons #2, 9-16 (R)
60
61 ******************************************************************************/
62
63#include "emu.h"
64#include "cpu/z80/z80.h"
65#include "sound/sn76496.h"
66#include "sound/2413intf.h"
67#include "includes/segasms.h"
68#include "video/smsvdp.h"
69#include "imagedev/cartslot.h"
70
71#include "sms1.lh"
72
73#define MASTER_CLOCK_PAL   53203400   /* This might be a tiny bit too low */
74
75
76static ADDRESS_MAP_START( sms1_mem, AS_PROGRAM, 8 )
77   AM_RANGE(0x0000, 0x03ff) AM_ROMBANK("bank1")               /* First 0x0400 part always points to first page */
78   AM_RANGE(0x0400, 0x3fff) AM_ROMBANK("bank2")               /* switchable rom bank */
79   AM_RANGE(0x4000, 0x5fff) AM_ROMBANK("bank3")               /* switchable rom bank */
80   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank4")               /* switchable rom bank */
81   AM_RANGE(0x8000, 0x9fff) AM_READ_BANK("bank5") AM_WRITE(sms_cartram_w)   /* ROM bank / on-cart RAM */
82   AM_RANGE(0xa000, 0xbfff) AM_READ_BANK("bank6") AM_WRITE(sms_cartram2_w)   /* ROM bank / on-cart RAM */
83   AM_RANGE(0xc000, 0xdff7) AM_MIRROR(0x2000) AM_RAM         /* RAM (mirror at 0xE000) */
84   AM_RANGE(0xdff8, 0xdfff) AM_RAM                  /* RAM "underneath" frame registers */
85   AM_RANGE(0xfff8, 0xfffb) AM_READWRITE(sms_sscope_r, sms_sscope_w)   /* 3-D glasses */
86   AM_RANGE(0xfffc, 0xffff) AM_READWRITE(sms_mapper_r, sms_mapper_w)   /* Bankswitch control */
87ADDRESS_MAP_END
88
89static ADDRESS_MAP_START( sms_mem, AS_PROGRAM, 8 )
90   AM_RANGE(0x0000, 0x03ff) AM_ROMBANK("bank1")               /* First 0x0400 part always points to first page */
91   AM_RANGE(0x0400, 0x3fff) AM_ROMBANK("bank2")               /* switchable rom bank */
92   AM_RANGE(0x4000, 0x5fff) AM_ROMBANK("bank3")               /* switchable rom bank */
93   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank4")               /* switchable rom bank */
94   AM_RANGE(0x8000, 0x9fff) AM_READ_BANK("bank5") AM_WRITE(sms_cartram_w)   /* ROM bank / on-cart RAM */
95   AM_RANGE(0xa000, 0xbfff) AM_READ_BANK("bank6") AM_WRITE(sms_cartram2_w)   /* ROM bank / on-cart RAM */
96   AM_RANGE(0xc000, 0xdffb) AM_MIRROR(0x2000) AM_RAM         /* RAM (mirror at 0xE000) */
97   AM_RANGE(0xdffc, 0xdfff) AM_RAM                  /* RAM "underneath" frame registers */
98   AM_RANGE(0xfffc, 0xffff) AM_READWRITE(sms_mapper_r, sms_mapper_w)   /* Bankswitch control */
99ADDRESS_MAP_END
100
101static ADDRESS_MAP_START( sms_store_mem, AS_PROGRAM, 8 )
102   AM_RANGE(0x0000, 0x3fff) AM_ROM                  /* BIOS */
103   AM_RANGE(0x4000, 0x47ff) AM_RAM                  /* RAM */
104   AM_RANGE(0x6000, 0x7fff) AM_ROMBANK("bank10")               /* Cartridge/card peek area */
105   AM_RANGE(0x8000, 0x8000) AM_READWRITE(sms_store_control_r, sms_store_control_w)   /* Control */
106   AM_RANGE(0xc000, 0xc000) AM_READWRITE(sms_store_cart_select_r, sms_store_cart_select_w)    /* cartridge/card slot selector */
107   AM_RANGE(0xd800, 0xd800) AM_READ(sms_store_select1)         /* Game selector port #1 */
108   AM_RANGE(0xdc00, 0xdc00) AM_READ(sms_store_select2)         /* Game selector port #2 */
109ADDRESS_MAP_END
110
111static ADDRESS_MAP_START( sms_io, AS_IO, 8 )
112   ADDRESS_MAP_GLOBAL_MASK(0xff)
113   ADDRESS_MAP_UNMAP_HIGH
114   AM_RANGE(0x00, 0x00) AM_MIRROR(0x3e) AM_WRITE(sms_bios_w)
115   AM_RANGE(0x01, 0x01) AM_MIRROR(0x3e) AM_WRITE(sms_io_control_w)
116   AM_RANGE(0x40, 0x7f)                 AM_READ(sms_count_r)
117   AM_RANGE(0x40, 0x7f)                 AM_DEVWRITE("smsiii", sn76496_w)
118   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
119   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
120   AM_RANGE(0xc0, 0xc0) AM_MIRROR(0x1e) AM_READ(sms_input_port_0_r)
121   AM_RANGE(0xc1, 0xc1) AM_MIRROR(0x1e) AM_READ(sms_input_port_1_r)
122   AM_RANGE(0xe0, 0xe0) AM_MIRROR(0x0e) AM_READ(sms_input_port_0_r)
123   AM_RANGE(0xe1, 0xe1) AM_MIRROR(0x0e) AM_READ(sms_input_port_1_r)
124   AM_RANGE(0xf0, 0xf0)                 AM_READWRITE(sms_input_port_0_r, sms_ym2413_register_port_0_w)
125   AM_RANGE(0xf1, 0xf1)                 AM_READWRITE(sms_input_port_1_r, sms_ym2413_data_port_0_w)
126   AM_RANGE(0xf2, 0xf2)                 AM_READWRITE(sms_fm_detect_r, sms_fm_detect_w)
127   AM_RANGE(0xf3, 0xf3)                 AM_READ(sms_input_port_1_r)
128   AM_RANGE(0xf4, 0xf4) AM_MIRROR(0x02) AM_READ(sms_input_port_0_r)
129   AM_RANGE(0xf5, 0xf5) AM_MIRROR(0x02) AM_READ(sms_input_port_1_r)
130   AM_RANGE(0xf8, 0xf8) AM_MIRROR(0x06) AM_READ(sms_input_port_0_r)
131   AM_RANGE(0xf9, 0xf9) AM_MIRROR(0x06) AM_READ(sms_input_port_1_r)
132ADDRESS_MAP_END
133
134static ADDRESS_MAP_START( gg_io, AS_IO, 8 )
135   ADDRESS_MAP_GLOBAL_MASK(0xff)
136   ADDRESS_MAP_UNMAP_HIGH
137   AM_RANGE(0x00, 0x00)                 AM_READ(gg_input_port_2_r)
138   AM_RANGE(0x01, 0x05)                 AM_READWRITE(gg_sio_r, gg_sio_w)
139   AM_RANGE(0x06, 0x06)                 AM_DEVWRITE("gamegear", sn76496_stereo_w)
140   AM_RANGE(0x07, 0x07)                 AM_WRITE(sms_io_control_w)
141   AM_RANGE(0x08, 0x08) AM_MIRROR(0x06) AM_WRITE(sms_bios_w)
142   AM_RANGE(0x09, 0x09) AM_MIRROR(0x06) AM_WRITE(sms_io_control_w)
143   AM_RANGE(0x10, 0x10) AM_MIRROR(0x0e) AM_WRITE(sms_bios_w)
144   AM_RANGE(0x11, 0x11) AM_MIRROR(0x0e) AM_WRITE(sms_io_control_w)
145   AM_RANGE(0x20, 0x20) AM_MIRROR(0x1e) AM_WRITE(sms_bios_w)
146   AM_RANGE(0x21, 0x21) AM_MIRROR(0x1e) AM_WRITE(sms_io_control_w)
147   AM_RANGE(0x40, 0x7f)                 AM_READ(sms_count_r)
148   AM_RANGE(0x40, 0x7f)                 AM_DEVWRITE("gamegear", sn76496_w)
149   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_data_r, sms_vdp_data_w)
150   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_DEVREADWRITE("sms_vdp", sms_vdp_ctrl_r, sms_vdp_ctrl_w)
151   AM_RANGE(0xc0, 0xc0)                 AM_READ_PORT("PORT_DC")
152   AM_RANGE(0xc1, 0xc1)                 AM_READ_PORT("PORT_DD")
153   AM_RANGE(0xdc, 0xdc)                 AM_READ_PORT("PORT_DC")
154   AM_RANGE(0xdd, 0xdd)                 AM_READ_PORT("PORT_DD")
155ADDRESS_MAP_END
156
157
158static INPUT_PORTS_START( sms )
159   PORT_START("PORT_DC")
160   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
161   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
162   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
163   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_CATEGORY(10) PORT_PLAYER(1) PORT_8WAY
164   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(10) PORT_PLAYER(1)
165   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(10) PORT_PLAYER(1)
166   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
167   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
168
169   PORT_START("PORT_DD")
170   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
171   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_CATEGORY(20) PORT_PLAYER(2) PORT_8WAY
172   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(20) PORT_PLAYER(2)
173   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(20) PORT_PLAYER(2)
174   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNUSED ) /* Software Reset bit */
175   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_UNUSED )
176   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNUSED ) /* Port A TH */
177   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNUSED ) /* Port B TH */
178
179   PORT_START("PAUSE")
180   PORT_BIT( 0x7f, IP_ACTIVE_LOW, IPT_UNUSED )
181   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_START ) PORT_NAME(DEF_STR(Pause))
182
183   PORT_START("LPHASER0")   /* Light phaser X - player 1 */
184   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) PORT_CHANGED(lgun1_changed, NULL)
185
186   PORT_START("LPHASER1")   /* Light phaser Y - player 1 */
187   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) PORT_CHANGED(lgun1_changed, NULL)
188
189   PORT_START("LPHASER2")   /* Light phaser X - player 2 */
190   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) PORT_CHANGED(lgun2_changed, NULL)
191
192   PORT_START("LPHASER3")   /* Light phaser Y - player 2 */
193   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) PORT_CHANGED(lgun2_changed, NULL)
194
195   PORT_START("RFU")   /* Rapid Fire Unit */
196   PORT_CONFNAME( 0x03, 0x00, "Rapid Fire Unit - Player 1" )
197   PORT_CONFSETTING(   0x00, DEF_STR( Off ) )
198   PORT_CONFSETTING(   0x01, "Button A" )
199   PORT_CONFSETTING(   0x02, "Button B" )
200   PORT_CONFSETTING(   0x03, "Button A + B" )
201   PORT_CONFNAME( 0x0c, 0x00, "Rapid Fire Unit - Player 2" )
202   PORT_CONFSETTING(   0x00, DEF_STR( Off ) )
203   PORT_CONFSETTING(   0x04, "Button A" )
204   PORT_CONFSETTING(   0x08, "Button B" )
205   PORT_CONFSETTING(   0x0c, "Button A + B" )
206
207   PORT_START("PADDLE0")   /* Paddle player 1 */
208   PORT_BIT( 0xff, 0x80, IPT_PADDLE) PORT_SENSITIVITY(40) PORT_KEYDELTA(20) PORT_CENTERDELTA(0) PORT_MINMAX(0,255) PORT_CATEGORY(12) PORT_PLAYER(1)
209
210   PORT_START("PADDLE1")   /* Paddle player 2 */
211   PORT_BIT( 0xff, 0x80, IPT_PADDLE) PORT_SENSITIVITY(40) PORT_KEYDELTA(20) PORT_CENTERDELTA(0) PORT_MINMAX(0,255) PORT_CATEGORY(22) PORT_PLAYER(2)
212
213   PORT_START("CTRLIPT")   /* Light Phaser and Paddle Control buttons */
214   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(11) PORT_PLAYER(1)
215   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(12) PORT_PLAYER(1)
216   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(13) PORT_PLAYER(1)
217   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(13) PORT_PLAYER(1)
218   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(21) PORT_PLAYER(2)
219   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(22) PORT_PLAYER(2)
220   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_CATEGORY(23) PORT_PLAYER(2)
221   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_CATEGORY(23) PORT_PLAYER(2)
222
223   PORT_START("CTRLSEL")   /* Controller selection */
224   PORT_CATEGORY_CLASS( 0x0f, 0x00, "Player 1 Controller" )
225   PORT_CATEGORY_ITEM( 0x00, DEF_STR( Joystick ), 10 )
226   PORT_CATEGORY_ITEM( 0x01, "Light Phaser", 11 )
227   PORT_CATEGORY_ITEM( 0x02, "Sega Paddle Control", 12 )
228   PORT_CATEGORY_ITEM( 0x03, "Sega Sports Pad", 13 )
229   PORT_CATEGORY_CLASS( 0xf0, 0x00, "Player 2 Controller" )
230   PORT_CATEGORY_ITEM( 0x00, DEF_STR( Joystick ), 20 )
231   PORT_CATEGORY_ITEM( 0x10, "Light Phaser", 21 )
232   PORT_CATEGORY_ITEM( 0x20, "Sega Paddle Control", 22 )
233   PORT_CATEGORY_ITEM( 0x30, "Sega Sports Pad", 23 )
234
235   PORT_START("SPORT0")   /* Player 1 Sports Pad X axis */
236   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(13) PORT_PLAYER(1)
237
238   PORT_START("SPORT1")   /* Player 1 Sports Pad Y axis */
239   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_Y ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(13) PORT_PLAYER(1)
240
241   PORT_START("SPORT2")   /* Player 2 Sports Pad X axis */
242   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(23) PORT_PLAYER(2)
243
244   PORT_START("SPORT3")   /* Player 2 Sports Pad Y axis */
245   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_Y ) PORT_SENSITIVITY(50) PORT_KEYDELTA(40) PORT_RESET PORT_REVERSE PORT_CATEGORY(23) PORT_PLAYER(2)
246INPUT_PORTS_END
247
248static INPUT_PORTS_START( sms1 )
249   PORT_INCLUDE( sms )
250
251   PORT_START("RESET")
252   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SERVICE1 ) PORT_NAME("Reset Button")
253
254   PORT_START("SEGASCOPE")
255   PORT_CONFNAME( 0x01, 0x00, "SegaScope (3-D Glasses)" )
256   PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
257   PORT_CONFSETTING( 0x01, DEF_STR( On ) )
258
259   PORT_START("SSCOPE_BINOCULAR")
260   PORT_CONFNAME( 0x03, 0x00, "SegaScope - Binocular Hack" ) PORT_CONDITION("SEGASCOPE", 0x01, PORTCOND_EQUALS, 0x01)
261   PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
262   PORT_CONFSETTING( 0x01, "Left Lens" )
263   PORT_CONFSETTING( 0x02, "Right Lens" )
264   PORT_CONFSETTING( 0x03, "Both Lens" )
265   PORT_BIT( 0x03, 0x00, IPT_UNUSED ) PORT_CONDITION("SEGASCOPE", 0x01, PORTCOND_EQUALS, 0x00)
266INPUT_PORTS_END
267
268static INPUT_PORTS_START( smsj )
269   PORT_INCLUDE( sms1 )
270
271   PORT_START("TVDRAW")
272   PORT_CONFNAME( 0x01, 0x00, "Terebi Oekaki Graphics Tablet" )
273   PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
274   PORT_CONFSETTING( 0x01, DEF_STR( On ) )
275
276   PORT_START("TVDRAW_X")
277   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_NAME("Tablet - X Axis") PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(10) PORT_PLAYER(1)
278      PORT_CONDITION("TVDRAW", 0x01, PORTCOND_EQUALS, 0x01)
279
280   PORT_START("TVDRAW_Y")
281   PORT_BIT( 0xff, 0x60, IPT_LIGHTGUN_Y ) PORT_NAME("Tablet - Y Axis") PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_MINMAX(0, 191) PORT_SENSITIVITY(50) PORT_KEYDELTA(10) PORT_PLAYER(1)
282      PORT_CONDITION("TVDRAW", 0x01, PORTCOND_EQUALS, 0x01)
283
284   PORT_START("TVDRAW_PEN")
285   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("Tablet - Pen") PORT_CONDITION("TVDRAW", 0x01, PORTCOND_EQUALS, 0x01)
286INPUT_PORTS_END
287
288static INPUT_PORTS_START( gg )
289   PORT_START("PORT_DC")
290   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(1) PORT_8WAY
291   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(1) PORT_8WAY
292   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_PLAYER(1) PORT_8WAY
293   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(1) PORT_8WAY
294   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1)
295   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1)
296   PORT_BIT( 0xc0, IP_ACTIVE_LOW, IPT_UNUSED )
297
298   PORT_START("PORT_DD")
299   PORT_BIT( 0xff, IP_ACTIVE_LOW, IPT_UNUSED )
300
301   PORT_START("START")
302   PORT_BIT( 0x7f, IP_ACTIVE_LOW, IPT_UNUSED )
303   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_START ) PORT_NAME("Start") /* Game Gear START */
304INPUT_PORTS_END
305
306static PALETTE_INIT( sms )
307{
308   int i;
309   for (i = 0; i < 64; i++)
310   {
311      int r = i & 0x03;
312      int g = (i & 0x0c) >> 2;
313      int b = (i & 0x30) >> 4;
314      palette_set_color_rgb(machine, i, r << 6, g << 6, b << 6);
315   }
316   /* TMS9918 palette */
317   palette_set_color_rgb(machine, 64+ 0,   0,   0,   0);
318   palette_set_color_rgb(machine, 64+ 1,   0,   0,   0);
319   palette_set_color_rgb(machine, 64+ 2,  33, 200,  66);
320   palette_set_color_rgb(machine, 64+ 3,  94, 220, 120);
321   palette_set_color_rgb(machine, 64+ 4,  84,  85, 237);
322   palette_set_color_rgb(machine, 64+ 5, 125, 118, 252);
323   palette_set_color_rgb(machine, 64+ 6, 212,  82,  77);
324   palette_set_color_rgb(machine, 64+ 7,  66, 235, 245);
325   palette_set_color_rgb(machine, 64+ 8, 252,  85,  84);
326   palette_set_color_rgb(machine, 64+ 9, 255, 121, 120);
327   palette_set_color_rgb(machine, 64+10, 212, 193,  84);
328   palette_set_color_rgb(machine, 64+11, 230, 206, 128);
329   palette_set_color_rgb(machine, 64+12,  33, 176,  59);
330   palette_set_color_rgb(machine, 64+13, 201,  91, 186);
331   palette_set_color_rgb(machine, 64+14, 204, 204, 204);
332   palette_set_color_rgb(machine, 64+15, 255, 255, 255);
333}
334
335static PALETTE_INIT( gamegear )
336{
337   int i;
338   for (i = 0; i < 4096; i++)
339   {
340      int r = i & 0x000f;
341      int g = (i & 0x00f0) >> 4;
342      int b = (i & 0x0f00) >> 8;
343      palette_set_color_rgb(machine, i, r << 4, g << 4, b << 4);
344   }
345}
346
347
348
349static void sms_int_callback( running_machine &machine, int state )
350{
351   cputag_set_input_line(machine, "maincpu", 0, state);
352}
353
354static const smsvdp_interface _315_5124_intf =
355{
356   MODEL_315_5124,
357   sms_int_callback,
358   sms_pause_callback
359};
360
361static const smsvdp_interface _315_5246_intf =
362{
363   MODEL_315_5246,
364   sms_int_callback,
365   sms_pause_callback
366};
367
368static const smsvdp_interface _315_5378_intf =
369{
370   MODEL_315_5378,
371   sms_int_callback,
372   sms_pause_callback
373};
374
375static const smsvdp_interface sms_store_intf =
376{
377   MODEL_315_5124,
378   sms_store_int_callback,
379   sms_pause_callback
380};
381
382static MACHINE_CONFIG_FRAGMENT( sms_cartslot )
383   MCFG_CARTSLOT_ADD("cart1")
384   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin")
385   MCFG_CARTSLOT_NOT_MANDATORY
386   MCFG_CARTSLOT_INTERFACE("sms_cart")
387   MCFG_CARTSLOT_START(sms_cart)
388   MCFG_CARTSLOT_LOAD(sms_cart)
389
390   MCFG_SOFTWARE_LIST_ADD("cart_list","sms")
391MACHINE_CONFIG_END
392
393static MACHINE_CONFIG_FRAGMENT( gg_cartslot )
394   MCFG_CARTSLOT_ADD("cart1")
395   MCFG_CARTSLOT_EXTENSION_LIST("gg,bin")
396   MCFG_CARTSLOT_MANDATORY
397   MCFG_CARTSLOT_INTERFACE("gamegear_cart")
398   MCFG_CARTSLOT_START(sms_cart)
399   MCFG_CARTSLOT_LOAD(sms_cart)
400
401   MCFG_SOFTWARE_LIST_ADD("cart_list","gamegear")
402MACHINE_CONFIG_END
403
404static MACHINE_CONFIG_START( sms_ntsc_base, sms_state )
405   /* basic machine hardware */
406   MCFG_CPU_ADD("maincpu", Z80, XTAL_53_693175MHz/15)
407   MCFG_CPU_PROGRAM_MAP(sms_mem)
408   MCFG_CPU_IO_MAP(sms_io)
409
410   MCFG_QUANTUM_TIME(attotime::from_hz(60))
411
412   MCFG_MACHINE_START(sms)
413   MCFG_MACHINE_RESET(sms)
414
415   /* sound hardware */
416   MCFG_SPEAKER_STANDARD_MONO("mono")
417   MCFG_SOUND_ADD("smsiii", SMSIII, XTAL_53_693175MHz/15)
418   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
419
420   MCFG_FRAGMENT_ADD( sms_cartslot )
421MACHINE_CONFIG_END
422
423static MACHINE_CONFIG_DERIVED( sms2_ntsc, sms_ntsc_base )
424   /* video hardware */
425   MCFG_SCREEN_ADD("screen", RASTER)
426   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
427   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
428   MCFG_SCREEN_UPDATE(sms)
429
430   MCFG_PALETTE_LENGTH(64+16)
431   MCFG_PALETTE_INIT(sms)
432
433   MCFG_SMSVDP_ADD("sms_vdp", _315_5246_intf)
434MACHINE_CONFIG_END
435
436static MACHINE_CONFIG_DERIVED( sms1_ntsc, sms_ntsc_base )
437
438   MCFG_CPU_MODIFY("maincpu")
439   MCFG_CPU_PROGRAM_MAP(sms1_mem)   // This adds the SegaScope handlers for 3-D glasses
440   MCFG_CPU_IO_MAP(sms_io)
441
442   /* video hardware */
443   MCFG_SCREEN_ADD("screen", RASTER)
444   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
445   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
446   MCFG_SCREEN_UPDATE(sms1)
447
448   MCFG_SCREEN_ADD("left_lcd", LCD)   // This is needed for SegaScope Left LCD
449   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
450   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
451   MCFG_SCREEN_UPDATE(sms1)
452
453   MCFG_SCREEN_ADD("right_lcd", LCD)   // This is needed for SegaScope Right LCD
454   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
455   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, NTSC_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS, TBORDER_START + NTSC_224_TBORDER_Y_PIXELS + 224)
456   MCFG_SCREEN_UPDATE(sms1)
457
458   MCFG_DEFAULT_LAYOUT(layout_sms1)
459
460   MCFG_PALETTE_LENGTH(64+16)
461   MCFG_PALETTE_INIT(sms)
462
463   MCFG_VIDEO_START(sms1)
464
465   MCFG_SMSVDP_ADD("sms_vdp", _315_5124_intf)
466MACHINE_CONFIG_END
467
468#define MCFG_SMSSDISP_CARTSLOT_ADD(_tag) \
469   MCFG_CARTSLOT_ADD(_tag) \
470   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin") \
471   MCFG_CARTSLOT_NOT_MANDATORY \
472   MCFG_CARTSLOT_INTERFACE("sms_cart") \
473   MCFG_CARTSLOT_START(sms_cart) \
474   MCFG_CARTSLOT_LOAD(sms_cart)
475
476static MACHINE_CONFIG_DERIVED( sms_sdisp, sms2_ntsc )
477
478   MCFG_DEVICE_REMOVE("sms_vdp")
479   MCFG_SMSVDP_ADD("sms_vdp", sms_store_intf)
480
481   MCFG_CPU_ADD("control", Z80, XTAL_53_693175MHz/15)
482   MCFG_CPU_PROGRAM_MAP(sms_store_mem)
483   /* Both CPUs seem to communicate with the VDP etc? */
484   MCFG_CPU_IO_MAP(sms_io)
485
486   MCFG_CARTSLOT_MODIFY("cart1")
487   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin")
488   MCFG_CARTSLOT_MANDATORY
489   MCFG_CARTSLOT_INTERFACE("sms_cart")
490   MCFG_CARTSLOT_START(sms_cart)
491   MCFG_CARTSLOT_LOAD(sms_cart)
492
493   MCFG_SMSSDISP_CARTSLOT_ADD("cart2")
494   MCFG_SMSSDISP_CARTSLOT_ADD("cart3")
495   MCFG_SMSSDISP_CARTSLOT_ADD("cart4")
496   MCFG_SMSSDISP_CARTSLOT_ADD("cart5")
497   MCFG_SMSSDISP_CARTSLOT_ADD("cart6")
498   MCFG_SMSSDISP_CARTSLOT_ADD("cart7")
499   MCFG_SMSSDISP_CARTSLOT_ADD("cart8")
500   MCFG_SMSSDISP_CARTSLOT_ADD("cart9")
501   MCFG_SMSSDISP_CARTSLOT_ADD("cart10")
502   MCFG_SMSSDISP_CARTSLOT_ADD("cart11")
503   MCFG_SMSSDISP_CARTSLOT_ADD("cart12")
504   MCFG_SMSSDISP_CARTSLOT_ADD("cart13")
505   MCFG_SMSSDISP_CARTSLOT_ADD("cart14")
506   MCFG_SMSSDISP_CARTSLOT_ADD("cart15")
507   MCFG_SMSSDISP_CARTSLOT_ADD("cart16")
508MACHINE_CONFIG_END
509
510static MACHINE_CONFIG_START( sms_pal_base, sms_state )
511   /* basic machine hardware */
512   MCFG_CPU_ADD("maincpu", Z80, MASTER_CLOCK_PAL/15)
513   MCFG_CPU_PROGRAM_MAP(sms_mem)
514   MCFG_CPU_IO_MAP(sms_io)
515
516   MCFG_QUANTUM_TIME(attotime::from_hz(60))
517
518   MCFG_MACHINE_START(sms)
519   MCFG_MACHINE_RESET(sms)
520
521   /* sound hardware */
522   MCFG_SPEAKER_STANDARD_MONO("mono")
523   MCFG_SOUND_ADD("smsiii", SMSIII, MASTER_CLOCK_PAL/15)
524   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
525
526   MCFG_FRAGMENT_ADD( sms_cartslot )
527MACHINE_CONFIG_END
528
529static MACHINE_CONFIG_DERIVED( sms2_pal, sms_pal_base )
530
531   /* video hardware */
532   MCFG_SCREEN_ADD("screen", RASTER)
533   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
534   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
535   MCFG_SCREEN_UPDATE(sms)
536
537   MCFG_PALETTE_LENGTH(64+16)
538   MCFG_PALETTE_INIT(sms)
539
540   MCFG_SMSVDP_ADD("sms_vdp", _315_5246_intf)
541MACHINE_CONFIG_END
542
543static MACHINE_CONFIG_DERIVED( sms1_pal, sms_pal_base )
544
545   MCFG_CPU_MODIFY("maincpu")
546   MCFG_CPU_PROGRAM_MAP(sms1_mem)   // This adds the SegaScope handlers for 3-D glasses
547   MCFG_CPU_IO_MAP(sms_io)
548
549   /* video hardware */
550   MCFG_SCREEN_ADD("screen", RASTER)
551   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
552   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
553   MCFG_SCREEN_UPDATE(sms1)
554
555   MCFG_SCREEN_ADD("left_lcd", LCD)   // This is needed for SegaScope Left LCD
556   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
557   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
558   MCFG_SCREEN_UPDATE(sms1)
559
560   MCFG_SCREEN_ADD("right_lcd", LCD)   // This is needed for SegaScope Right LCD
561   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
562   MCFG_SCREEN_RAW_PARAMS(MASTER_CLOCK_PAL/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS - 2, LBORDER_START + LBORDER_X_PIXELS + 256 + 10, PAL_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS, TBORDER_START + PAL_240_TBORDER_Y_PIXELS + 240)
563   MCFG_SCREEN_UPDATE(sms1)
564
565   MCFG_PALETTE_LENGTH(64+16)
566   MCFG_PALETTE_INIT(sms)
567
568   MCFG_DEFAULT_LAYOUT(layout_sms1)
569
570   MCFG_SMSVDP_ADD("sms_vdp", _315_5124_intf)
571MACHINE_CONFIG_END
572
573static MACHINE_CONFIG_DERIVED( sms_fm, sms1_ntsc )
574
575   MCFG_SOUND_ADD("ym2413", YM2413, XTAL_53_693175MHz/15)
576   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
577MACHINE_CONFIG_END
578
579static MACHINE_CONFIG_DERIVED( sg1000m3, sms_fm )
580
581   MCFG_CARTSLOT_MODIFY("cart1")
582   MCFG_CARTSLOT_EXTENSION_LIST("sms,bin,sg")
583   MCFG_CARTSLOT_MANDATORY
584   MCFG_CARTSLOT_START(sms_cart)
585   MCFG_CARTSLOT_LOAD(sms_cart)
586MACHINE_CONFIG_END
587
588static MACHINE_CONFIG_DERIVED( sms2_fm, sms2_ntsc )
589
590   MCFG_SOUND_ADD("ym2413", YM2413, XTAL_53_693175MHz/15)
591   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
592MACHINE_CONFIG_END
593
594static MACHINE_CONFIG_START( gamegear, sms_state )
595   /* basic machine hardware */
596   MCFG_CPU_ADD("maincpu", Z80, XTAL_53_693175MHz/15)
597   MCFG_CPU_PROGRAM_MAP(sms_mem)
598   MCFG_CPU_IO_MAP(gg_io)
599
600   MCFG_QUANTUM_TIME(attotime::from_hz(60))
601
602   MCFG_MACHINE_START(sms)
603   MCFG_MACHINE_RESET(sms)
604
605   /* video hardware */
606   MCFG_SCREEN_ADD("screen", LCD)
607   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
608   MCFG_SCREEN_RAW_PARAMS(XTAL_53_693175MHz/10, SMS_X_PIXELS, LBORDER_START + LBORDER_X_PIXELS + 6*8, LBORDER_START + LBORDER_X_PIXELS + 26*8, NTSC_Y_PIXELS, TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 3*8, TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 21*8 )
609   MCFG_SCREEN_UPDATE(gamegear)
610
611   MCFG_PALETTE_LENGTH(4096)
612   MCFG_PALETTE_INIT(gamegear)
613
614   MCFG_VIDEO_START(gamegear)
615
616   MCFG_SMSVDP_ADD("sms_vdp", _315_5378_intf)
617
618   /* sound hardware */
619   MCFG_SPEAKER_STANDARD_STEREO("lspeaker","rspeaker")
620   MCFG_SOUND_ADD("gamegear", GAMEGEAR, XTAL_53_693175MHz/15)
621   MCFG_SOUND_ROUTE(0, "lspeaker", 1.00)
622   MCFG_SOUND_ROUTE(1, "rspeaker", 1.00)
623
624   /* cartridge */
625   MCFG_FRAGMENT_ADD( gg_cartslot )
626MACHINE_CONFIG_END
627
628
629ROM_START(sms1)
630   ROM_REGION(0x4000, "maincpu", 0)
631   ROM_FILL(0x0000, 0x4000, 0xff)
632
633   ROM_REGION(0x20000, "user1", 0)
634   ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
635   ROMX_LOAD("bios13fx.rom", 0x0000, 0x2000, CRC(0072ED54) SHA1(c315672807d8ddb8d91443729405c766dd95cae7), ROM_BIOS(1))
636   ROM_SYSTEM_BIOS( 1, "hangonsh", "US/European BIOS v2.4 with Hang On and Safari Hunt (1988)" )
637   ROMX_LOAD("hshbios.rom", 0x0000, 0x20000, CRC(91E93385) SHA1(9e179392cd416af14024d8f31c981d9ee9a64517), ROM_BIOS(2))
638   ROM_SYSTEM_BIOS( 2, "hangon", "US/European BIOS v3.4 with Hang On (1988)" )
639   ROMX_LOAD("hangbios.rom", 0x0000, 0x20000, CRC(8EDF7AC6) SHA1(51fd6d7990f62cd9d18c9ecfc62ed7936169107e), ROM_BIOS(3))
640   ROM_SYSTEM_BIOS( 3, "missiled", "US/European BIOS v4.4 with Missile Defense 3D (1988)" )
641   ROMX_LOAD("missiled.rom", 0x0000, 0x20000, CRC(E79BB689) SHA1(aa92ae576ca670b00855e278378d89e9f85e0351), ROM_BIOS(4))
642   ROM_SYSTEM_BIOS( 4, "proto", "US Master System Prototype BIOS" )
643   ROMX_LOAD("m404prot.rom", 0x0000, 0x2000, CRC(1a15dfcc) SHA1(4a06c8e66261611dce0305217c42138b71331701), ROM_BIOS(5))
644ROM_END
645
646ROM_START(sms)
647   ROM_REGION(0x4000, "maincpu", 0)
648   ROM_FILL(0x0000, 0x4000, 0xff)
649
650   ROM_REGION(0x20000, "user1", 0)
651   ROM_SYSTEM_BIOS( 0, "alexkidd", "US/European BIOS with Alex Kidd in Miracle World (1990)" )
652   ROMX_LOAD("akbios.rom", 0x0000, 0x20000, CRC(CF4A09EA) SHA1(3af7b66248d34eb26da40c92bf2fa4c73a46a051), ROM_BIOS(1))
653ROM_END
654
655ROM_START(smssdisp)
656   ROM_REGION(0x4000, "maincpu", 0)
657   ROM_FILL(0x0000, 0x4000, 0x00)
658
659   ROM_REGION(0x4000, "user1", 0)
660   ROM_FILL(0x0000, 0x4000, 0xff)
661
662   ROM_REGION(0x4000, "control", 0)
663   ROM_LOAD("smssdisp.rom", 0x0000, 0x4000, CRC(ee2c29ba) SHA1(fc465122134d95363112eb51b9ab71db3576cefd))
664ROM_END
665
666ROM_START(sms1pal)
667   ROM_REGION(0x4000, "maincpu", 0)
668   ROM_FILL(0x0000, 0x4000, 0xff)
669
670   ROM_REGION(0x20000, "user1", 0)
671   ROM_SYSTEM_BIOS( 0, "bios13", "US/European BIOS v1.3 (1986)" )
672   ROMX_LOAD("bios13fx.rom", 0x0000, 0x2000, CRC(0072ED54) SHA1(c315672807d8ddb8d91443729405c766dd95cae7), ROM_BIOS(1))
673   ROM_SYSTEM_BIOS( 1, "hangonsh", "US/European BIOS v2.4 with Hang On and Safari Hunt (1988)" )
674   ROMX_LOAD("hshbios.rom", 0x0000, 0x20000, CRC(91E93385) SHA1(9e179392cd416af14024d8f31c981d9ee9a64517), ROM_BIOS(2))
675   ROM_SYSTEM_BIOS( 2, "hangon", "Sega Master System - US/European BIOS v3.4 with Hang On (1988)" )
676   ROMX_LOAD("hangbios.rom", 0x0000, 0x20000, CRC(8EDF7AC6) SHA1(51fd6d7990f62cd9d18c9ecfc62ed7936169107e), ROM_BIOS(3))
677   ROM_SYSTEM_BIOS( 3, "missiled", "US/European BIOS v4.4 with Missile Defense 3D (1988)" )
678   ROMX_LOAD("missiled.rom", 0x0000, 0x20000, CRC(E79BB689) SHA1(aa92ae576ca670b00855e278378d89e9f85e0351), ROM_BIOS(4))
679ROM_END
680
681ROM_START(smspal)
682   ROM_REGION(0x4000, "maincpu", 0)
683   ROM_FILL(0x0000, 0x4000, 0xff)
684
685   ROM_REGION(0x40000, "user1", 0)
686   ROM_SYSTEM_BIOS( 0, "alexkidd", "US/European BIOS with Alex Kidd in Miracle World (1990)" )
687   ROMX_LOAD("akbios.rom", 0x0000, 0x20000, CRC(CF4A09EA) SHA1(3af7b66248d34eb26da40c92bf2fa4c73a46a051), ROM_BIOS(1))
688   ROM_SYSTEM_BIOS( 1, "sonic", "European/Brazilian BIOS with Sonic the Hedgehog (1991)" )
689   ROMX_LOAD("sonbios.rom", 0x0000, 0x40000, CRC(81C3476B) SHA1(6aca0e3dffe461ba1cb11a86cd4caf5b97e1b8df), ROM_BIOS(2))
690ROM_END
691
692ROM_START(sg1000m3)
693   ROM_REGION(0x4000, "maincpu", 0)
694   ROM_FILL(0x0000, 0x4000, 0x00)
695ROM_END
696
697ROM_START(smsj)
698   ROM_REGION(0x4000, "maincpu", 0)
699   ROM_FILL(0x0000, 0x4000, 0xff)
700
701   ROM_REGION(0x4000, "user1", 0)
702   ROM_SYSTEM_BIOS( 0, "jbios21", "Japanese BIOS v2.1 (1987)" )
703   ROMX_LOAD("jbios21.rom", 0x0000, 0x2000, CRC(48D44A13) SHA1(a8c1b39a2e41137835eda6a5de6d46dd9fadbaf2), ROM_BIOS(1))
704ROM_END
705
706ROM_START(sms2kr)
707   ROM_REGION(0x4000, "maincpu", 0)
708   ROM_FILL(0x0000, 0x4000, 0xff)
709
710   ROM_REGION(0x20000, "user1", 0)
711   ROM_SYSTEM_BIOS( 0, "akbioskr", "Samsung Gam*Boy II with Alex Kidd in Miracle World (1990)" )
712   ROMX_LOAD("akbioskr.rom", 0x000, 0x20000, CRC(9c5bad91) SHA1(2feafd8f1c40fdf1bd5668f8c5c02e5560945b17), ROM_BIOS(1))
713ROM_END
714
715ROM_START(gamegear)
716   ROM_REGION(0x4000, "maincpu", 0)
717   ROM_FILL(0x0000, 0x4000, 0x00)
718
719   ROM_REGION(0x0400, "user1", 0)
720   ROM_SYSTEM_BIOS( 0, "none", "No BIOS" ) /* gamegear */
721   ROM_SYSTEM_BIOS( 1, "majesco", "Majesco BIOS" ) /* gamg */
722   ROMX_LOAD("majbios.rom", 0x0000, 0x0400, CRC(0EBEA9D4) SHA1(914aa165e3d879f060be77870d345b60cfeb4ede), ROM_BIOS(2))
723ROM_END
724
725#define rom_gamegeaj rom_gamegear
726
727/***************************************************************************
728
729  Game driver(s)
730
731  US
732   - Sega Master System I (sms1)
733     - prototype bios - 1986
734     - without built-in game v1.3 - 1986
735     - built-in Hang On/Safari Hunt v2.4 - 1988
736     - built-in Hang On v3.4 - 1988
737     - built-in Missile Defense 3-D v4.4 - 1988
738     - built-in Hang On/Astro Warrior ????
739   - Sega Master System II (sms/sms2)
740     - built-in Alex Kidd in Miracle World - 1990
741
742  JP
743   - Sega SG-1000 Mark III (smsm3)
744     - no bios
745   - Sega Master System (I) (smsj)
746     - without built-in game v2.1 - 1987
747
748  KR
749   - Sega Master System II (sms2kr)
750     - built-in Alex Kidd in Miracle World (Korean)
751
752  EU
753   - Sega Master System I (sms1pal)
754     - without built-in game v1.3 - 1986
755     - built-in Hang On/Safari Hunt v2.4 - 1988
756     - built-in Hang On v3.4 - 1988
757     - built-in Missile Defense 3-D v4.4 - 1988
758     - built-in Hang On/Astro Warrior ????
759   - Sega Master System II (sms2pal)
760     - built-in Alex Kidd in Miracle World - 1990
761     - built-in Sonic the Hedgehog - 1991
762
763  BR
764   - Sega Master System I - 1987
765   - Sega Master System II???
766   - Sega Master System III - Tec Toy, 1987
767   - Sega Master System Compact - Tec Toy, 1992
768   - Sega Master System Girl - Tec Toy, 1992
769
770***************************************************************************/
771
772/*    YEAR  NAME        PARENT      COMPAT  MACHINE      INPUT   INIT      COMPANY     FULLNAME                            FLAGS */
773CONS( 1984, sg1000m3,   sms,        0,      sg1000m3,    smsj,   sg1000m3, "Sega",     "SG-1000 Mark III",                 0 )
774CONS( 1986, sms1,       sms,        0,      sms1_ntsc,   sms1,   sms1,     "Sega",     "Master System I",                  0 )
775CONS( 1986, sms1pal,    sms,        0,      sms1_pal,    sms1,   sms1,     "Sega",     "Master System I (PAL)" ,           0 )
776CONS( 1986, smssdisp,   sms,        0,      sms_sdisp,   sms,    smssdisp, "Sega",     "Master System Store Display Unit", GAME_NOT_WORKING )
777CONS( 1987, smsj,       sms,        0,      sms_fm,      smsj,   smsj,     "Sega",     "Master System (Japan)",            0 )
778CONS( 1990, sms,        0,          0,      sms2_ntsc,   sms,    sms1,     "Sega",     "Master System II",                 0 )
779CONS( 1990, smspal,     sms,        0,      sms2_pal,    sms,    sms1,     "Sega",     "Master System II (PAL)",           0 )
780CONS( 1990, sms2kr,     sms,        0,      sms2_fm,     sms,    sms2kr,   "Samsung",  "Gam*Boy II (Korea)",               0 )
781
782CONS( 1990, gamegear,   0,          sms,    gamegear,    gg,     gamegear, "Sega",     "Game Gear (Europe/America)",       0 )
783CONS( 1990, gamegeaj,   gamegear,   0,      gamegear,    gg,     gamegeaj, "Sega",     "Game Gear (Japan)",                0 )
src/mess/mess.mak
r11572r11573
493493   $(MAME_DRIVERS)/vectrex.o   \
494494   $(MAME_VIDEO)/vectrex.o      \
495495   $(MAME_MACHINE)/vectrex.o   \
496   $(MAME_VIDEO)/smsvdp.o      \
497   $(MAME_MACHINE)/segasms.o   \
498   $(MAME_DRIVERS)/segasms.o   \
496   $(MESS_VIDEO)/smsvdp.o      \
497   $(MESS_MACHINE)/segasms.o   \
498   $(MESS_DRIVERS)/segasms.o   \
499499   $(MESS_DRIVERS)/sg1000.o   \
500500   $(MAME_DRIVERS)/saturn.o   \
501501   $(MAME_DRIVERS)/cps1.o   \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team