Previous 15994 Revisions Next

r9580 Sunday 14th November, 2010 at 12:05:03 UTC by Fabio Priuli
[SMS/GG] converted to use driver data class
[src/mess/drivers]sms.c
[src/mess/includes]sms.h
[src/mess/machine]sms.c

src/mess/machine/sms.c
r9579r9580
1717#define CF_ONCART_RAM            0x20
1818#define CF_GG_SMS_MODE           0x40
1919
20#define MAX_CARTRIDGES        16
21
2220#define LGUN_RADIUS           6
2321#define LGUN_X_INTERVAL       4
2422
25typedef struct _sms_driver_data sms_driver_data;
26struct _sms_driver_data {
27   UINT8 bios_page_count;
28   UINT8 fm_detect;
29   UINT8 ctrl_reg;
30   int paused;
31   UINT8 bios_port;
32   UINT8 *BIOS;
33   UINT8 *mapper_ram;
34   UINT8 mapper[4];
35   // we are going to use 1-6, same as bank numbers. Notice, though, that most mappers
36   // only work on 16K banks and, hence, banks 4-6 are not always directly set
37   // (they often use bank3 + 0x2000 and bank5 + 0x2000)
38   UINT8 *banking_bios[7];
39   UINT8 *banking_cart[7];
40   UINT8 *banking_none[7];
41   UINT8 gg_sio[5];
42   UINT8 store_control;
43   UINT8 input_port0;
44   UINT8 input_port1;
4523
46   // for gamegear LCD persistence hack
47   bitmap_t *tmp_bitmap;
48   bitmap_t *prev_bitmap;
49
50   // for 3D glass binocular hack
51   bitmap_t *prevleft_bitmap;
52   bitmap_t *prevright_bitmap;
53   
54   /* Model identifiers */
55   UINT8 is_gamegear;
56   UINT8 is_region_japan;
57   UINT8 has_bios_0400;
58   UINT8 has_bios_2000;
59   UINT8 has_bios_full;
60   UINT8 has_bios;
61   UINT8 has_fm;
62
63   /* Data needed for Rapid Fire Unit support */
64   emu_timer *rapid_fire_timer;
65   UINT8 rapid_fire_state_1;
66   UINT8 rapid_fire_state_2;
67
68   /* Data needed for Paddle Control controller */
69   UINT32 last_paddle_read_time;
70   UINT8 paddle_read_state;
71
72   /* Data needed for Sports Pad controller */
73   UINT32 last_sports_pad_time_1;
74   UINT32 last_sports_pad_time_2;
75   UINT8 sports_pad_state_1;
76   UINT8 sports_pad_state_2;
77   UINT8 sports_pad_last_data_1;
78   UINT8 sports_pad_last_data_2;
79   UINT8 sports_pad_1_x;
80   UINT8 sports_pad_1_y;
81   UINT8 sports_pad_2_x;
82   UINT8 sports_pad_2_y;
83
84   /* Data needed for Light Phaser */
85   emu_timer *lphaser_1_timer;
86   emu_timer *lphaser_2_timer;
87   UINT8 lphaser_1_latch;
88   UINT8 lphaser_2_latch;
89   int lphaser_x_offs;   /* Needed to 'calibrate' lphaser; set at cart loading */
90
91   /* Data needed for SegaScope (3D glasses) */
92   UINT8 sscope_state;
93
94   /* Data needed for Terebi Oekaki (TV Draw) */
95   UINT8 tvdraw_data;
96
97   /* Cartridge slot info */
98   UINT8 current_cartridge;
99   struct
100   {
101      UINT8 *ROM;        /* Pointer to ROM image data */
102      UINT32 size;       /* Size of the ROM image */
103      UINT8 features;    /* on-cartridge special hardware */
104      UINT8 *cartSRAM;   /* on-cartridge SRAM */
105      UINT8 sram_save;   /* should be the contents of the on-cartridge SRAM be saved */
106      UINT8 *cartRAM;    /* additional on-cartridge RAM (64KB for Ernie Els Golf) */
107      UINT32 ram_size;   /* size of the on-cartridge RAM */
108      UINT8 ram_page;    /* currently swapped in cartridge RAM */
109   } cartridge[MAX_CARTRIDGES];
110};
111
112static sms_driver_data sms_state;
113
114
11524static void setup_rom(address_space *space);
11625
11726
11827static TIMER_CALLBACK( rapid_fire_callback )
11928{
120   sms_state.rapid_fire_state_1 ^= 0xff;
121   sms_state.rapid_fire_state_2 ^= 0xff;
29   sms_state *state = machine->driver_data<sms_state>();
30   state->rapid_fire_state_1 ^= 0xff;
31   state->rapid_fire_state_2 ^= 0xff;
12232}
12333
12434
12535static WRITE8_HANDLER( sms_input_write )
12636{
37   sms_state *state = space->machine->driver_data<sms_state>();
38
12739   switch (offset)
12840   {
12941   case 0:
13042      switch (input_port_read_safe(space->machine, "CTRLSEL", 0x00) & 0x0f)
13143      {
13244      case 0x03:   /* Sports Pad */
133         if (data != sms_state.sports_pad_last_data_1)
45         if (data != state->sports_pad_last_data_1)
13446         {
13547            UINT32 cpu_cycles = downcast<cpu_device *>(space->cpu)->total_cycles();
13648
137            sms_state.sports_pad_last_data_1 = data;
138            if (cpu_cycles - sms_state.last_sports_pad_time_1 > 512)
49            state->sports_pad_last_data_1 = data;
50            if (cpu_cycles - state->last_sports_pad_time_1 > 512)
13951            {
140               sms_state.sports_pad_state_1 = 3;
141               sms_state.sports_pad_1_x = input_port_read(space->machine, "SPORT0");
142               sms_state.sports_pad_1_y = input_port_read(space->machine, "SPORT1");
52               state->sports_pad_state_1 = 3;
53               state->sports_pad_1_x = input_port_read(space->machine, "SPORT0");
54               state->sports_pad_1_y = input_port_read(space->machine, "SPORT1");
14355            }
144            sms_state.last_sports_pad_time_1 = cpu_cycles;
145            sms_state.sports_pad_state_1 = (sms_state.sports_pad_state_1 + 1) & 3;
56            state->last_sports_pad_time_1 = cpu_cycles;
57            state->sports_pad_state_1 = (state->sports_pad_state_1 + 1) & 3;
14658         }
14759         break;
14860      }
r9579r9580
15264      switch (input_port_read_safe(space->machine, "CTRLSEL", 0x00) >> 4)
15365      {
15466      case 0x03:   /* Sports Pad */
155         if (data != sms_state.sports_pad_last_data_2)
67         if (data != state->sports_pad_last_data_2)
15668         {
15769            UINT32 cpu_cycles = downcast<cpu_device *>(space->cpu)->total_cycles();
15870
159            sms_state.sports_pad_last_data_2 = data;
160            if (cpu_cycles - sms_state.last_sports_pad_time_2 > 2048)
71            state->sports_pad_last_data_2 = data;
72            if (cpu_cycles - state->last_sports_pad_time_2 > 2048)
16173            {
162               sms_state.sports_pad_state_2 = 3;
163               sms_state.sports_pad_2_x = input_port_read(space->machine, "SPORT2");
164               sms_state.sports_pad_2_y = input_port_read(space->machine, "SPORT3");
74               state->sports_pad_state_2 = 3;
75               state->sports_pad_2_x = input_port_read(space->machine, "SPORT2");
76               state->sports_pad_2_y = input_port_read(space->machine, "SPORT3");
16577            }
166            sms_state.last_sports_pad_time_2 = cpu_cycles;
167            sms_state.sports_pad_state_2 = (sms_state.sports_pad_state_2 + 1) & 3;
78            state->last_sports_pad_time_2 = cpu_cycles;
79            state->sports_pad_state_2 = (state->sports_pad_state_2 + 1) & 3;
16880         }
16981         break;
17082      }
r9579r9580
17789   position for unknown reason (timing?) */
17890static void sms_vdp_hcount_lphaser( running_machine *machine, int hpos )
17991{
180    running_device *smsvdp = machine->device("sms_vdp");
181   int hpos_tmp = hpos + sms_state.lphaser_x_offs;
92   sms_state *state = machine->driver_data<sms_state>();
93   int hpos_tmp = hpos + state->lphaser_x_offs;
18294   UINT8 tmp = ((hpos_tmp - 46) >> 1) & 0xff;
18395   
18496   //printf ("sms_vdp_hcount_lphaser: hpos %3d hpos_tmp %3d => hcount %2X\n", hpos, hpos_tmp, tmp);
185    sms_vdp_hcount_latch_w(smsvdp, 0, tmp);
97    sms_vdp_hcount_latch_w(state->vdp, 0, tmp);
18698}
18799
188100
r9579r9580
210122*/
211123static int lgun_bright_aim_area( running_machine *machine, emu_timer *timer, int lgun_x, int lgun_y )
212124{
125   sms_state *state = machine->driver_data<sms_state>();
213126   const int r_x_r = LGUN_RADIUS * LGUN_RADIUS;
214   running_device *smsvdp = machine->device("sms_vdp");
215127   screen_device *screen = screen_first(*machine);
216128   const rectangle &visarea = screen->visible_area();
217129   int beam_x = screen->hpos();
r9579r9580
255167
256168      if (!pos_changed)
257169      {
258         result = sms_vdp_check_brightness(smsvdp, beam_x, beam_y);
170         result = sms_vdp_check_brightness(state->vdp, beam_x, beam_y);
259171         /* next check at same line */
260172         beam_x += LGUN_X_INTERVAL;
261173         pos_changed = 1;
r9579r9580
300212
301213static void sms_vdp_hcount_latch( running_machine *machine )
302214{
215   sms_state *state = machine->driver_data<sms_state>();
303216   UINT8 value = sms_vdp_hcount(machine);
304   running_device *smsvdp = machine->device("sms_vdp");
305217
306   sms_vdp_hcount_latch_w(smsvdp, 0, value);
218   sms_vdp_hcount_latch_w(state->vdp, 0, value);
307219}
308220
309221
r9579r9580
325237
326238static void lphaser1_sensor_check( running_machine *machine )
327239{
240   sms_state *state = machine->driver_data<sms_state>();
328241   const int x = screen_hpos_nonscaled(screen_first(*machine), input_port_read(machine, "LPHASER0"));
329242   const int y = screen_vpos_nonscaled(screen_first(*machine), input_port_read(machine, "LPHASER1"));
330243
331   if (lgun_bright_aim_area(machine, sms_state.lphaser_1_timer, x, y))
244   if (lgun_bright_aim_area(machine, state->lphaser_1_timer, x, y))
332245   {
333      if (sms_state.lphaser_1_latch == 0)
246      if (state->lphaser_1_latch == 0)
334247      {
335         sms_state.lphaser_1_latch = 1;
248         state->lphaser_1_latch = 1;
336249         sms_vdp_hcount_lphaser(machine, x);
337250      }
338251   }
r9579r9580
340253
341254static void lphaser2_sensor_check( running_machine *machine )
342255{
256   sms_state *state = machine->driver_data<sms_state>();
343257   const int x = screen_hpos_nonscaled(screen_first(*machine), input_port_read(machine, "LPHASER2"));
344258   const int y = screen_vpos_nonscaled(screen_first(*machine), input_port_read(machine, "LPHASER3"));
345259   
346   if (lgun_bright_aim_area(machine, sms_state.lphaser_2_timer, x, y))
260   if (lgun_bright_aim_area(machine, state->lphaser_2_timer, x, y))
347261   {
348      if (sms_state.lphaser_2_latch == 0)
262      if (state->lphaser_2_latch == 0)
349263      {
350         sms_state.lphaser_2_latch = 1;
264         state->lphaser_2_latch = 1;
351265         sms_vdp_hcount_lphaser(machine, x);
352266      }
353267   }
r9579r9580
358272// if so, we turn on crosshair and the lightgun timer
359273static TIMER_CALLBACK( lightgun_tick )
360274{
275   sms_state *state = machine->driver_data<sms_state>();
276
361277   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f) == 0x01)
362278   {
363279      /* enable crosshair */
364280      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
365      if (!timer_enabled(sms_state.lphaser_1_timer))
281      if (!timer_enabled(state->lphaser_1_timer))
366282         lphaser1_sensor_check(machine);
367283   }
368284   else
369285   {
370286      /* disable crosshair */
371287      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
372      timer_enable(sms_state.lphaser_1_timer, 0);
288      timer_enable(state->lphaser_1_timer, 0);
373289   }
374290   
375291   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0) == 0x10)
376292   {
377293      /* enable crosshair */
378294      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_ALL);
379      if (!timer_enabled(sms_state.lphaser_2_timer))
295      if (!timer_enabled(state->lphaser_2_timer))
380296         lphaser2_sensor_check(machine);
381297   }
382298   else
383299   {
384300      /* disable crosshair */
385301      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_NONE);
386      timer_enable(sms_state.lphaser_2_timer, 0);
302      timer_enable(state->lphaser_2_timer, 0);
387303   }
388304}
389305
r9579r9580
402318
403319INPUT_CHANGED( lgun1_changed )
404320{
405   if (!sms_state.lphaser_1_timer ||
321   sms_state *state = field->port->machine->driver_data<sms_state>();
322   if (!state->lphaser_1_timer ||
406323      (input_port_read_safe(field->port->machine, "CTRLSEL", 0x00) & 0x0f) != 0x01)
407324      return;
408325   
r9579r9580
412329
413330INPUT_CHANGED( lgun2_changed )
414331{
415   if (!sms_state.lphaser_2_timer ||
332   sms_state *state = field->port->machine->driver_data<sms_state>();
333   if (!state->lphaser_2_timer ||
416334      (input_port_read_safe(field->port->machine, "CTRLSEL", 0x00) & 0xf0) != 0x10)
417335      return;
418336   
r9579r9580
423341
424342static void sms_get_inputs( address_space *space )
425343{
344   sms_state *state = space->machine->driver_data<sms_state>();
426345   UINT8 data = 0x00;
427346   UINT32 cpu_cycles = downcast<cpu_device *>(space->cpu)->total_cycles();
428347   running_machine *machine = space->machine;
429348
430   sms_state.input_port0 = 0xff;
431   sms_state.input_port1 = 0xff;
349   state->input_port0 = 0xff;
350   state->input_port1 = 0xff;
432351
433   if (cpu_cycles - sms_state.last_paddle_read_time > 256)
352   if (cpu_cycles - state->last_paddle_read_time > 256)
434353   {
435      sms_state.paddle_read_state ^= 0xff;
436      sms_state.last_paddle_read_time = cpu_cycles;
354      state->paddle_read_state ^= 0xff;
355      state->last_paddle_read_time = cpu_cycles;
437356   }
438357
439358   /* Check if lightgun has been chosen as input: if so, enable crosshair */
r9579r9580
446365      data = input_port_read(machine, "PORT_DC");
447366      /* Check Rapid Fire setting for Button A */
448367      if (!(data & 0x10) && (input_port_read(machine, "RFU") & 0x01))
449         data |= sms_state.rapid_fire_state_1 & 0x10;
368         data |= state->rapid_fire_state_1 & 0x10;
450369
451370      /* Check Rapid Fire setting for Button B */
452371      if (!(data & 0x20) && (input_port_read(machine, "RFU") & 0x02))
453         data |= sms_state.rapid_fire_state_1 & 0x20;
372         data |= state->rapid_fire_state_1 & 0x20;
454373
455      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | (data & 0x3f);
374      state->input_port0 = (state->input_port0 & 0xc0) | (data & 0x3f);
456375      break;
457376
458377   case 0x01:  /* Light Phaser */
r9579r9580
460379      if (!(data & 0x10))
461380      {
462381         if (input_port_read(machine, "RFU") & 0x01)
463            data |= sms_state.rapid_fire_state_1 & 0x10;
382            data |= state->rapid_fire_state_1 & 0x10;
464383      }
465384      /* just consider the button (trigger) bit */
466385      data |= ~0x10;
467      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | (data & 0x3f);
386      state->input_port0 = (state->input_port0 & 0xc0) | (data & 0x3f);
468387      break;
469388
470389   case 0x02:  /* Paddle Control */
471390      /* Get button A state */
472391      data = input_port_read(machine, "PADDLE0");
473392
474      if (sms_state.paddle_read_state)
393      if (state->paddle_read_state)
475394         data = data >> 4;
476395
477      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | (data & 0x0f) | (sms_state.paddle_read_state & 0x20)
396      state->input_port0 = (state->input_port0 & 0xc0) | (data & 0x0f) | (state->paddle_read_state & 0x20)
478397                      | ((input_port_read(machine, "CTRLIPT") & 0x02) << 3);
479398      break;
480399
481400   case 0x03:   /* Sega Sports Pad */
482      switch (sms_state.sports_pad_state_1)
401      switch (state->sports_pad_state_1)
483402      {
484403      case 0:
485         data = (sms_state.sports_pad_1_x >> 4) & 0x0f;
404         data = (state->sports_pad_1_x >> 4) & 0x0f;
486405         break;
487406      case 1:
488         data = sms_state.sports_pad_1_x & 0x0f;
407         data = state->sports_pad_1_x & 0x0f;
489408         break;
490409      case 2:
491         data = (sms_state.sports_pad_1_y >> 4) & 0x0f;
410         data = (state->sports_pad_1_y >> 4) & 0x0f;
492411         break;
493412      case 3:
494         data = sms_state.sports_pad_1_y & 0x0f;
413         data = state->sports_pad_1_y & 0x0f;
495414         break;
496415      }
497      sms_state.input_port0 = (sms_state.input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
416      state->input_port0 = (state->input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
498417      break;
499418   }
500419
r9579r9580
503422   {
504423   case 0x00:   /* Joystick */
505424      data = input_port_read(machine, "PORT_DC");
506      sms_state.input_port0 = (sms_state.input_port0 & 0x3f) | (data & 0xc0);
425      state->input_port0 = (state->input_port0 & 0x3f) | (data & 0xc0);
507426
508427      data = input_port_read(machine, "PORT_DD");
509428      /* Check Rapid Fire setting for Button A */
510429      if (!(data & 0x04) && (input_port_read(machine, "RFU") & 0x04))
511         data |= sms_state.rapid_fire_state_2 & 0x04;
430         data |= state->rapid_fire_state_2 & 0x04;
512431
513432      /* Check Rapid Fire setting for Button B */
514433      if (!(data & 0x08) && (input_port_read(machine, "RFU") & 0x08))
515         data |= sms_state.rapid_fire_state_2 & 0x08;
434         data |= state->rapid_fire_state_2 & 0x08;
516435
517      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | (data & 0x0f);
436      state->input_port1 = (state->input_port1 & 0xf0) | (data & 0x0f);
518437      break;
519438
520439   case 0x10:   /* Light Phaser */
r9579r9580
522441      if (!(data & 0x04))
523442      {
524443         if (input_port_read(machine, "RFU") & 0x04)
525            data |= sms_state.rapid_fire_state_2 & 0x04;
444            data |= state->rapid_fire_state_2 & 0x04;
526445      }
527446      /* just consider the button (trigger) bit */
528447      data |= ~0x04;
529      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | (data & 0x0f);
448      state->input_port1 = (state->input_port1 & 0xf0) | (data & 0x0f);
530449      break;
531450
532451   case 0x20:   /* Paddle Control */
533452      /* Get button A state */
534453      data = input_port_read(machine, "PADDLE1");
535      if (sms_state.paddle_read_state)
454      if (state->paddle_read_state)
536455         data = data >> 4;
537456
538      sms_state.input_port0 = (sms_state.input_port0 & 0x3f) | ((data & 0x03) << 6);
539      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (sms_state.paddle_read_state & 0x08)
457      state->input_port0 = (state->input_port0 & 0x3f) | ((data & 0x03) << 6);
458      state->input_port1 = (state->input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (state->paddle_read_state & 0x08)
540459                      | ((input_port_read(machine, "CTRLIPT") & 0x20) >> 3);
541460      break;
542461
543462   case 0x30:   /* Sega Sports Pad */
544      switch (sms_state.sports_pad_state_2)
463      switch (state->sports_pad_state_2)
545464      {
546465      case 0:
547         data = sms_state.sports_pad_2_x & 0x0f;
466         data = state->sports_pad_2_x & 0x0f;
548467         break;
549468      case 1:
550         data = (sms_state.sports_pad_2_x >> 4) & 0x0f;
469         data = (state->sports_pad_2_x >> 4) & 0x0f;
551470         break;
552471      case 2:
553         data = sms_state.sports_pad_2_y & 0x0f;
472         data = state->sports_pad_2_y & 0x0f;
554473         break;
555474      case 3:
556         data = (sms_state.sports_pad_2_y >> 4) & 0x0f;
475         data = (state->sports_pad_2_y >> 4) & 0x0f;
557476         break;
558477      }
559      sms_state.input_port0 = (sms_state.input_port0 & 0x3f) | ((data & 0x03) << 6);
560      sms_state.input_port1 = (sms_state.input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
478      state->input_port0 = (state->input_port0 & 0x3f) | ((data & 0x03) << 6);
479      state->input_port1 = (state->input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
561480      break;
562481   }
563482}
r9579r9580
565484
566485WRITE8_HANDLER( sms_fm_detect_w )
567486{
568   if (sms_state.has_fm)
569      sms_state.fm_detect = (data & 0x01);
487   sms_state *state = space->machine->driver_data<sms_state>();
488
489   if (state->has_fm)
490      state->fm_detect = (data & 0x01);
570491}
571492
572493
573494READ8_HANDLER( sms_fm_detect_r )
574495{
575   if (sms_state.has_fm)
496   sms_state *state = space->machine->driver_data<sms_state>();
497
498   if (state->has_fm)
576499   {
577      return sms_state.fm_detect;
500      return state->fm_detect;
578501   }
579502   else
580503   {
581      if (sms_state.bios_port & IO_CHIP)
504      if (state->bios_port & IO_CHIP)
582505      {
583506         return 0xff;
584507      }
585508      else
586509      {
587510         sms_get_inputs(space);
588         return sms_state.input_port0;
511         return state->input_port0;
589512      }
590513   }
591514}
592515
593516WRITE8_HANDLER( sms_io_control_w )
594517{
518   sms_state *state = space->machine->driver_data<sms_state>();
519
595520   if (data & 0x08)
596521   {
597522      /* check if TH pin level is high (1) and was low last time */
598      if (data & 0x80 && !(sms_state.ctrl_reg & 0x80))
523      if (data & 0x80 && !(state->ctrl_reg & 0x80))
599524      {
600525         sms_vdp_hcount_latch(space->machine);
601526      }
r9579r9580
604529
605530   if (data & 0x02)
606531   {
607      if (data & 0x20 && !(sms_state.ctrl_reg & 0x20))
532      if (data & 0x20 && !(state->ctrl_reg & 0x20))
608533      {
609534         sms_vdp_hcount_latch(space->machine);
610535      }
611536      sms_input_write(space, 1, (data & 0x80) >> 7);
612537   }
613538
614   sms_state.ctrl_reg = data;
539   state->ctrl_reg = data;
615540}
616541
617542
618543READ8_HANDLER( sms_count_r )
619544{
620   running_device *smsvdp = space->machine->device("sms_vdp");
545   sms_state *state = space->machine->driver_data<sms_state>();
621546
622547   if (offset & 0x01)
623      return sms_vdp_hcount_latch_r(smsvdp, offset);
548      return sms_vdp_hcount_latch_r(state->vdp, offset);
624549   else
625      return sms_vdp_vcount_r(smsvdp, offset);
550      return sms_vdp_vcount_r(state->vdp, offset);
626551}
627552
628553
r9579r9580
632557 */
633558void sms_pause_callback( running_machine *machine )
634559{
635   if (sms_state.is_gamegear && !(sms_state.cartridge[sms_state.current_cartridge].features & CF_GG_SMS_MODE))
560   sms_state *state = machine->driver_data<sms_state>();
561
562   if (state->is_gamegear && !(state->cartridge[state->current_cartridge].features & CF_GG_SMS_MODE))
636563      return;
637564
638   if (!(input_port_read(machine, sms_state.is_gamegear ? "START" : "PAUSE") & 0x80))
565   if (!(input_port_read(machine, state->is_gamegear ? "START" : "PAUSE") & 0x80))
639566   {
640      if (!sms_state.paused)
567      if (!state->paused)
641568      {
642569         cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE);
643570      }
644      sms_state.paused = 1;
571      state->paused = 1;
645572   }
646573   else
647574   {
648      sms_state.paused = 0;
575      state->paused = 0;
649576   }
650577
651578   /* clear Light Phaser latch flags for next frame */
652   sms_state.lphaser_1_latch = 0;
653   sms_state.lphaser_2_latch = 0;
579   state->lphaser_1_latch = 0;
580   state->lphaser_2_latch = 0;
654581}
655582
656583READ8_HANDLER( sms_input_port_0_r )
657584{
658   if (sms_state.bios_port & IO_CHIP)
585   sms_state *state = space->machine->driver_data<sms_state>();
586
587   if (state->bios_port & IO_CHIP)
659588   {
660589      return 0xff;
661590   }
662591   else
663592   {
664593      sms_get_inputs(space);
665      return sms_state.input_port0;
594      return state->input_port0;
666595   }
667596}
668597
669598
670599READ8_HANDLER( sms_input_port_1_r )
671600{
672   if (sms_state.bios_port & IO_CHIP)
601   sms_state *state = space->machine->driver_data<sms_state>();
602
603   if (state->bios_port & IO_CHIP)
673604      return 0xff;
674605
675606   sms_get_inputs(space);
676607
677608   /* Reset Button */
678   sms_state.input_port1 = (sms_state.input_port1 & 0xef) | (input_port_read_safe(space->machine, "RESET", 0x01) & 0x01) << 4;
609   state->input_port1 = (state->input_port1 & 0xef) | (input_port_read_safe(space->machine, "RESET", 0x01) & 0x01) << 4;
679610
680611   /* Do region detection if TH of ports A and B are set to output (0) */
681   if (!(sms_state.ctrl_reg & 0x0a))
612   if (!(state->ctrl_reg & 0x0a))
682613   {
683614      /* Move bits 7,5 of IO control port into bits 7, 6 */
684      sms_state.input_port1 = (sms_state.input_port1 & 0x3f) | (sms_state.ctrl_reg & 0x80) | (sms_state.ctrl_reg & 0x20) << 1;
615      state->input_port1 = (state->input_port1 & 0x3f) | (state->ctrl_reg & 0x80) | (state->ctrl_reg & 0x20) << 1;
685616
686617      /* Inverse region detect value for Japanese machines */
687      if (sms_state.is_region_japan)
688         sms_state.input_port1 ^= 0xc0;
618      if (state->is_region_japan)
619         state->input_port1 ^= 0xc0;
689620   }
690621   else
691622   {
692      if (sms_state.ctrl_reg & 0x02 && sms_state.lphaser_1_latch)
623      if (state->ctrl_reg & 0x02 && state->lphaser_1_latch)
693624      {
694         sms_state.input_port1 &= ~0x40;
695         sms_state.lphaser_1_latch = 0;
625         state->input_port1 &= ~0x40;
626         state->lphaser_1_latch = 0;
696627      }
697628         
698      if (sms_state.ctrl_reg & 0x08 && sms_state.lphaser_2_latch)
629      if (state->ctrl_reg & 0x08 && state->lphaser_2_latch)
699630      {
700         sms_state.input_port1 &= ~0x80;
701         sms_state.lphaser_2_latch = 0;
631         state->input_port1 &= ~0x80;
632         state->lphaser_2_latch = 0;
702633      }
703634   }
704635
705   return sms_state.input_port1;
636   return state->input_port1;
706637}
707638
708639
709640
710641WRITE8_HANDLER( sms_ym2413_register_port_0_w )
711642{
712   if (sms_state.has_fm)
713   {
714      running_device *ym = space->machine->device("ym2413");
715      ym2413_w(ym, 0, (data & 0x3f));
716   }
643   sms_state *state = space->machine->driver_data<sms_state>();
644
645   if (state->has_fm)
646      ym2413_w(state->ym, 0, (data & 0x3f));
717647}
718648
719649
720650WRITE8_HANDLER( sms_ym2413_data_port_0_w )
721651{
722   if (sms_state.has_fm)
652   sms_state *state = space->machine->driver_data<sms_state>();
653
654   if (state->has_fm)
723655   {
724      running_device *ym = space->machine->device("ym2413");
725656      logerror("data_port_0_w %x %x\n", offset, data);
726      ym2413_w(ym, 1, data);
657      ym2413_w(state->ym, 1, data);
727658   }
728659}
729660
730661
731662READ8_HANDLER( gg_input_port_2_r )
732663{
733   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((sms_state.is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
734   return ((sms_state.is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine, "START") & 0x80));
664   sms_state *state = space->machine->driver_data<sms_state>();
665
666   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((state->is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
667   return ((state->is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine, "START") & 0x80));
735668}
736669
737670
738671READ8_HANDLER( sms_sscope_r )
739672{
740   return sms_state.sscope_state;
673   sms_state *state = space->machine->driver_data<sms_state>();
674   return state->sscope_state;
741675}
742676
743677
744678WRITE8_HANDLER( sms_sscope_w )
745679{
746   sms_state.sscope_state = data;
680   sms_state *state = space->machine->driver_data<sms_state>();
681   state->sscope_state = data;
747682}
748683
749684
750685READ8_HANDLER( sms_mapper_r )
751686{
752   return sms_state.mapper[offset];
687   sms_state *state = space->machine->driver_data<sms_state>();
688   return state->mapper[offset];
753689}
754690
755691/* Terebi Oekaki */
756692/* The following code comes from sg1000.c. We should eventually merge these TV Draw implementations */
757693static WRITE8_HANDLER( sms_tvdraw_axis_w )
758694{
695   sms_state *state = space->machine->driver_data<sms_state>();
759696   UINT8 tvboard_on = input_port_read_safe(space->machine, "TVDRAW", 0x00);
697
760698   if (data & 0x01)
761699   {
762      sms_state.tvdraw_data = tvboard_on ? input_port_read(space->machine, "TVDRAW_X") : 0x80;
700      state->tvdraw_data = tvboard_on ? input_port_read(space->machine, "TVDRAW_X") : 0x80;
763701
764      if (sms_state.tvdraw_data < 4) sms_state.tvdraw_data = 4;
765      if (sms_state.tvdraw_data > 251) sms_state.tvdraw_data = 251;
702      if (state->tvdraw_data < 4) state->tvdraw_data = 4;
703      if (state->tvdraw_data > 251) state->tvdraw_data = 251;
766704   }
767705   else
768706   {
769      sms_state.tvdraw_data = tvboard_on ? input_port_read(space->machine, "TVDRAW_Y") + 0x20 : 0x80;
707      state->tvdraw_data = tvboard_on ? input_port_read(space->machine, "TVDRAW_Y") + 0x20 : 0x80;
770708   }
771709}
772710
r9579r9580
778716
779717static READ8_HANDLER( sms_tvdraw_data_r )
780718{
781   return sms_state.tvdraw_data;
719   sms_state *state = space->machine->driver_data<sms_state>();
720   return state->tvdraw_data;
782721}
783722
784723
785724WRITE8_HANDLER( sms_mapper_w )
786725{
726   sms_state *state = space->machine->driver_data<sms_state>();
787727   int page;
788728   UINT8 *SOURCE_BIOS;
789729   UINT8 *SOURCE_CART;
790730   UINT8 *SOURCE;
791   UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
731   UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x4000;
792732
793733   offset &= 3;
794734
795   sms_state.mapper[offset] = data;
796   sms_state.mapper_ram[offset] = data;
735   state->mapper[offset] = data;
736   state->mapper_ram[offset] = data;
797737
798   if (sms_state.cartridge[sms_state.current_cartridge].ROM)
738   if (state->cartridge[state->current_cartridge].ROM)
799739   {
800      SOURCE_CART = sms_state.cartridge[sms_state.current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
740      SOURCE_CART = state->cartridge[state->current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
801741   }
802742   else
803743   {
804      SOURCE_CART = sms_state.banking_none[1];
744      SOURCE_CART = state->banking_none[1];
805745   }
806746
807   if (sms_state.BIOS)
747   if (state->BIOS)
808748   {
809      SOURCE_BIOS = sms_state.BIOS + ((sms_state.bios_page_count > 0) ? data % sms_state.bios_page_count : 0) * 0x4000;
749      SOURCE_BIOS = state->BIOS + ((state->bios_page_count > 0) ? data % state->bios_page_count : 0) * 0x4000;
810750   }
811751   else
812752   {
813      SOURCE_BIOS = sms_state.banking_none[1];
753      SOURCE_BIOS = state->banking_none[1];
814754   }
815755
816   if (sms_state.bios_port & IO_BIOS_ROM || (sms_state.is_gamegear && sms_state.BIOS == NULL))
756   if (state->bios_port & IO_BIOS_ROM || (state->is_gamegear && state->BIOS == NULL))
817757   {
818      if (!(sms_state.bios_port & IO_CARTRIDGE) || (sms_state.is_gamegear && sms_state.BIOS == NULL))
758      if (!(state->bios_port & IO_CARTRIDGE) || (state->is_gamegear && state->BIOS == NULL))
819759      {
820760         page = (rom_page_count > 0) ? data % rom_page_count : 0;
821         if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
761         if (!state->cartridge[state->current_cartridge].ROM)
822762            return;
823763         SOURCE = SOURCE_CART;
824764      }
r9579r9580
830770   }
831771   else
832772   {
833      page = (sms_state.bios_page_count > 0) ? data % sms_state.bios_page_count : 0;
834      if (!sms_state.BIOS)
773      page = (state->bios_page_count > 0) ? data % state->bios_page_count : 0;
774      if (!state->BIOS)
835775         return;
836776      SOURCE = SOURCE_BIOS;
837777   }
r9579r9580
842782      /* Is it ram or rom? */
843783      if (data & 0x08) /* it's ram */
844784      {
845         sms_state.cartridge[sms_state.current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
785         state->cartridge[state->current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
846786         if (data & 0x04)
847787         {
848788            LOG(("ram 1 paged.\n"));
849            SOURCE = sms_state.cartridge[sms_state.current_cartridge].cartSRAM + 0x4000;
789            SOURCE = state->cartridge[state->current_cartridge].cartSRAM + 0x4000;
850790         }
851791         else
852792         {
853793            LOG(("ram 0 paged.\n"));
854            SOURCE = sms_state.cartridge[sms_state.current_cartridge].cartSRAM;
794            SOURCE = state->cartridge[state->current_cartridge].cartSRAM;
855795         }
856796         memory_set_bankptr(space->machine,  "bank5", SOURCE);
857797         memory_set_bankptr(space->machine,  "bank6", SOURCE + 0x2000);
858798      }
859799      else /* it's rom */
860800      {
861         if (sms_state.bios_port & IO_BIOS_ROM || ! sms_state.has_bios)
801         if (state->bios_port & IO_BIOS_ROM || ! state->has_bios)
862802         {
863            page = (rom_page_count > 0) ? sms_state.mapper[3] % rom_page_count : 0;
864            SOURCE = sms_state.banking_cart[5];
803            page = (rom_page_count > 0) ? state->mapper[3] % rom_page_count : 0;
804            SOURCE = state->banking_cart[5];
865805         }
866806         else
867807         {
868            page = (sms_state.bios_page_count > 0) ? sms_state.mapper[3] % sms_state.bios_page_count : 0;
869            SOURCE = sms_state.banking_bios[5];
808            page = (state->bios_page_count > 0) ? state->mapper[3] % state->bios_page_count : 0;
809            SOURCE = state->banking_bios[5];
870810         }
871811         LOG(("rom 2 paged in %x.\n", page));
872812         memory_set_bankptr(space->machine, "bank5", SOURCE);
r9579r9580
876816
877817   case 1: /* Select 16k ROM bank for 0400-3FFF */
878818      LOG(("rom 0 paged in %x.\n", page));
879      sms_state.banking_bios[2] = SOURCE_BIOS + 0x0400;
880      sms_state.banking_cart[2] = SOURCE_CART + 0x0400;
881      if (sms_state.is_gamegear)
819      state->banking_bios[2] = SOURCE_BIOS + 0x0400;
820      state->banking_cart[2] = SOURCE_CART + 0x0400;
821      if (state->is_gamegear)
882822         SOURCE = SOURCE_CART;
883823
884824      memory_set_bankptr(space->machine, "bank2", SOURCE + 0x0400);
r9579r9580
886826
887827   case 2: /* Select 16k ROM bank for 4000-7FFF */
888828      LOG(("rom 1 paged in %x.\n", page));
889      sms_state.banking_bios[3] = SOURCE_BIOS;
890      sms_state.banking_cart[3] = SOURCE_CART;
891      if (sms_state.is_gamegear)
829      state->banking_bios[3] = SOURCE_BIOS;
830      state->banking_cart[3] = SOURCE_CART;
831      if (state->is_gamegear)
892832         SOURCE = SOURCE_CART;
893833
894834      memory_set_bankptr(space->machine, "bank3", SOURCE);
r9579r9580
896836      break;
897837
898838   case 3: /* Select 16k ROM bank for 8000-BFFF */
899      sms_state.banking_bios[5] = SOURCE_BIOS;
900      if (sms_state.is_gamegear)
839      state->banking_bios[5] = SOURCE_BIOS;
840      if (state->is_gamegear)
901841         SOURCE = SOURCE_CART;
902842
903      if ( sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
843      if ( state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER)
904844      {
905845         if (SOURCE == SOURCE_CART)
906846         {
907            SOURCE = sms_state.banking_cart[5];
847            SOURCE = state->banking_cart[5];
908848         }
909849      }
910850      else
911851      {
912         sms_state.banking_cart[5] = SOURCE_CART;
852         state->banking_cart[5] = SOURCE_CART;
913853      }
914854
915      if (!(sms_state.mapper[0] & 0x08)) /* is RAM disabled? */
855      if (!(state->mapper[0] & 0x08)) /* is RAM disabled? */
916856      {
917857         LOG(("rom 2 paged in %x.\n", page));
918858         memory_set_bankptr(space->machine, "bank5", SOURCE);
r9579r9580
925865
926866static WRITE8_HANDLER( sms_korean_zemina_banksw_w )
927867{
928   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
868   sms_state *state = space->machine->driver_data<sms_state>();
869
870   if (state->cartridge[state->current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
929871   {
930      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x2000;
872      UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x2000;
931873      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
932874
933      if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
875      if (!state->cartridge[state->current_cartridge].ROM)
934876         return;
935877
936878      switch (offset & 3)
937879      {
938880         case 0:
939            sms_state.banking_cart[5] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x2000;
940            memory_set_bankptr(space->machine, "bank5", sms_state.banking_cart[5]);
881            state->banking_cart[5] = state->cartridge[state->current_cartridge].ROM + page * 0x2000;
882            memory_set_bankptr(space->machine, "bank5", state->banking_cart[5]);
941883            break;
942884         case 1:
943            sms_state.banking_cart[6] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x2000;
944            memory_set_bankptr(space->machine, "bank6", sms_state.banking_cart[6]);
885            state->banking_cart[6] = state->cartridge[state->current_cartridge].ROM + page * 0x2000;
886            memory_set_bankptr(space->machine, "bank6", state->banking_cart[6]);
945887            break;
946888         case 2:
947            sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x2000;
948            memory_set_bankptr(space->machine, "bank3", sms_state.banking_cart[3]);
889            state->banking_cart[3] = state->cartridge[state->current_cartridge].ROM + page * 0x2000;
890            memory_set_bankptr(space->machine, "bank3", state->banking_cart[3]);
949891            break;
950892         case 3:
951            sms_state.banking_cart[4] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x2000;
952            memory_set_bankptr(space->machine, "bank4", sms_state.banking_cart[4]);
893            state->banking_cart[4] = state->cartridge[state->current_cartridge].ROM + page * 0x2000;
894            memory_set_bankptr(space->machine, "bank4", state->banking_cart[4]);
953895            break;
954896      }
955897      LOG(("Zemina mapper write: offset %x data %x.\n", offset, page));
r9579r9580
958900
959901static WRITE8_HANDLER( sms_codemasters_page0_w )
960902{
961   if (sms_state.cartridge[sms_state.current_cartridge].ROM && sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
903   sms_state *state = space->machine->driver_data<sms_state>();
904
905   if (state->cartridge[state->current_cartridge].ROM && state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER)
962906   {
963      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
964      sms_state.banking_cart[1] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
965      sms_state.banking_cart[2] = sms_state.banking_cart[1] + 0x0400;
966      memory_set_bankptr(space->machine, "bank1", sms_state.banking_cart[1]);
967      memory_set_bankptr(space->machine, "bank2", sms_state.banking_cart[2]);
907      UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x4000;
908      state->banking_cart[1] = state->cartridge[state->current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
909      state->banking_cart[2] = state->banking_cart[1] + 0x0400;
910      memory_set_bankptr(space->machine, "bank1", state->banking_cart[1]);
911      memory_set_bankptr(space->machine, "bank2", state->banking_cart[2]);
968912   }
969913}
970914
971915
972916static WRITE8_HANDLER( sms_codemasters_page1_w )
973917{
974   if (sms_state.cartridge[sms_state.current_cartridge].ROM && sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
918   sms_state *state = space->machine->driver_data<sms_state>();
919
920   if (state->cartridge[state->current_cartridge].ROM && state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER)
975921   {
976922      /* Check if we need to switch in some RAM */
977923      if (data & 0x80)
978924      {
979         sms_state.cartridge[sms_state.current_cartridge].ram_page = data & 0x07;
980         memory_set_bankptr(space->machine, "bank6", sms_state.cartridge[sms_state.current_cartridge].cartRAM + sms_state.cartridge[sms_state.current_cartridge].ram_page * 0x2000);
925         state->cartridge[state->current_cartridge].ram_page = data & 0x07;
926         memory_set_bankptr(space->machine, "bank6", state->cartridge[state->current_cartridge].cartRAM + state->cartridge[state->current_cartridge].ram_page * 0x2000);
981927      }
982928      else
983929      {
984         UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
985         sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
986         memory_set_bankptr(space->machine, "bank3", sms_state.banking_cart[3]);
987         memory_set_bankptr(space->machine, "bank4", sms_state.banking_cart[3] + 0x2000);
988         memory_set_bankptr(space->machine, "bank6", sms_state.banking_cart[5] + 0x2000);
930         UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x4000;
931         state->banking_cart[3] = state->cartridge[state->current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
932         memory_set_bankptr(space->machine, "bank3", state->banking_cart[3]);
933         memory_set_bankptr(space->machine, "bank4", state->banking_cart[3] + 0x2000);
934         memory_set_bankptr(space->machine, "bank6", state->banking_cart[5] + 0x2000);
989935      }
990936   }
991937}
r9579r9580
998944
999945static WRITE8_HANDLER( sms_kor_nobank_w )
1000946{
947   sms_state *state = space->machine->driver_data<sms_state>();
1001948   UINT8 *SOURCE_BIOS;
1002949   UINT8 *SOURCE = NULL;
1003950
1004951   space->write_byte(0xdffc + offset, data);
1005   sms_state.mapper[offset] = data;
952   state->mapper[offset] = data;
1006953
1007954   // FIXME: the code below (which only handles the BIOS bankswitch at start) should be cleaned up
1008955   // currently, it's just a stripped down version of sms_mapper_w
1009   if (sms_state.BIOS)
1010      SOURCE_BIOS = sms_state.BIOS + ((sms_state.bios_page_count > 0) ? data % sms_state.bios_page_count : 0) * 0x4000;
956   if (state->BIOS)
957      SOURCE_BIOS = state->BIOS + ((state->bios_page_count > 0) ? data % state->bios_page_count : 0) * 0x4000;
1011958   else
1012      SOURCE_BIOS = sms_state.banking_none[1];
959      SOURCE_BIOS = state->banking_none[1];
1013960
1014   if (sms_state.bios_port & IO_BIOS_ROM || (sms_state.is_gamegear && sms_state.BIOS == NULL))
961   if (state->bios_port & IO_BIOS_ROM || (state->is_gamegear && state->BIOS == NULL))
1015962      return;
1016963   else
1017964   {
1018      if (!sms_state.BIOS)
965      if (!state->BIOS)
1019966         return;
1020967      SOURCE = SOURCE_BIOS;
1021968   }
r9579r9580
1027974      case 0: /* Control */
1028975         if (!(data & 0x08)) /* it's not ram */
1029976         {
1030            if (!(sms_state.bios_port & IO_BIOS_ROM) && sms_state.has_bios)
977            if (!(state->bios_port & IO_BIOS_ROM) && state->has_bios)
1031978            {
1032               SOURCE = sms_state.banking_bios[5];
979               SOURCE = state->banking_bios[5];
1033980               memory_set_bankptr(space->machine, "bank5", SOURCE);
1034981               memory_set_bankptr(space->machine, "bank6", SOURCE + 0x2000);
1035982            }
r9579r9580
1037984         break;
1038985
1039986      case 1: /* Select 16k ROM bank for 0400-3FFF */
1040         sms_state.banking_bios[2] = SOURCE_BIOS + 0x0400;
987         state->banking_bios[2] = SOURCE_BIOS + 0x0400;
1041988         memory_set_bankptr(space->machine, "bank2", SOURCE + 0x0400);
1042989         break;
1043990
1044991      case 2: /* Select 16k ROM bank for 4000-7FFF */
1045         sms_state.banking_bios[3] = SOURCE_BIOS;
992         state->banking_bios[3] = SOURCE_BIOS;
1046993         memory_set_bankptr(space->machine, "bank3", SOURCE);
1047994         memory_set_bankptr(space->machine, "bank4", SOURCE + 0x2000);
1048995         break;
1049996
1050997      case 3: /* Select 16k ROM bank for 8000-BFFF */
1051         sms_state.banking_bios[5] = SOURCE_BIOS;
1052         if (!(sms_state.mapper[0] & 0x08)) /* is RAM disabled? */
998         state->banking_bios[5] = SOURCE_BIOS;
999         if (!(state->mapper[0] & 0x08)) /* is RAM disabled? */
10531000         {
10541001            memory_set_bankptr(space->machine, "bank5", SOURCE);
10551002            memory_set_bankptr(space->machine, "bank6", SOURCE + 0x2000);
r9579r9580
10601007
10611008WRITE8_HANDLER( sms_bios_w )
10621009{
1063   sms_state.bios_port = data;
1010   sms_state *state = space->machine->driver_data<sms_state>();
1011   state->bios_port = data;
10641012
10651013   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(space->cpu));
10661014
r9579r9580
10701018
10711019WRITE8_HANDLER( sms_cartram2_w )
10721020{
1073   if (sms_state.mapper[0] & 0x08)
1021   sms_state *state = space->machine->driver_data<sms_state>();
1022
1023   if (state->mapper[0] & 0x08)
10741024   {
10751025      logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000);
1076      if (sms_state.mapper[0] & 0x04)
1026      if (state->mapper[0] & 0x04)
10771027      {
1078         sms_state.cartridge[sms_state.current_cartridge].cartSRAM[offset + 0x6000] = data;
1028         state->cartridge[state->current_cartridge].cartSRAM[offset + 0x6000] = data;
10791029      }
10801030      else
10811031      {
1082         sms_state.cartridge[sms_state.current_cartridge].cartSRAM[offset + 0x2000] = data;
1032         state->cartridge[state->current_cartridge].cartSRAM[offset + 0x2000] = data;
10831033      }
10841034   }
10851035
1086   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
1036   if (state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER)
10871037   {
1088      sms_state.cartridge[sms_state.current_cartridge].cartRAM[sms_state.cartridge[sms_state.current_cartridge].ram_page * 0x2000 + offset] = data;
1038      state->cartridge[state->current_cartridge].cartRAM[state->cartridge[state->current_cartridge].ram_page * 0x2000 + offset] = data;
10891039   }
10901040
1091   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
1041   if (state->cartridge[state->current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
10921042   {
1093      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
1043      UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x4000;
10941044      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
10951045
1096      if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
1046      if (!state->cartridge[state->current_cartridge].ROM)
10971047         return;
10981048
1099      sms_state.banking_cart[5] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x4000;
1100      memory_set_bankptr(space->machine, "bank5", sms_state.banking_cart[5]);
1101      memory_set_bankptr(space->machine, "bank6", sms_state.banking_cart[5] + 0x2000);
1049      state->banking_cart[5] = state->cartridge[state->current_cartridge].ROM + page * 0x4000;
1050      memory_set_bankptr(space->machine, "bank5", state->banking_cart[5]);
1051      memory_set_bankptr(space->machine, "bank6", state->banking_cart[5] + 0x2000);
11021052      LOG(("rom 2 paged in %x (Korean mapper).\n", page));
11031053   }
11041054}
r9579r9580
11061056
11071057WRITE8_HANDLER( sms_cartram_w )
11081058{
1059   sms_state *state = space->machine->driver_data<sms_state>();
11091060   int page;
11101061
1111   if (sms_state.mapper[0] & 0x08)
1062   if (state->mapper[0] & 0x08)
11121063   {
11131064      logerror("write %02X to cartram at offset #%04X\n", data, offset);
1114      if (sms_state.mapper[0] & 0x04)
1065      if (state->mapper[0] & 0x04)
11151066      {
1116         sms_state.cartridge[sms_state.current_cartridge].cartSRAM[offset + 0x4000] = data;
1067         state->cartridge[state->current_cartridge].cartSRAM[offset + 0x4000] = data;
11171068      }
11181069      else
11191070      {
1120         sms_state.cartridge[sms_state.current_cartridge].cartSRAM[offset] = data;
1071         state->cartridge[state->current_cartridge].cartSRAM[offset] = data;
11211072      }
11221073   }
11231074   else
11241075   {
1125      if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
1076      if (state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
11261077      {
1127         UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
1078         UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x4000;
11281079         page = (rom_page_count > 0) ? data % rom_page_count : 0;
1129         if (!sms_state.cartridge[sms_state.current_cartridge].ROM)
1080         if (!state->cartridge[state->current_cartridge].ROM)
11301081            return;
1131         sms_state.banking_cart[5] = sms_state.cartridge[sms_state.current_cartridge].ROM + page * 0x4000;
1132         memory_set_bankptr(space->machine, "bank5", sms_state.banking_cart[5]);
1133         memory_set_bankptr(space->machine, "bank6", sms_state.banking_cart[5] + 0x2000);
1082         state->banking_cart[5] = state->cartridge[state->current_cartridge].ROM + page * 0x4000;
1083         memory_set_bankptr(space->machine, "bank5", state->banking_cart[5]);
1084         memory_set_bankptr(space->machine, "bank6", state->banking_cart[5] + 0x2000);
11341085         LOG(("rom 2 paged in %x (Codemasters mapper).\n", page));
11351086      }
1136      else if (sms_state.cartridge[sms_state.current_cartridge].features & CF_ONCART_RAM)
1087      else if (state->cartridge[state->current_cartridge].features & CF_ONCART_RAM)
11371088      {
1138         sms_state.cartridge[sms_state.current_cartridge].cartRAM[offset & (sms_state.cartridge[sms_state.current_cartridge].ram_size - 1)] = data;
1089         state->cartridge[state->current_cartridge].cartRAM[offset & (state->cartridge[state->current_cartridge].ram_size - 1)] = data;
11391090      }
11401091      else
11411092      {
r9579r9580
11471098
11481099WRITE8_HANDLER( gg_sio_w )
11491100{
1101   sms_state *state = space->machine->driver_data<sms_state>();
11501102   logerror("*** write %02X to SIO register #%d\n", data, offset);
11511103
1152   sms_state.gg_sio[offset & 0x07] = data;
1104   state->gg_sio[offset & 0x07] = data;
11531105   switch (offset & 7)
11541106   {
11551107      case 0x00: /* Parallel Data */
r9579r9580
11721124
11731125READ8_HANDLER( gg_sio_r )
11741126{
1127   sms_state *state = space->machine->driver_data<sms_state>();
11751128   logerror("*** read SIO register #%d\n", offset);
11761129
11771130   switch (offset & 7)
r9579r9580
11921145         break;
11931146   }
11941147
1195   return sms_state.gg_sio[offset];
1148   return state->gg_sio[offset];
11961149}
11971150
11981151static void sms_machine_stop( running_machine &machine )
11991152{
1153   sms_state *state = machine.driver_data<sms_state>();
1154
12001155   /* Does the cartridge have SRAM that should be saved? */
1201   if (sms_state.cartridge[sms_state.current_cartridge].sram_save) {
1156   if (state->cartridge[state->current_cartridge].sram_save) {
12021157      device_image_interface *image = dynamic_cast<device_image_interface *>(machine.device("cart1"));
1203      image->battery_save(sms_state.cartridge[sms_state.current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
1158      image->battery_save(state->cartridge[state->current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
12041159   }
12051160}
12061161
12071162
12081163static void setup_rom( address_space *space )
12091164{
1165   sms_state *state = space->machine->driver_data<sms_state>();
12101166   running_machine *machine = space->machine;
12111167
12121168   /* 1. set up bank pointers to point to nothing */
1213   memory_set_bankptr(machine, "bank1", sms_state.banking_none[1]);
1214   memory_set_bankptr(machine, "bank2", sms_state.banking_none[2]);
1215   memory_set_bankptr(machine, "bank3", sms_state.banking_none[3]);
1216   memory_set_bankptr(machine, "bank4", sms_state.banking_none[3] + 0x2000);
1217   memory_set_bankptr(machine, "bank5", sms_state.banking_none[5]);
1218   memory_set_bankptr(machine, "bank6", sms_state.banking_none[5] + 0x2000);
1169   memory_set_bankptr(machine, "bank1", state->banking_none[1]);
1170   memory_set_bankptr(machine, "bank2", state->banking_none[2]);
1171   memory_set_bankptr(machine, "bank3", state->banking_none[3]);
1172   memory_set_bankptr(machine, "bank4", state->banking_none[3] + 0x2000);
1173   memory_set_bankptr(machine, "bank5", state->banking_none[5]);
1174   memory_set_bankptr(machine, "bank6", state->banking_none[5] + 0x2000);
12191175
12201176   /* 2. check and set up expansion port */
1221   if (!(sms_state.bios_port & IO_EXPANSION) && (sms_state.bios_port & IO_CARTRIDGE) && (sms_state.bios_port & IO_CARD))
1177   if (!(state->bios_port & IO_EXPANSION) && (state->bios_port & IO_CARTRIDGE) && (state->bios_port & IO_CARD))
12221178   {
12231179      /* TODO: Implement me */
12241180      logerror("Switching to unsupported expansion port.\n");
12251181   }
12261182
12271183   /* 3. check and set up card rom */
1228   if (!(sms_state.bios_port & IO_CARD) && (sms_state.bios_port & IO_CARTRIDGE) && (sms_state.bios_port & IO_EXPANSION))
1184   if (!(state->bios_port & IO_CARD) && (state->bios_port & IO_CARTRIDGE) && (state->bios_port & IO_EXPANSION))
12291185   {
12301186      /* TODO: Implement me */
12311187      logerror("Switching to unsupported card rom port.\n");
12321188   }
12331189
12341190   /* 4. check and set up cartridge rom */
1235   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || sms_state.is_gamegear) { */
1191   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || state->is_gamegear) { */
12361192   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
1237   if ((!(sms_state.bios_port & IO_CARTRIDGE)) || sms_state.is_gamegear)
1193   if ((!(state->bios_port & IO_CARTRIDGE)) || state->is_gamegear)
12381194   {
1239      memory_set_bankptr(machine, "bank1", sms_state.banking_cart[1]);
1240      memory_set_bankptr(machine, "bank2", sms_state.banking_cart[2]);
1241      memory_set_bankptr(machine, "bank3", sms_state.banking_cart[3]);
1242      memory_set_bankptr(machine, "bank4", sms_state.banking_cart[3] + 0x2000);
1243      memory_set_bankptr(machine, "bank5", sms_state.banking_cart[5]);
1244      memory_set_bankptr(machine, "bank6", sms_state.banking_cart[5] + 0x2000);
1195      memory_set_bankptr(machine, "bank1", state->banking_cart[1]);
1196      memory_set_bankptr(machine, "bank2", state->banking_cart[2]);
1197      memory_set_bankptr(machine, "bank3", state->banking_cart[3]);
1198      memory_set_bankptr(machine, "bank4", state->banking_cart[3] + 0x2000);
1199      memory_set_bankptr(machine, "bank5", state->banking_cart[5]);
1200      memory_set_bankptr(machine, "bank6", state->banking_cart[5] + 0x2000);
12451201      logerror("Switched in cartridge rom.\n");
12461202   }
12471203
12481204   /* 5. check and set up bios rom */
1249   if (!(sms_state.bios_port & IO_BIOS_ROM))
1205   if (!(state->bios_port & IO_BIOS_ROM))
12501206   {
12511207      /* 0x0400 bioses */
1252      if (sms_state.has_bios_0400)
1208      if (state->has_bios_0400)
12531209      {
1254         memory_set_bankptr(machine, "bank1", sms_state.banking_bios[1]);
1210         memory_set_bankptr(machine, "bank1", state->banking_bios[1]);
12551211         logerror("Switched in 0x0400 bios.\n");
12561212      }
12571213      /* 0x2000 bioses */
1258      if (sms_state.has_bios_2000)
1214      if (state->has_bios_2000)
12591215      {
1260         memory_set_bankptr(machine, "bank1", sms_state.banking_bios[1]);
1261         memory_set_bankptr(machine, "bank2", sms_state.banking_bios[2]);
1216         memory_set_bankptr(machine, "bank1", state->banking_bios[1]);
1217         memory_set_bankptr(machine, "bank2", state->banking_bios[2]);
12621218         logerror("Switched in 0x2000 bios.\n");
12631219      }
1264      if (sms_state.has_bios_full)
1220      if (state->has_bios_full)
12651221      {
1266         memory_set_bankptr(machine, "bank1", sms_state.banking_bios[1]);
1267         memory_set_bankptr(machine, "bank2", sms_state.banking_bios[2]);
1268         memory_set_bankptr(machine, "bank3", sms_state.banking_bios[3]);
1269         memory_set_bankptr(machine, "bank4", sms_state.banking_bios[3] + 0x2000);
1270         memory_set_bankptr(machine, "bank5", sms_state.banking_bios[5]);
1271         memory_set_bankptr(machine, "bank6", sms_state.banking_bios[5] + 0x2000);
1222         memory_set_bankptr(machine, "bank1", state->banking_bios[1]);
1223         memory_set_bankptr(machine, "bank2", state->banking_bios[2]);
1224         memory_set_bankptr(machine, "bank3", state->banking_bios[3]);
1225         memory_set_bankptr(machine, "bank4", state->banking_bios[3] + 0x2000);
1226         memory_set_bankptr(machine, "bank5", state->banking_bios[5]);
1227         memory_set_bankptr(machine, "bank6", state->banking_bios[5] + 0x2000);
12721228         logerror("Switched in full bios.\n");
12731229      }
12741230   }
12751231
1276   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_ONCART_RAM)
1232   if (state->cartridge[state->current_cartridge].features & CF_ONCART_RAM)
12771233   {
1278      memory_set_bankptr(machine, "bank5", sms_state.cartridge[sms_state.current_cartridge].cartRAM);
1279      memory_set_bankptr(machine, "bank6", sms_state.cartridge[sms_state.current_cartridge].cartRAM);
1234      memory_set_bankptr(machine, "bank5", state->cartridge[state->current_cartridge].cartRAM);
1235      memory_set_bankptr(machine, "bank6", state->cartridge[state->current_cartridge].cartRAM);
12801236   }
12811237}
12821238
r9579r9580
14061362}
14071363
14081364
1409static int detect_lphaser_xoffset( UINT8 *rom )
1365static int detect_lphaser_xoffset( running_machine *machine, UINT8 *rom )
14101366{
1367   sms_state *state = machine->driver_data<sms_state>();
1368
14111369   static const UINT8 signatures[6][16] =
14121370   {
14131371      /* Spacegun */
r9579r9580
14241382      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9f, 0x74, 0x34, 0x70, 0x00, 0x40 },
14251383   };
14261384
1427   if (!(sms_state.bios_port & IO_CARTRIDGE) && sms_state.cartridge[sms_state.current_cartridge].size >= 0x8000)
1385   if (!(state->bios_port & IO_CARTRIDGE) && state->cartridge[state->current_cartridge].size >= 0x8000)
14281386   {
14291387      if (!memcmp(&rom[0x7ff0], signatures[0], 16) || !memcmp(&rom[0x7ff0], signatures[1], 16))
14301388         return 40;
r9579r9580
14481406
14491407DEVICE_START( sms_cart )
14501408{
1409   sms_state *state = device->machine->driver_data<sms_state>();
14511410   int i;
14521411
14531412   for (i = 0; i < MAX_CARTRIDGES; i++)
14541413   {
1455      sms_state.cartridge[i].ROM = NULL;
1456      sms_state.cartridge[i].size = 0;
1457      sms_state.cartridge[i].features = 0;
1458      sms_state.cartridge[i].cartSRAM = NULL;
1459      sms_state.cartridge[i].sram_save = 0;
1460      sms_state.cartridge[i].cartRAM = NULL;
1461      sms_state.cartridge[i].ram_size = 0;
1462      sms_state.cartridge[i].ram_page = 0;
1414      state->cartridge[i].ROM = NULL;
1415      state->cartridge[i].size = 0;
1416      state->cartridge[i].features = 0;
1417      state->cartridge[i].cartSRAM = NULL;
1418      state->cartridge[i].sram_save = 0;
1419      state->cartridge[i].cartRAM = NULL;
1420      state->cartridge[i].ram_size = 0;
1421      state->cartridge[i].ram_page = 0;
14631422   }
1464   sms_state.current_cartridge = 0;
1423   state->current_cartridge = 0;
14651424
1466   sms_state.bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1467   if (!sms_state.is_gamegear && !sms_state.has_bios)
1425   state->bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1426   if (!state->is_gamegear && !state->has_bios)
14681427   {
1469      sms_state.bios_port &= ~(IO_CARTRIDGE);
1470      sms_state.bios_port |= IO_BIOS_ROM;
1428      state->bios_port &= ~(IO_CARTRIDGE);
1429      state->bios_port |= IO_BIOS_ROM;
14711430   }
14721431}
14731432
14741433
14751434DEVICE_IMAGE_LOAD( sms_cart )
14761435{
1436   running_machine *machine = image.device().machine;
1437   sms_state *state = machine->driver_data<sms_state>();
14771438   int size, index = 0, offset = 0;
14781439   const char *extrainfo = NULL;
14791440
r9579r9580
15101471   if (strcmp(image.device().tag(), "cart16") == 0)
15111472      index = 15;
15121473
1513   sms_state.cartridge[index].features = 0;
1474   state->cartridge[index].features = 0;
15141475
15151476   if (image.software_entry() == NULL)
15161477   {
r9579r9580
15361497
15371498   /* Create a new memory region to hold the ROM. */
15381499   /* Make sure the region holds only complete (0x4000) rom banks */
1539   sms_state.cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1540   sms_state.cartridge[index].ROM = auto_alloc_array(image.device().machine, UINT8, sms_state.cartridge[index].size);
1541   sms_state.cartridge[index].cartSRAM = auto_alloc_array(image.device().machine, UINT8, NVRAM_SIZE);
1500   state->cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1501   state->cartridge[index].ROM = auto_alloc_array(machine, UINT8, state->cartridge[index].size);
1502   state->cartridge[index].cartSRAM = auto_alloc_array(machine, UINT8, NVRAM_SIZE);
15421503
15431504   /* Load ROM banks */
15441505   if (image.software_entry() == NULL)
15451506   {
15461507      image.fseek(offset, SEEK_SET);
15471508
1548      if (image.fread( sms_state.cartridge[index].ROM, size) != size)
1509      if (image.fread( state->cartridge[index].ROM, size) != size)
15491510         return IMAGE_INIT_FAIL;
15501511   }
15511512   else
15521513   {
1553      memcpy(sms_state.cartridge[index].ROM, image.get_software_region("rom") + offset, size);
1514      memcpy(state->cartridge[index].ROM, image.get_software_region("rom") + offset, size);
15541515
15551516      // check for mapper feature (currently only KOREAN_NOBANK, but eventually we will add the other mappers as well)
15561517      if (image.get_feature("pcb") != NULL)
15571518      {
15581519         const char *mapper = image.get_feature("pcb");
15591520         if (!strcmp(mapper, "korean_nobank"))
1560            sms_state.cartridge[index].features |= CF_KOREAN_NOBANK_MAPPER;
1521            state->cartridge[index].features |= CF_KOREAN_NOBANK_MAPPER;
15611522      }
15621523   }
15631524
15641525   /* check the image */
1565   if (!sms_state.has_bios)
1526   if (!state->has_bios)
15661527   {
1567      if (sms_verify_cart(sms_state.cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
1528      if (sms_verify_cart(state->cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
15681529      {
15691530         logerror("Warning loading image: sms_verify_cart failed\n");
15701531      }
r9579r9580
15751536   {
15761537      /* Check for codemasters mapper */
15771538      if (strstr(extrainfo, "CODEMASTERS"))
1578         sms_state.cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1539         state->cartridge[index].features |= CF_CODEMASTERS_MAPPER;
15791540
15801541      /* Check for korean mapper */
15811542      if (strstr(extrainfo, "KOREAN"))
1582         sms_state.cartridge[index].features |= CF_KOREAN_MAPPER;
1543         state->cartridge[index].features |= CF_KOREAN_MAPPER;
15831544
15841545      /* Check for special SMS Compatibility mode gamegear cartridges */
1585      if (sms_state.is_gamegear && strstr(extrainfo, "GGSMS"))
1586         sms_state.cartridge[index].features |= CF_GG_SMS_MODE;
1546      if (state->is_gamegear && strstr(extrainfo, "GGSMS"))
1547         state->cartridge[index].features |= CF_GG_SMS_MODE;
15871548
15881549      /* Check for 93C46 eeprom */
15891550      if (strstr(extrainfo, "93C46"))
1590         sms_state.cartridge[index].features |= CF_93C46_EEPROM;
1551         state->cartridge[index].features |= CF_93C46_EEPROM;
15911552
15921553      /* Check for 8KB on-cart RAM */
15931554      if (strstr(extrainfo, "8KB_CART_RAM"))
15941555      {
1595         sms_state.cartridge[index].features |= CF_ONCART_RAM;
1596         sms_state.cartridge[index].ram_size = 0x2000;
1597         sms_state.cartridge[index].cartRAM = auto_alloc_array(image.device().machine, UINT8, sms_state.cartridge[index].ram_size);
1556         state->cartridge[index].features |= CF_ONCART_RAM;
1557         state->cartridge[index].ram_size = 0x2000;
1558         state->cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->cartridge[index].ram_size);
15981559      }
15991560   }
16001561   else
r9579r9580
16061567         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, i;
16071568         for (i = 0; i < 0x8000; i++)
16081569         {
1609            if (sms_state.cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
1570            if (state->cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
16101571            {
1611               UINT16 addr = (sms_state.cartridge[index].ROM[i + 2] << 8) | sms_state.cartridge[index].ROM[i + 1];
1572               UINT16 addr = (state->cartridge[index].ROM[i + 2] << 8) | state->cartridge[index].ROM[i + 1];
16121573               if (addr == 0xFFFF)
16131574               { i += 2; cFFFF++; continue; }
16141575               if (addr == 0x0002 || addr == 0x0003 || addr == 0x0004)
r9579r9580
16241585
16251586         // 2 is a security measure, although tests on existing ROM showed it was not needed
16261587         if (c0002 > cFFFF + 2 || (c0002 > 0 && cFFFF == 0))
1627            sms_state.cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1588            state->cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
16281589         else if (c8000 > cFFFF + 2 || (c8000 > 0 && cFFFF == 0))
1629            sms_state.cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1590            state->cartridge[index].features |= CF_CODEMASTERS_MAPPER;
16301591         else if (cA000 > cFFFF + 2 || (cA000 > 0 && cFFFF == 0))
1631            sms_state.cartridge[index].features |= CF_KOREAN_MAPPER;
1592            state->cartridge[index].features |= CF_KOREAN_MAPPER;
16321593
16331594      }
16341595
16351596      /* Check for special SMS Compatibility mode gamegear cartridges */
1636      if (sms_state.is_gamegear && image.software_entry() == NULL)   // not sure about how to handle this with softlists
1597      if (state->is_gamegear && image.software_entry() == NULL)   // not sure about how to handle this with softlists
16371598      {
16381599         /* Just in case someone passes us an sms file */
16391600         if (!mame_stricmp (image.filetype(), "sms"))
1640            sms_state.cartridge[index].features |= CF_GG_SMS_MODE;
1601            state->cartridge[index].features |= CF_GG_SMS_MODE;
16411602      }
16421603   }
16431604
1644   if (sms_state.cartridge[index].features & CF_CODEMASTERS_MAPPER)
1605   if (state->cartridge[index].features & CF_CODEMASTERS_MAPPER)
16451606   {
1646      sms_state.cartridge[index].ram_size = 0x10000;
1647      sms_state.cartridge[index].cartRAM = auto_alloc_array(image.device().machine, UINT8, sms_state.cartridge[index].ram_size);
1648      sms_state.cartridge[index].ram_page = 0;
1607      state->cartridge[index].ram_size = 0x10000;
1608      state->cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->cartridge[index].ram_size);
1609      state->cartridge[index].ram_page = 0;
16491610   }
16501611
16511612   /* For Light Phaser games, we have to detect the x offset */
1652   sms_state.lphaser_x_offs = detect_lphaser_xoffset(sms_state.cartridge[index].ROM);
1613   state->lphaser_x_offs = detect_lphaser_xoffset(machine, state->cartridge[index].ROM);
16531614
16541615   /* Terebi Oekaki (TV Draw) is a SG1000 game with special input device which is compatible with SG1000 Mark III */
1655   if ((detect_tvdraw(sms_state.cartridge[index].ROM)) && sms_state.is_region_japan)
1616   if ((detect_tvdraw(state->cartridge[index].ROM)) && state->is_region_japan)
16561617   {
1657      address_space *program = cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1618      address_space *program = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
16581619      memory_install_write8_handler(program, 0x6000, 0x6000, 0, 0, &sms_tvdraw_axis_w);
16591620      memory_install_read8_handler(program, 0x8000, 0x8000, 0, 0, &sms_tvdraw_status_r);
16601621      memory_install_read8_handler(program, 0xa000, 0xa000, 0, 0, &sms_tvdraw_data_r);
16611622      memory_nop_write(program, 0xa000, 0xa000, 0, 0);
16621623   }
16631624
1664   if (sms_state.cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
1625   if (state->cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
16651626   {
16661627      // FIXME: we should have by default FFFD-FFFF to be only a mirror for DFFD-DFFF (with no bankswitch logic)
16671628      // and then the handlers should be installed here for all but the KOREAN_NOBANK carts
16681629      // However, to avoid memory map breakage, we currently go the other way around
1669      address_space *program = cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1630      address_space *program = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
16701631      memory_install_readwrite8_handler(program, 0xfffc, 0xffff, 0, 0, &sms_kor_nobank_r, &sms_kor_nobank_w);
16711632   }
16721633
1673   LOG(("Cart Features: %x\n", sms_state.cartridge[index].features));
1634   LOG(("Cart Features: %x\n", state->cartridge[index].features));
16741635
16751636   /* Load battery backed RAM, if available */
1676   image.battery_load(sms_state.cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE, 0x00);
1637   image.battery_load(state->cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE, 0x00);
16771638
16781639   return IMAGE_INIT_PASS;
16791640}
16801641
16811642
1682static void setup_cart_banks( void )
1643static void setup_cart_banks( running_machine *machine )
16831644{
1684   if (sms_state.cartridge[sms_state.current_cartridge].ROM)
1645   sms_state *state = machine->driver_data<sms_state>();
1646   if (state->cartridge[state->current_cartridge].ROM)
16851647   {
1686      UINT8 rom_page_count = sms_state.cartridge[sms_state.current_cartridge].size / 0x4000;
1687      sms_state.banking_cart[1] = sms_state.cartridge[sms_state.current_cartridge].ROM;
1688      sms_state.banking_cart[2] = sms_state.cartridge[sms_state.current_cartridge].ROM + 0x0400;
1689      sms_state.banking_cart[3] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1690      sms_state.banking_cart[5] = sms_state.cartridge[sms_state.current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
1648      UINT8 rom_page_count = state->cartridge[state->current_cartridge].size / 0x4000;
1649      state->banking_cart[1] = state->cartridge[state->current_cartridge].ROM;
1650      state->banking_cart[2] = state->cartridge[state->current_cartridge].ROM + 0x0400;
1651      state->banking_cart[3] = state->cartridge[state->current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1652      state->banking_cart[5] = state->cartridge[state->current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
16911653      /* Codemasters mapper points to bank 0 for page 2 */
1692      if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
1654      if (state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER)
16931655      {
1694         sms_state.banking_cart[5] = sms_state.cartridge[sms_state.current_cartridge].ROM;
1656         state->banking_cart[5] = state->cartridge[state->current_cartridge].ROM;
16951657      }
16961658   }
16971659   else
16981660   {
1699      sms_state.banking_cart[1] = sms_state.banking_none[1];
1700      sms_state.banking_cart[2] = sms_state.banking_none[2];
1701      sms_state.banking_cart[3] = sms_state.banking_none[3];
1702      sms_state.banking_cart[5] = sms_state.banking_none[5];
1661      state->banking_cart[1] = state->banking_none[1];
1662      state->banking_cart[2] = state->banking_none[2];
1663      state->banking_cart[3] = state->banking_none[3];
1664      state->banking_cart[5] = state->banking_none[5];
17031665   }
17041666}
17051667
17061668
17071669static void setup_banks( running_machine *machine )
17081670{
1671   sms_state *state = machine->driver_data<sms_state>();
17091672   UINT8 *mem = memory_region(machine, "maincpu");
1710   sms_state.banking_bios[1] = sms_state.banking_cart[1] = sms_state.banking_none[1] = mem;
1711   sms_state.banking_bios[2] = sms_state.banking_cart[2] = sms_state.banking_none[2] = mem;
1712   sms_state.banking_bios[3] = sms_state.banking_cart[3] = sms_state.banking_none[3] = mem;
1713   sms_state.banking_bios[4] = sms_state.banking_cart[4] = sms_state.banking_none[4] = mem;
1714   sms_state.banking_bios[5] = sms_state.banking_cart[5] = sms_state.banking_none[5] = mem;
1715   sms_state.banking_bios[6] = sms_state.banking_cart[6] = sms_state.banking_none[6] = mem;
1673   state->banking_bios[1] = state->banking_cart[1] = state->banking_none[1] = mem;
1674   state->banking_bios[2] = state->banking_cart[2] = state->banking_none[2] = mem;
1675   state->banking_bios[3] = state->banking_cart[3] = state->banking_none[3] = mem;
1676   state->banking_bios[4] = state->banking_cart[4] = state->banking_none[4] = mem;
1677   state->banking_bios[5] = state->banking_cart[5] = state->banking_none[5] = mem;
1678   state->banking_bios[6] = state->banking_cart[6] = state->banking_none[6] = mem;
17161679
1717   sms_state.BIOS = memory_region(machine, "user1");
1680   state->BIOS = memory_region(machine, "user1");
17181681
1719   sms_state.bios_page_count = (sms_state.BIOS ? memory_region_length(machine, "user1") / 0x4000 : 0);
1682   state->bios_page_count = (state->BIOS ? memory_region_length(machine, "user1") / 0x4000 : 0);
17201683
1721   setup_cart_banks();
1684   setup_cart_banks(machine);
17221685
1723   if (sms_state.BIOS == NULL || sms_state.BIOS[0] == 0x00)
1686   if (state->BIOS == NULL || state->BIOS[0] == 0x00)
17241687   {
1725      sms_state.BIOS = NULL;
1726      sms_state.bios_port |= IO_BIOS_ROM;
1688      state->BIOS = NULL;
1689      state->bios_port |= IO_BIOS_ROM;
17271690   }
17281691
1729   if (sms_state.BIOS)
1692   if (state->BIOS)
17301693   {
1731      sms_state.banking_bios[1] = sms_state.BIOS;
1732      sms_state.banking_bios[2] = sms_state.BIOS + 0x0400;
1733      sms_state.banking_bios[3] = sms_state.BIOS + ((1 < sms_state.bios_page_count) ? 0x4000 : 0);
1734      sms_state.banking_bios[5] = sms_state.BIOS + ((2 < sms_state.bios_page_count) ? 0x8000 : 0);
1694      state->banking_bios[1] = state->BIOS;
1695      state->banking_bios[2] = state->BIOS + 0x0400;
1696      state->banking_bios[3] = state->BIOS + ((1 < state->bios_page_count) ? 0x4000 : 0);
1697      state->banking_bios[5] = state->BIOS + ((2 < state->bios_page_count) ? 0x8000 : 0);
17351698   }
17361699}
17371700
17381701
17391702MACHINE_START( sms )
17401703{
1704   sms_state *state = machine->driver_data<sms_state>();
1705
17411706   machine->add_notifier(MACHINE_NOTIFY_EXIT, sms_machine_stop);
1742   sms_state.rapid_fire_timer = timer_alloc(machine, rapid_fire_callback , NULL);
1743   timer_adjust_periodic(sms_state.rapid_fire_timer, ATTOTIME_IN_HZ(10), 0, ATTOTIME_IN_HZ(10));
1707   state->rapid_fire_timer = timer_alloc(machine, rapid_fire_callback , NULL);
1708   timer_adjust_periodic(state->rapid_fire_timer, ATTOTIME_IN_HZ(10), 0, ATTOTIME_IN_HZ(10));
17441709
1745   sms_state.lphaser_1_timer = timer_alloc(machine, lphaser_1_callback , NULL);
1746   sms_state.lphaser_2_timer = timer_alloc(machine, lphaser_2_callback , NULL);
1710   state->lphaser_1_timer = timer_alloc(machine, lphaser_1_callback , NULL);
1711   state->lphaser_2_timer = timer_alloc(machine, lphaser_2_callback , NULL);
17471712
1713   state->main_cpu = machine->device("maincpu");
1714   state->control_cpu = machine->device("control");
1715   state->vdp = machine->device("sms_vdp");
1716   state->ym = machine->device("ym2413");
1717   state->main_scr = machine->device("screen");
1718   state->left_lcd = machine->device("left_lcd");
1719   state->right_lcd = machine->device("right_lcd");
1720   
17481721   /* Check if lightgun has been chosen as input: if so, enable crosshair */
17491722   timer_set(machine, attotime_zero, NULL, 0, lightgun_tick);
17501723}
r9579r9580
17531726MACHINE_RESET( sms )
17541727{
17551728   address_space *space = cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM);
1756   running_device *smsvdp = machine->device("sms_vdp");
1729   sms_state *state = machine->driver_data<sms_state>();
17571730
1758   sms_state.ctrl_reg = 0xff;
1759   if (sms_state.has_fm)
1760      sms_state.fm_detect = 0x01;
1731   state->ctrl_reg = 0xff;
1732   if (state->has_fm)
1733      state->fm_detect = 0x01;
17611734
1762   sms_state.mapper_ram = (UINT8*)space->get_write_ptr(0xdffc);
1735   state->mapper_ram = (UINT8*)space->get_write_ptr(0xdffc);
17631736
1764   sms_state.bios_port = 0;
1737   state->bios_port = 0;
17651738
1766   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_CODEMASTERS_MAPPER)
1739   if (state->cartridge[state->current_cartridge].features & CF_CODEMASTERS_MAPPER)
17671740   {
17681741      /* Install special memory handlers */
17691742      memory_install_write8_handler(space, 0x0000, 0x0000, 0, 0, sms_codemasters_page0_w);
17701743      memory_install_write8_handler(space, 0x4000, 0x4000, 0, 0, sms_codemasters_page1_w);
17711744   }
17721745
1773   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
1746   if (state->cartridge[state->current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
17741747      memory_install_write8_handler(space, 0x0000, 0x0003, 0, 0, sms_korean_zemina_banksw_w);
17751748
1776   if (sms_state.cartridge[sms_state.current_cartridge].features & CF_GG_SMS_MODE)
1777      sms_vdp_set_ggsmsmode(smsvdp, 1);
1749   if (state->cartridge[state->current_cartridge].features & CF_GG_SMS_MODE)
1750      sms_vdp_set_ggsmsmode(state->vdp, 1);
17781751
17791752   /* Initialize SIO stuff for GG */
1780   sms_state.gg_sio[0] = 0x7f;
1781   sms_state.gg_sio[1] = 0xff;
1782   sms_state.gg_sio[2] = 0x00;
1783   sms_state.gg_sio[3] = 0xff;
1784   sms_state.gg_sio[4] = 0x00;
1753   state->gg_sio[0] = 0x7f;
1754   state->gg_sio[1] = 0xff;
1755   state->gg_sio[2] = 0x00;
1756   state->gg_sio[3] = 0xff;
1757   state->gg_sio[4] = 0x00;
17851758
1786   sms_state.store_control = 0;
1759   state->store_control = 0;
17871760
17881761   setup_banks(machine);
17891762
17901763   setup_rom(space);
17911764
1792   sms_state.rapid_fire_state_1 = 0;
1793   sms_state.rapid_fire_state_2 = 0;
1765   state->rapid_fire_state_1 = 0;
1766   state->rapid_fire_state_2 = 0;
17941767
1795   sms_state.last_paddle_read_time = 0;
1796   sms_state.paddle_read_state = 0;
1768   state->last_paddle_read_time = 0;
1769   state->paddle_read_state = 0;
17971770
1798   sms_state.last_sports_pad_time_1 = 0;
1799   sms_state.last_sports_pad_time_2 = 0;
1800   sms_state.sports_pad_state_1 = 0;
1801   sms_state.sports_pad_state_2 = 0;
1802   sms_state.sports_pad_last_data_1 = 0;
1803   sms_state.sports_pad_last_data_2 = 0;
1804   sms_state.sports_pad_1_x = 0;
1805   sms_state.sports_pad_1_y = 0;
1806   sms_state.sports_pad_2_x = 0;
1807   sms_state.sports_pad_2_y = 0;
1771   state->last_sports_pad_time_1 = 0;
1772   state->last_sports_pad_time_2 = 0;
1773   state->sports_pad_state_1 = 0;
1774   state->sports_pad_state_2 = 0;
1775   state->sports_pad_last_data_1 = 0;
1776   state->sports_pad_last_data_2 = 0;
1777   state->sports_pad_1_x = 0;
1778   state->sports_pad_1_y = 0;
1779   state->sports_pad_2_x = 0;
1780   state->sports_pad_2_y = 0;
18081781
1809   sms_state.lphaser_1_latch = 0;
1810   sms_state.lphaser_2_latch = 0;
1782   state->lphaser_1_latch = 0;
1783   state->lphaser_2_latch = 0;
18111784
1812   sms_state.sscope_state = 0;
1785   state->sscope_state = 0;
18131786
1814   sms_state.tvdraw_data = 0;
1787   state->tvdraw_data = 0;
18151788}
18161789
18171790
r9579r9580
18231796
18241797WRITE8_HANDLER( sms_store_cart_select_w )
18251798{
1799   sms_state *state = space->machine->driver_data<sms_state>();
18261800   UINT8 slot = data >> 4;
18271801   UINT8 slottype = data & 0x08;
18281802
18291803   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
18301804   /* cartridge? slot #0 */
18311805   if (slottype == 0)
1832      sms_state.current_cartridge = slot;
1806      state->current_cartridge = slot;
18331807
1834   setup_cart_banks();
1835   memory_set_bankptr(space->machine, "bank10", sms_state.banking_cart[3] + 0x2000);
1808   setup_cart_banks(space->machine);
1809   memory_set_bankptr(space->machine, "bank10", state->banking_cart[3] + 0x2000);
18361810   setup_rom(space);
18371811}
18381812
r9579r9580
18511825
18521826READ8_HANDLER( sms_store_control_r )
18531827{
1854   return sms_state.store_control;
1828   sms_state *state = space->machine->driver_data<sms_state>();
1829   return state->store_control;
18551830}
18561831
18571832
18581833WRITE8_HANDLER( sms_store_control_w )
18591834{
1835   sms_state *state = space->machine->driver_data<sms_state>();
18601836   logerror("0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(space->cpu), data);
18611837   if (data & 0x02)
18621838   {
r9579r9580
18681844      space->machine->device("maincpu")->reset();
18691845      space->machine->device<cpu_device>("maincpu")->suspend(SUSPEND_REASON_HALT, 1);
18701846   }
1871   sms_state.store_control = data;
1847   state->store_control = data;
18721848}
18731849
18741850void sms_store_int_callback( running_machine *machine, int state )
18751851{
1876   cputag_set_input_line(machine, sms_state.store_control & 0x01 ? "control" : "maincpu", 0, state);
1852   sms_state *driver_state = machine->driver_data<sms_state>();
1853   cpu_set_input_line(driver_state->store_control & 0x01 ? driver_state->control_cpu : driver_state->main_cpu, 0, state);
18771854}
18781855
18791856
1880static void sms_set_zero_flag( void )
1857static void sms_set_zero_flag( running_machine *machine )
18811858{
1882   sms_state.is_gamegear = 0;
1883   sms_state.is_region_japan = 0;
1884   sms_state.has_bios_0400 = 0;
1885   sms_state.has_bios_2000 = 0;
1886   sms_state.has_bios_full = 0;
1887   sms_state.has_bios = 0;
1888   sms_state.has_fm = 0;
1859   sms_state *state = machine->driver_data<sms_state>();
1860   state->is_gamegear = 0;
1861   state->is_region_japan = 0;
1862   state->has_bios_0400 = 0;
1863   state->has_bios_2000 = 0;
1864   state->has_bios_full = 0;
1865   state->has_bios = 0;
1866   state->has_fm = 0;
18891867}
18901868
18911869DRIVER_INIT( sg1000m3 )
18921870{
1893   sms_set_zero_flag();
1894   sms_state.is_region_japan = 1;
1895   sms_state.has_fm = 1;
1871   sms_state *state = machine->driver_data<sms_state>();
1872   sms_set_zero_flag(machine);
1873   state->is_region_japan = 1;
1874   state->has_fm = 1;
18961875}
18971876
18981877
18991878DRIVER_INIT( sms1 )
19001879{
1901   sms_set_zero_flag();
1902   sms_state.has_bios_full = 1;
1880   sms_state *state = machine->driver_data<sms_state>();
1881   sms_set_zero_flag(machine);
1882   state->has_bios_full = 1;
19031883}
19041884
19051885
19061886DRIVER_INIT( smsj )
19071887{
1908   sms_set_zero_flag();
1909   sms_state.is_region_japan = 1;
1910   sms_state.has_bios_2000 = 1;
1911   sms_state.has_fm = 1;
1888   sms_state *state = machine->driver_data<sms_state>();
1889   sms_set_zero_flag(machine);
1890   state->is_region_japan = 1;
1891   state->has_bios_2000 = 1;
1892   state->has_fm = 1;
19121893}
19131894
19141895
19151896DRIVER_INIT( sms2kr )
19161897{
1917   sms_set_zero_flag();
1918   sms_state.is_region_japan = 1;
1919   sms_state.has_bios_full = 1;
1920   sms_state.has_fm = 1;
1898   sms_state *state = machine->driver_data<sms_state>();
1899   sms_set_zero_flag(machine);
1900   state->is_region_japan = 1;
1901   state->has_bios_full = 1;
1902   state->has_fm = 1;
19211903}
19221904
19231905
19241906DRIVER_INIT( smssdisp )
19251907{
1926   sms_set_zero_flag();
1908   sms_set_zero_flag(machine);
19271909}
19281910
19291911
19301912DRIVER_INIT( gamegear )
19311913{
1932   sms_set_zero_flag();
1933   sms_state.is_gamegear = 1;
1914   sms_state *state = machine->driver_data<sms_state>();
1915   sms_set_zero_flag(machine);
1916   state->is_gamegear = 1;
19341917}
19351918
19361919
19371920DRIVER_INIT( gamegeaj )
19381921{
1939   sms_set_zero_flag();
1940   sms_state.is_region_japan = 1;
1941   sms_state.is_gamegear = 1;
1942   sms_state.has_bios_0400 = 1;
1922   sms_state *state = machine->driver_data<sms_state>();
1923   sms_set_zero_flag(machine);
1924   state->is_region_japan = 1;
1925   state->is_gamegear = 1;
1926   state->has_bios_0400 = 1;
19431927}
19441928
19451929
r9579r9580
19571941
19581942VIDEO_START( sms1 )
19591943{
1944   sms_state *state = machine->driver_data<sms_state>();
19601945   screen_device *screen = screen_first(*machine);
19611946   int width = screen->width();
19621947   int height = screen->height();
19631948   
1964   sms_state.prevleft_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1965   sms_state.prevright_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1966   state_save_register_global_bitmap(machine, sms_state.prevleft_bitmap);
1967   state_save_register_global_bitmap(machine, sms_state.prevright_bitmap);
1949   state->prevleft_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1950   state->prevright_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1951   state_save_register_global_bitmap(machine, state->prevleft_bitmap);
1952   state_save_register_global_bitmap(machine, state->prevright_bitmap);
19681953}
19691954
19701955VIDEO_UPDATE( sms1 )
19711956{
1972   running_device *main_scr = screen->machine->device("screen");
1973   running_device *left_lcd = screen->machine->device("left_lcd");
1974   running_device *right_lcd = screen->machine->device("right_lcd");
1975   running_device *smsvdp = screen->machine->device("sms_vdp");
1957   sms_state *state = screen->machine->driver_data<sms_state>();
19761958   UINT8 sscope = input_port_read_safe(screen->machine, "SEGASCOPE", 0x00);
19771959   UINT8 sscope_binocular_hack = input_port_read_safe(screen->machine, "SSCOPE_BINOCULAR", 0x00);
19781960   UINT8 occluded_view = 0;
19791961
19801962   // without SuperScope, both LCDs for glasses go black
1981   if ((screen != main_scr) && !sscope)
1963   if ((screen != state->main_scr) && !sscope)
19821964      occluded_view = 1;
19831965
19841966   // with SuperScope, sscope_state 0 = left screen OFF, right screen ON
1985   if (!(sms_state.sscope_state & 0x01) && (screen == left_lcd))
1967   if (!(state->sscope_state & 0x01) && (screen == state->left_lcd))
19861968      occluded_view = 1;
19871969
19881970   // with SuperScope, sscope_state 1 = left screen ON, right screen OFF
1989   if ((sms_state.sscope_state & 0x01) && (screen == right_lcd))
1971   if ((state->sscope_state & 0x01) && (screen == state->right_lcd))
19901972      occluded_view = 1;
19911973         
19921974   if (!occluded_view)
19931975   {
1994      sms_vdp_update(smsvdp, bitmap, cliprect);
1976      sms_vdp_update(state->vdp, bitmap, cliprect);
19951977
19961978      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
19971979      // save a copy of current bitmap for the binocular hack
1998      if (sscope && (screen == left_lcd) && (sscope_binocular_hack & 0x01))
1999         copybitmap(sms_state.prevleft_bitmap, bitmap, 0, 0, 0, 0, cliprect);
2000      if (sscope && (screen == right_lcd) && (sscope_binocular_hack & 0x02))
2001         copybitmap(sms_state.prevright_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1980      if (sscope && (screen == state->left_lcd) && (sscope_binocular_hack & 0x01))
1981         copybitmap(state->prevleft_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1982      if (sscope && (screen == state->right_lcd) && (sscope_binocular_hack & 0x02))
1983         copybitmap(state->prevright_bitmap, bitmap, 0, 0, 0, 0, cliprect);
20021984   }
20031985   else
20041986   {
r9579r9580
20061988
20071989      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
20081990      // use the copied bitmap for the binocular hack
2009      if (sscope && (screen == left_lcd) && (sscope_binocular_hack & 0x01))
2010         copybitmap(bitmap, sms_state.prevleft_bitmap, 0, 0, 0, 0, cliprect);
2011      if (sscope && (screen == right_lcd) && (sscope_binocular_hack & 0x02))
2012         copybitmap(bitmap, sms_state.prevright_bitmap, 0, 0, 0, 0, cliprect);
1991      if (sscope && (screen == state->left_lcd) && (sscope_binocular_hack & 0x01))
1992         copybitmap(bitmap, state->prevleft_bitmap, 0, 0, 0, 0, cliprect);
1993      if (sscope && (screen == state->right_lcd) && (sscope_binocular_hack & 0x02))
1994         copybitmap(bitmap, state->prevright_bitmap, 0, 0, 0, 0, cliprect);
20131995   }
20141996
20151997   return 0;
r9579r9580
20171999
20182000VIDEO_UPDATE( sms )
20192001{
2020   running_device *smsvdp = screen->machine->device("sms_vdp");
2021   sms_vdp_update(smsvdp, bitmap, cliprect);
2022
2002   sms_state *state = screen->machine->driver_data<sms_state>();
2003   sms_vdp_update(state->vdp, bitmap, cliprect);
20232004   return 0;
20242005}
20252006
20262007VIDEO_START( gamegear )
20272008{
2009   sms_state *state = machine->driver_data<sms_state>();
20282010   screen_device *screen = screen_first(*machine);
20292011   int width = screen->width();
20302012   int height = screen->height();
20312013   
2032   sms_state.prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2033   sms_state.tmp_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2034   state_save_register_global_bitmap(machine, sms_state.prev_bitmap);
2014   state->prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2015   state->tmp_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2016   state_save_register_global_bitmap(machine, state->prev_bitmap);
20352017}
20362018
20372019VIDEO_UPDATE( gamegear )
20382020{
2039   running_device *smsvdp = screen->machine->device("sms_vdp");
2021   sms_state *state = screen->machine->driver_data<sms_state>();
20402022   int width = screen->width();
20412023   int height = screen->height();
20422024   int x, y;
20432025
2044   sms_vdp_update(smsvdp, sms_state.tmp_bitmap, cliprect);
2026   sms_vdp_update(state->vdp, state->tmp_bitmap, cliprect);
20452027
20462028   // HACK: fake LCD persistence effect
20472029   // (it would be better to generalized this in the core, to be used for all LCD systems)
20482030   for (y = 0; y < height; y++)
20492031   {
2050      UINT32 *line0 = BITMAP_ADDR32(sms_state.tmp_bitmap, y, 0);
2051      UINT32 *line1 = BITMAP_ADDR32(sms_state.prev_bitmap, y, 0);
2032      UINT32 *line0 = BITMAP_ADDR32(state->tmp_bitmap, y, 0);
2033      UINT32 *line1 = BITMAP_ADDR32(state->prev_bitmap, y, 0);
20522034      for (x = 0; x < width; x++)
20532035      {
20542036         UINT32 color0 = line0[x];
r9579r9580
20652047         *BITMAP_ADDR32(bitmap, y, x) = (r << 16) | (g << 8) | b;
20662048      }
20672049   }
2068   copybitmap(sms_state.prev_bitmap, sms_state.tmp_bitmap, 0, 0, 0, 0, cliprect);
2050   copybitmap(state->prev_bitmap, state->tmp_bitmap, 0, 0, 0, 0, cliprect);
20692051
20702052   return 0;
20712053}
src/mess/includes/sms.h
r9579r9580
1414#define NVRAM_SIZE             (0x08000)
1515#define CPU_ADDRESSABLE_SIZE   (0x10000)
1616
17#define MAX_CARTRIDGES        16
18
19class sms_state : public driver_device
20{
21public:
22   sms_state(running_machine &machine, const driver_device_config_base &config)
23   : driver_device(machine, config) { }
24
25   // running_devices
26   running_device *main_cpu;
27   running_device *control_cpu;
28   running_device *vdp;
29   running_device *ym;
30   running_device *main_scr;
31   running_device *left_lcd;
32   running_device *right_lcd;
33
34   UINT8 bios_page_count;
35   UINT8 fm_detect;
36   UINT8 ctrl_reg;
37   int paused;
38   UINT8 bios_port;
39   UINT8 *BIOS;
40   UINT8 *mapper_ram;
41   UINT8 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 *banking_bios[7];
46   UINT8 *banking_cart[7];
47   UINT8 *banking_none[7];
48   UINT8 gg_sio[5];
49   UINT8 store_control;
50   UINT8 input_port0;
51   UINT8 input_port1;
52   
53   // for gamegear LCD persistence hack
54   bitmap_t *tmp_bitmap;
55   bitmap_t *prev_bitmap;
56   
57   // for 3D glass binocular hack
58   bitmap_t *prevleft_bitmap;
59   bitmap_t *prevright_bitmap;
60   
61   /* Model identifiers */
62   UINT8 is_gamegear;
63   UINT8 is_region_japan;
64   UINT8 has_bios_0400;
65   UINT8 has_bios_2000;
66   UINT8 has_bios_full;
67   UINT8 has_bios;
68   UINT8 has_fm;
69   
70   /* Data needed for Rapid Fire Unit support */
71   emu_timer *rapid_fire_timer;
72   UINT8 rapid_fire_state_1;
73   UINT8 rapid_fire_state_2;
74   
75   /* Data needed for Paddle Control controller */
76   UINT32 last_paddle_read_time;
77   UINT8 paddle_read_state;
78   
79   /* Data needed for Sports Pad controller */
80   UINT32 last_sports_pad_time_1;
81   UINT32 last_sports_pad_time_2;
82   UINT8 sports_pad_state_1;
83   UINT8 sports_pad_state_2;
84   UINT8 sports_pad_last_data_1;
85   UINT8 sports_pad_last_data_2;
86   UINT8 sports_pad_1_x;
87   UINT8 sports_pad_1_y;
88   UINT8 sports_pad_2_x;
89   UINT8 sports_pad_2_y;
90   
91   /* Data needed for Light Phaser */
92   emu_timer *lphaser_1_timer;
93   emu_timer *lphaser_2_timer;
94   UINT8 lphaser_1_latch;
95   UINT8 lphaser_2_latch;
96   int lphaser_x_offs;   /* Needed to 'calibrate' lphaser; set at cart loading */
97   
98   /* Data needed for SegaScope (3D glasses) */
99   UINT8 sscope_state;
100   
101   /* Data needed for Terebi Oekaki (TV Draw) */
102   UINT8 tvdraw_data;
103   
104   /* Cartridge slot info */
105   UINT8 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   } cartridge[MAX_CARTRIDGES];
117};
118
119
17120/*----------- defined in machine/sms.c -----------*/
18121
19122/* Function prototypes */
src/mess/drivers/sms.c
r9579r9580
401401   MDRV_SOFTWARE_LIST_ADD("cart_list","gamegear")
402402MACHINE_CONFIG_END
403403
404static MACHINE_CONFIG_START( sms_ntsc_base, driver_device )
404static MACHINE_CONFIG_START( sms_ntsc_base, sms_state )
405405   /* basic machine hardware */
406406   MDRV_CPU_ADD("maincpu", Z80, XTAL_53_693175MHz/15)
407407   MDRV_CPU_PROGRAM_MAP(sms_mem)
r9579r9580
507507   MDRV_SMSSDISP_CARTSLOT_ADD("cart16")
508508MACHINE_CONFIG_END
509509
510static MACHINE_CONFIG_START( sms_pal_base, driver_device )
510static MACHINE_CONFIG_START( sms_pal_base, sms_state )
511511   /* basic machine hardware */
512512   MDRV_CPU_ADD("maincpu", Z80, MASTER_CLOCK_PAL/15)
513513   MDRV_CPU_PROGRAM_MAP(sms_mem)
r9579r9580
591591   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
592592MACHINE_CONFIG_END
593593
594static MACHINE_CONFIG_START( gamegear, driver_device )
594static MACHINE_CONFIG_START( gamegear, sms_state )
595595   /* basic machine hardware */
596596   MDRV_CPU_ADD("maincpu", Z80, XTAL_53_693175MHz/15)
597597   MDRV_CPU_PROGRAM_MAP(sms_mem)

Previous 15994 Revisions Next


© 1998-2018 The MESS Team