Previous 15994 Revisions Next

r10034 Friday 14th January, 2011 at 09:07:17 UTC by Miodrag Milanović
Placed megadrive code in MAME section [David Haywood]
[src/mame/drivers]megadriv.c
[src/mame/includes]megadriv.h
[src/mame/machine]md_pcb.c* md_pcb.h* megadriv.c*
[src/mess]mess.mak
[src/mess/drivers]genesis.c
[src/mess/includes]genesis.h
[src/mess/machine]gen_pcb.c gen_pcb.h genesis.c

src/mame/machine/md_pcb.c
r0r10034
1/*****************************************************************************************
2
3    Genesis/MegaDrive Cart PCBs Emulation
4
5****************************************************************************************/
6
7#include "emu.h"
8#include "md_pcb.h"
9
10typedef struct _md_pcb  md_pcb;
11struct _md_pcb
12{
13   const char              *pcb_name;
14   int                     pcb_id;
15};
16
17// Here, we take the feature attribute from .xml (i.e. the PCB name) and we assign a unique ID to it
18static const md_pcb pcb_list[] =
19{
20   /* Standard Sega PCB (one rom) */
21   {"171-5703", STD_ROM},
22   {"171-5927", STD_ROM},
23   {"171-5978B", STD_ROM},
24   {"171-5978BA", STD_ROM},
25   {"171-6329A", STD_ROM},
26
27   /* Sega PCB with two roms */
28   {"171-5841", STD_ROM},
29
30   /* Sega PCB with Serial EEPROM */
31   {"171-5878", SEGA_5878},
32   {"171-6584A", SEGA_6584A},
33
34   /* Sega PCB with sram */
35   {"171-5921", SEGA_5921},
36   {"171-6278A", SEGA_6278A},
37
38   /* Sega PCB with fram */
39   {"171-6658A", SEGA_6658A},
40
41   /* Namcot PCB (blob epoxy chip) */
42   {"837-8861", STD_ROM},
43
44   /* Codemasters PCB */
45   {"SRJCV1-2", CM_JCART},
46   {"SRJCV2-1", CM_JCART_SEPROM},
47   {"SRJCV2-2", CM_JCART_SEPROM}
48};
49
50int md_get_pcb_id(const char *pcb)
51{
52   int   i;
53
54   for (i = 0; i < ARRAY_LENGTH(pcb_list); i++)
55   {
56      if (!mame_stricmp(pcb_list[i].pcb_name, pcb))
57         return pcb_list[i].pcb_id;
58   }
59
60   return STD_ROM;
61}
src/mame/machine/md_pcb.h
r0r10034
1#ifndef __GENESIS_PCB_H
2# define __GENESIS_PCB_H
3
4/* PCB */
5enum
6  {
7    STD_ROM = 0,
8
9    /* Sega PCB */
10    SEGA_5878, SEGA_6584A, SEGA_5921, SEGA_6278A, SEGA_6658A,
11
12    /* Codemasters PCB (J-Carts and SEPROM) */
13    CM_JCART, CM_JCART_SEPROM
14  };
15
16int md_get_pcb_id(const char *pcb);
17
18#endif
19
src/mame/machine/megadriv.c
r0r10034
1/***************************************************************************
2
3  genesis.c
4
5  Machine file to handle emulation of the Sega Mega Drive & Genesis.
6
7    i2c games mapping table:
8
9    game name                         |   SDA_IN   |  SDA_OUT   |     SCL    |  SIZE_MASK     | PAGE_MASK |
10    ----------------------------------|------------|------------|------------|----------------|-----------|
11    NBA Jam                           | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x00ff (24C02) |   0x03    | xx
12    NBA Jam TE                        | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    | xx
13    NBA Jam TE (32x)                  | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    |
14    NFL Quarterback Club              | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    | xx
15    NFL Quarterback Club 96           | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x07ff (24C16) |   0x07    | xx
16    College Slam                      | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x1fff (24C64) |   0x07    | xx
17    Frank Thomas Big Hurt Baseball    | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x1fff (24C64) |   0x07    | xx
18    NHLPA Hockey 93                   | 0x200001-7 | 0x200001-7 | 0x200001-6 | 0x007f (24C01) |   0x03    | xx
19    Rings of Power                    | 0x200001-7 | 0x200001-7 | 0x200001-6 | 0x007f (24C01) |   0x03    | xx
20    Evander Holyfield's Boxing        | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
21    Greatest Heavyweights of the Ring | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
22    Wonder Boy V                      | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
23    Sports Talk Baseball              | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
24    Megaman - the Wily Wars           | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx **
25    Micro Machines 2                  | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x03ff (24C08) |   0x0f    |
26    Micro Machines Military           | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x03ff (24C08) |   0x0f    |
27    Micro Machines 96                 | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x07ff (24C16) |   0x0f    |
28    Brian Lara Cricket 96             | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x1fff (24C64) |   0x??*   |
29    ----------------------------------|------------|------------|------------|----------------|-----------|
30
31    * Notes: check these
32    ** original Rockman Mega World (J) set uses normal backup RAM
33
34    2008-09: Moved here cart code and custom mapper handlers. Hopefully,
35        it will make painless the future merging with HazeMD
36    2008-10: Fixed SRAM, matching as much as possible HazeMD. Some game is
37        not detected, however. E.g. Sonic 3. Also some games seem false
38        positives (e.g. Wonderboy 5: according to its headers it should
39        have 1byte of SRAM). Better detection routines would be welcome.
40    2009-06: Changed SRAM code, fixing more games, including Sonic 3. The
41        false positives seem only some of the games using EEPROM. Todo
42        list in mess/drivers/genesis.c includes EEPROM emulation.
43
44***************************************************************************/
45
46
47#include "emu.h"
48#include "imageutl.h"
49#include "cpu/m68000/m68000.h"
50
51#include "imagedev/cartslot.h"
52#include "../includes/megadriv.h"
53#include "md_pcb.h"
54
55
56#define MAX_MD_CART_SIZE 0x500000
57
58/* where a fresh copy of rom is stashed for reset and banking setup */
59#define VIRGIN_COPY_GEN 0xd00000
60
61enum t_cart_type
62{
63   STANDARD = 0,
64   SSF2,               /* Super Street Fighter 2 */
65   LIONK3,               /* Lion King 3 */
66   SKINGKONG,            /* Super King Kong 99 */
67   SDK99,               /* Super Donkey Kong 99 */
68   REDCLIFF,            /* Romance of the Three Kingdoms - Battle of Red Cliffs, already decoded from .mdx format */
69   REDCL_EN,            /* The encoded version... */
70   RADICA,               /* Radica TV games.. these probably should be a seperate driver since they are a seperate 'console' */
71   KOF99,               /* King of Fighters '99 */
72   SOULBLAD,            /* Soul Blade */
73   MJLOVER,            /* Mahjong Lover */
74   SQUIRRELK,            /* Squirrel King */
75   SMOUSE,               /* Smart Mouse */
76   SMB,               /* Super Mario Bros. */
77   SMB2,               /* Super Mario Bros. 2 */
78   KAIJU,               /* Pokemon Stadium */
79   CHINFIGHT3,            /* Chinese Fighters 3 */
80   LIONK2,               /* Lion King 2 */
81   BUGSLIFE,            /* A Bug's Life */
82   ELFWOR,               /* Elf Wor */
83   ROCKMANX3,            /* Rockman X3 */
84   SBUBBOB,            /* Super Bubble Bobble */
85   KOF98,               /* King of Fighters '98 */
86   REALTEC,            /* Whac a Critter/Mallet legend, Defend the Earth, Funnyworld/Ballonboy */
87   SUP19IN1,            /* Super 19 in 1 */
88   SUP15IN1,            /* Super 15 in 1 */
89   NBA_JAM,            /* NBA Jam */
90   NBA_JAM_TE,            /* NBA Jam TE / NFL Quarterback Club */
91   NFL_QB_96,            /* NFL Quarterback Club '96 */
92   C_SLAM,               /* College Slam / Frank Thomas Big Hurt Baseball */
93   EA_NHLPA,            /* NHLPA Hockey 93 / Rings of Power */
94   WBOY_V,               /* Wonder Boy V / Evander Holyfield's Boxing / Greatest Heavyweights of the Ring / Sports Talk Baseball / Megaman */
95   CODE_MASTERS         /* Micro Machines 2 / Military / 96 / Brian Lara Cricket */
96};
97
98
99static enum t_cart_type cart_type;
100
101
102static UINT16 squirrel_king_extra;
103static UINT16 lion2_prot1_data, lion2_prot2_data;
104static UINT16 realtec_bank_addr, realtec_bank_size, realtec_old_bank_addr;
105static UINT16 g_l3alt_pdat;
106static UINT16 g_l3alt_pcmd;
107
108static int genesis_last_loaded_image_length;
109
110UINT16 *genesis_sram;
111static int genesis_sram_start, genesis_sram_end;
112static int genesis_sram_active, genesis_sram_readonly;
113static int sram_handlers_installed;
114
115static int has_serial_eeprom;
116
117
118/*************************************
119 *
120 *  Handlers for custom mappers
121 *
122 *************************************/
123
124static WRITE16_HANDLER( genesis_ssf2_bank_w )
125{
126   static int lastoffset = -1,lastdata = -1;
127   UINT8 *ROM = space->machine->region("maincpu")->base();
128
129   if ((lastoffset != offset) || (lastdata != data))
130   {
131      lastoffset = offset; lastdata = data;
132      switch (offset << 1)
133      {
134         case 0x00: /* write protect register */ // this is not a write protect, but seems to do nothing useful but reset bank0 after the checksum test (red screen otherwise)
135            if (data == 2)
136            {
137               memcpy(ROM + 0x000000, ROM + 0x400000 + (((data & 0xf) - 2) * 0x080000), 0x080000);
138            }
139            break;
140         case 0x02: /* 0x080000 - 0x0FFFFF */
141            memcpy(ROM + 0x080000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
142            break;
143         case 0x04: /* 0x100000 - 0x17FFFF */
144            memcpy(ROM + 0x100000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
145            break;
146         case 0x06: /* 0x180000 - 0x1FFFFF */
147            memcpy(ROM + 0x180000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
148            break;
149         case 0x08: /* 0x200000 - 0x27FFFF */
150            memcpy(ROM + 0x200000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
151            break;
152         case 0x0a: /* 0x280000 - 0x2FFFFF */
153            memcpy(ROM + 0x280000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
154            break;
155         case 0x0c: /* 0x300000 - 0x37FFFF */
156            memcpy(ROM + 0x300000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
157            break;
158         case 0x0e: /* 0x380000 - 0x3FFFFF */
159            memcpy(ROM + 0x380000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
160            break;
161      }
162   }
163}
164
165#ifdef UNUSED_FUNCTION
166static WRITE16_HANDLER( g_l3alt_pdat_w )
167{
168   g_l3alt_pdat = data;
169}
170
171static WRITE16_HANDLER( g_l3alt_pcmd_w )
172{
173   g_l3alt_pcmd = data;
174}
175#endif
176
177static READ16_HANDLER( g_l3alt_prot_r )
178{
179   int retdata = 0;
180
181   offset &= 0x07;
182
183   switch (offset)
184   {
185
186      case 2:
187
188         switch (g_l3alt_pcmd)
189         {
190            case 1:
191               retdata = g_l3alt_pdat >> 1;
192               break;
193
194            case 2:
195               retdata = g_l3alt_pdat >> 4;
196               retdata |= (g_l3alt_pdat & 0x0f) << 4;
197               break;
198
199            default:
200               /* printf("unk prot case %d\n", g_l3alt_pcmd); */
201               retdata =  (((g_l3alt_pdat >> 7) & 1) << 0);
202               retdata |= (((g_l3alt_pdat >> 6) & 1) << 1);
203               retdata |= (((g_l3alt_pdat >> 5) & 1) << 2);
204               retdata |= (((g_l3alt_pdat >> 4) & 1) << 3);
205               retdata |= (((g_l3alt_pdat >> 3) & 1) << 4);
206               retdata |= (((g_l3alt_pdat >> 2) & 1) << 5);
207               retdata |= (((g_l3alt_pdat >> 1) & 1) << 6);
208               retdata |= (((g_l3alt_pdat >> 0) & 1) << 7);
209               break;
210         }
211         break;
212
213      default:
214
215         printf("protection read, unknown offset\n");
216         break;
217   }
218
219/*  printf("%06x: g_l3alt_pdat_w %04x g_l3alt_pcmd_w %04x return %04x\n", activecpu_get_pc(), g_l3alt_pdat, g_l3alt_pcmd, retdata); */
220
221   return retdata;
222}
223
224static WRITE16_HANDLER( g_l3alt_prot_w )
225{
226   offset &= 0x7;
227
228   switch (offset)
229   {
230      case 0x0:
231         g_l3alt_pdat = data;
232         break;
233      case 0x1:
234         g_l3alt_pcmd = data;
235         break;
236      default:
237         printf("protection write, unknown offst\n");
238         break;
239   }
240}
241
242static WRITE16_HANDLER( g_l3alt_bank_w )
243{
244   offset &= 0x7;
245
246   switch (offset)
247   {
248      case 0:
249      {
250      UINT8 *ROM = space->machine->region("maincpu")->base();
251      /* printf("%06x data %04x\n",activecpu_get_pc(), data); */
252      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN + (data & 0xffff) * 0x8000], 0x8000);
253      }
254      break;
255
256      default:
257      printf("unk bank w\n");
258      break;
259
260   }
261
262}
263
264
265static WRITE16_HANDLER( realtec_402000_w )
266{
267   realtec_bank_addr = 0;
268   realtec_bank_size = (data >> 8) & 0x1f;
269}
270
271static WRITE16_HANDLER( realtec_400000_w )
272{
273   int bankdata = (data >> 9) & 0x7;
274
275   UINT8 *ROM = space->machine->region("maincpu")->base();
276
277   realtec_old_bank_addr = realtec_bank_addr;
278   realtec_bank_addr = (realtec_bank_addr & 0x7) | bankdata << 3;
279
280   memcpy(ROM, ROM + (realtec_bank_addr * 0x20000) + 0x400000, realtec_bank_size * 0x20000);
281   memcpy(ROM + realtec_bank_size * 0x20000, ROM + (realtec_bank_addr * 0x20000) + 0x400000, realtec_bank_size * 0x20000);
282}
283
284static WRITE16_HANDLER( realtec_404000_w )
285{
286   int bankdata = (data >> 8) & 0x3;
287   UINT8 *ROM = space->machine->region("maincpu")->base();
288
289   realtec_old_bank_addr = realtec_bank_addr;
290   realtec_bank_addr = (realtec_bank_addr & 0xf8) | bankdata;
291
292   if (realtec_old_bank_addr != realtec_bank_addr)
293   {
294      memcpy(ROM, ROM + (realtec_bank_addr * 0x20000)+ 0x400000, realtec_bank_size * 0x20000);
295      memcpy(ROM + realtec_bank_size * 0x20000, ROM + (realtec_bank_addr * 0x20000) + 0x400000, realtec_bank_size * 0x20000);
296   }
297}
298
299static WRITE16_HANDLER( g_chifi3_bank_w )
300{
301   UINT8 *ROM = space->machine->region("maincpu")->base();
302
303   if (data == 0xf100) // *hit player
304   {
305      int x;
306      for (x = 0; x < 0x100000; x += 0x10000)
307      {
308         memcpy(ROM + x, ROM + 0x410000, 0x10000);
309      }
310   }
311   else if (data == 0xd700) // title screen..
312   {
313      int x;
314      for (x = 0; x < 0x100000; x += 0x10000)
315      {
316         memcpy(ROM + x, ROM + 0x470000, 0x10000);
317      }
318   }
319   else if (data == 0xd300) // character hits floor
320   {
321      int x;
322      for (x = 0; x < 0x100000; x += 0x10000)
323      {
324         memcpy(ROM + x, ROM + 0x430000, 0x10000);
325      }
326   }
327   else if (data == 0x0000)
328   {
329      int x;
330      for (x = 0; x < 0x100000; x += 0x10000)
331      {
332         memcpy(ROM + x, ROM + 0x400000 + x, 0x10000);
333      }
334   }
335   else
336   {
337      logerror("%06x chifi3, bankw? %04x %04x\n", cpu_get_pc(space->cpu), offset, data);
338   }
339
340}
341
342static READ16_HANDLER( g_chifi3_prot_r )
343{
344   UINT32 retdat;
345
346   /* not 100% correct, there may be some relationship between the reads here
347    and the writes made at the start of the game.. */
348
349   /*
350    04dc10 chifi3, prot_r? 2800
351    04cefa chifi3, prot_r? 65262
352    */
353
354   if (cpu_get_pc(space->cpu) == 0x01782) // makes 'VS' screen appear
355   {
356      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
357      retdat <<= 8;
358      return retdat;
359   }
360   else if (cpu_get_pc(space->cpu) == 0x1c24) // background gfx etc.
361   {
362      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
363      retdat <<= 8;
364      return retdat;
365   }
366   else if (cpu_get_pc(space->cpu) == 0x10c4a) // unknown
367   {
368      return space->machine->rand();
369   }
370   else if (cpu_get_pc(space->cpu) == 0x10c50) // unknown
371   {
372      return space->machine->rand();
373   }
374   else if (cpu_get_pc(space->cpu) == 0x10c52) // relates to the game speed..
375   {
376      retdat = cpu_get_reg(space->cpu, M68K_D4) & 0xff;
377      retdat <<= 8;
378      return retdat;
379   }
380   else if (cpu_get_pc(space->cpu) == 0x061ae)
381   {
382      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
383      retdat <<= 8;
384      return retdat;
385   }
386   else if (cpu_get_pc(space->cpu) == 0x061b0)
387   {
388      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
389      retdat <<= 8;
390      return retdat;
391   }
392   else
393   {
394      logerror("%06x chifi3, prot_r? %04x\n", cpu_get_pc(space->cpu), offset);
395   }
396
397   return 0;
398}
399
400static WRITE16_HANDLER( s19in1_bank )
401{
402   UINT8 *ROM = space->machine->region("maincpu")->base();
403   memcpy(ROM + 0x000000, ROM + 0x400000 + ((offset << 1) * 0x10000), 0x80000);
404}
405
406// Kaiju? (Pokemon Stadium) handler from HazeMD
407static WRITE16_HANDLER( g_kaiju_bank_w )
408{
409   UINT8 *ROM = space->machine->region("maincpu")->base();
410   memcpy(ROM + 0x000000, ROM + 0x400000 + (data & 0x7f) * 0x8000, 0x8000);
411}
412
413// Soulblade handler from HazeMD
414static READ16_HANDLER( soulb_0x400006_r )
415{
416   return 0xf000;
417}
418
419static READ16_HANDLER( soulb_0x400002_r )
420{
421   return 0x9800;
422}
423
424static READ16_HANDLER( soulb_0x400004_r )
425{
426   return 0xc900;
427}
428
429// Mahjong Lover handler from HazeMD
430static READ16_HANDLER( mjlovr_prot_1_r )
431{
432   return 0x9000;
433}
434
435static READ16_HANDLER( mjlovr_prot_2_r )
436{
437   return 0xd300;
438}
439
440// Super Mario Bros handler from HazeMD
441static READ16_HANDLER( smbro_prot_r )
442{
443   return 0xc;
444}
445
446
447// Smart Mouse handler from HazeMD
448static READ16_HANDLER( smous_prot_r )
449{
450   switch (offset)
451   {
452      case 0: return 0x5500;
453      case 1: return 0x0f00;
454      case 2: return 0xaa00;
455      case 3: return 0xf000;
456   }
457
458   return -1;
459}
460
461// Super Bubble Bobble MD handler from HazeMD
462static READ16_HANDLER( sbub_extra1_r )
463{
464   return 0x5500;
465}
466
467static READ16_HANDLER( sbub_extra2_r )
468{
469   return 0x0f00;
470}
471
472// KOF99 handler from HazeMD
473static READ16_HANDLER( kof99_0xA13002_r )
474{
475   // write 02 to a13002.. shift right 1?
476   return 0x01;
477}
478
479static READ16_HANDLER( kof99_00A1303E_r )
480{
481   // write 3e to a1303e.. shift right 1?
482   return 0x1f;
483}
484
485static READ16_HANDLER( kof99_0xA13000_r )
486{
487   // no write, startup check, chinese message if != 0
488   return 0x0;
489}
490
491// Radica handler from HazeMD
492
493static READ16_HANDLER( radica_bank_select )
494{
495   int bank = offset&0x3f;
496   UINT8 *ROM = space->machine->region("maincpu")->base();
497   memcpy(ROM, ROM +  (bank * 0x10000) + 0x400000, 0x400000);
498   return 0;
499}
500
501// ROTK Red Cliff handler from HazeMD
502
503static READ16_HANDLER( redclif_prot_r )
504{
505   return -0x56 << 8;
506}
507
508static READ16_HANDLER( redclif_prot2_r )
509{
510   return 0x55 << 8;
511}
512
513// Squirrel King handler from HazeMD, this does not give screen garbage like HazeMD compile If you reset it twice
514static READ16_HANDLER( squirrel_king_extra_r )
515{
516   return squirrel_king_extra;
517
518}
519static WRITE16_HANDLER( squirrel_king_extra_w )
520{
521   squirrel_king_extra = data;
522}
523
524// Lion King 2 handler from HazeMD
525static READ16_HANDLER( lion2_prot1_r )
526{
527   return lion2_prot1_data;
528}
529
530static READ16_HANDLER( lion2_prot2_r )
531{
532   return lion2_prot2_data;
533}
534
535static WRITE16_HANDLER ( lion2_prot1_w )
536{
537   lion2_prot1_data = data;
538}
539
540static WRITE16_HANDLER ( lion2_prot2_w )
541{
542   lion2_prot2_data = data;
543}
544
545// Rockman X3 handler from HazeMD
546static READ16_HANDLER( rx3_extra_r )
547{
548   return 0xc;
549}
550
551// King of Fighters 98 handler from HazeMD
552static READ16_HANDLER( g_kof98_aa_r )
553{
554   return 0xaa00;
555}
556
557static READ16_HANDLER( g_kof98_0a_r )
558{
559   return 0x0a00;
560}
561
562static READ16_HANDLER( g_kof98_00_r )
563{
564   return 0x0000;
565}
566
567// Super Mario Bros 2 handler from HazeMD
568static READ16_HANDLER( smb2_extra_r )
569{
570   return 0xa;
571}
572
573// Bug's Life handler from HazeMD
574static READ16_HANDLER( bugl_extra_r )
575{
576   return 0x28;
577}
578
579// Elf Wor handler from HazeMD (DFJustin says the title is 'Linghuan Daoshi Super Magician')
580static READ16_HANDLER( elfwor_0x400000_r )
581{
582   return 0x5500;
583}
584
585static READ16_HANDLER( elfwor_0x400002_r )
586{
587   return 0x0f00;
588}
589
590static READ16_HANDLER( elfwor_0x400004_r )
591{
592   return 0xc900;
593}
594
595static READ16_HANDLER( elfwor_0x400006_r )
596{
597   return 0x1800;
598}
599
600static WRITE16_HANDLER( genesis_TMSS_bank_w )
601{
602   /* this probably should do more, like make Genesis V2 'die' if the SEGA string is not written promptly */
603}
604
605
606
607/*************************************
608 *
609 *  Handlers for SRAM
610 *
611 *************************************/
612
613static void alloc_sram(running_machine *machine)
614{
615   genesis_sram = auto_alloc_array(machine, UINT16, (genesis_sram_end - genesis_sram_start + 1) / sizeof(UINT16));
616   device_image_interface *image = dynamic_cast<device_image_interface *>(machine->device("cart"));
617   image->battery_load(genesis_sram, genesis_sram_end - genesis_sram_start + 1, 0xff); // Dino Dini's Soccer needs backup RAM to be 1fill
618   memcpy(megadriv_backupram, genesis_sram, genesis_sram_end - genesis_sram_start + 1);
619}
620
621static READ16_HANDLER( genesis_sram_read )
622{
623   UINT8 *ROM;
624   int rom_offset;
625
626   if (genesis_sram_active)
627   {
628      if (genesis_sram == NULL)
629         alloc_sram(space->machine);
630      return genesis_sram[offset];
631   }
632   else
633   {
634      ROM = space->machine->region("maincpu")->base();
635      rom_offset = genesis_sram_start + (offset << 1);
636
637      return (UINT16) ROM[rom_offset] | (ROM[rom_offset + 1] << 8);
638   }
639}
640
641static WRITE16_HANDLER( genesis_sram_write )
642{
643   if (genesis_sram_active)
644   {
645      if (genesis_sram == NULL)
646         alloc_sram(space->machine);
647      if (!genesis_sram_readonly)
648         genesis_sram[offset] = data;
649   }
650}
651
652static void install_sram_rw_handlers(running_machine *machine)
653{
654   memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), genesis_sram_start & 0x3fffff, genesis_sram_end & 0x3fffff, 0, 0, genesis_sram_read);
655   memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), genesis_sram_start & 0x3fffff, genesis_sram_end & 0x3fffff, 0, 0, genesis_sram_write);
656   sram_handlers_installed = 1;
657}
658
659static WRITE16_HANDLER( genesis_sram_toggle )
660{
661
662   /* unsure if this is actually supposed to toggle or just switch on?
663    * Yet to encounter game that utilizes
664    */
665   genesis_sram_active = (data & 1) ? 1 : 0;
666   genesis_sram_readonly = (data & 2) ? 1 : 0;
667
668   if (genesis_sram_active && !sram_handlers_installed)
669      install_sram_rw_handlers (space->machine);
670}
671
672/*
673I2c bare minimum handlings (TODO: supports proper device interfacing and parameter change!)
674*/
675
676static UINT8 i2c_mem,i2c_clk;
677
678static READ16_HANDLER( nba_jam_eeprom_r )
679{
680//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
681
682   return i2c_mem & 1;
683}
684
685static WRITE16_HANDLER( nba_jam_eeprom_w )
686{
687   i2c_clk = (data & 0x0002) >> 1;
688   i2c_mem = (data & 0x0001);
689
690//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
691//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
692}
693
694static READ16_HANDLER( nba_jam_te_eeprom_r )
695{
696//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
697
698   return (i2c_mem & 1);
699}
700
701static WRITE16_HANDLER( nba_jam_te_eeprom_w )
702{
703   i2c_clk = ((data & 0x0100) >> 8);
704   i2c_mem = data & 0x0001;
705
706//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
707//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
708}
709
710static READ16_HANDLER( ea_nhlpa_eeprom_r )
711{
712//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
713
714   return (i2c_mem & 1) << 7;
715}
716
717static WRITE16_HANDLER( ea_nhlpa_eeprom_w )
718{
719   i2c_clk = ((data & 0x0040) >> 6);
720   i2c_mem = ((data & 0x0080) >> 7);
721
722//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
723//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
724}
725
726/* TODO: identical as NBA Jam, used as kludge */
727static READ16_HANDLER( wboy_v_eeprom_r )
728{
729//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
730
731   return ~i2c_mem & 1;
732}
733
734static WRITE16_HANDLER( wboy_v_eeprom_w )
735{
736   i2c_clk = (data & 0x0002) >> 1;
737   i2c_mem = (data & 0x0001);
738
739//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
740//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
741}
742
743static READ16_HANDLER( codemasters_eeprom_r )
744{
745//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
746
747   return (i2c_mem & 1) << 7;
748}
749
750static WRITE16_HANDLER( codemasters_eeprom_w )
751{
752   i2c_clk = (data & 0x0200) >> 9;
753   i2c_mem = (data & 0x0100) >> 8;
754
755//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
756//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
757}
758
759/*************************************
760 *
761 *  Machine driver reset
762 *
763 *************************************/
764
765
766static void setup_megadriv_custom_mappers(running_machine *machine)
767{
768   static int relocate = VIRGIN_COPY_GEN;
769   unsigned char *ROM;
770   UINT32 mirroraddr;
771
772   /* SRAM data */
773   sram_handlers_installed = 0;
774   genesis_sram_readonly = 0;
775   genesis_sram_active = 0;
776
777   ROM = machine->region("maincpu")->base();
778
779   if (cart_type == SSF2)
780   {
781      memcpy(&ROM[0x800000], &ROM[VIRGIN_COPY_GEN + 0x400000], 0x100000);
782      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x400000);
783      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x400000);
784
785      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa130f0, 0xa130ff, 0, 0, genesis_ssf2_bank_w);
786   }
787
788   if (cart_type == LIONK3 || cart_type == SKINGKONG)
789   {
790      g_l3alt_pdat = g_l3alt_pcmd = 0;
791      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x200000); /* default rom */
792      memcpy(&ROM[0x200000], &ROM[VIRGIN_COPY_GEN], 0x200000); /* default rom */
793
794      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x600000, 0x6fffff, 0, 0, g_l3alt_prot_r, g_l3alt_prot_w);
795      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x700000, 0x7fffff, 0, 0, g_l3alt_bank_w);
796   }
797
798   if (cart_type == SDK99)
799   {
800      g_l3alt_pdat = g_l3alt_pcmd = 0;
801
802      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x300000); /* default rom */
803      memcpy(&ROM[0x300000], &ROM[VIRGIN_COPY_GEN], 0x100000); /* default rom */
804
805      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x600000, 0x6fffff, 0, 0, g_l3alt_prot_r, g_l3alt_prot_w);
806      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x700000, 0x7fffff, 0, 0, g_l3alt_bank_w);
807   }
808
809   if (cart_type == REDCLIFF)
810   {
811      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, redclif_prot2_r);
812      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, redclif_prot_r);
813   }
814
815   if (cart_type == REDCL_EN)
816   {
817      int x;
818
819      for (x = VIRGIN_COPY_GEN; x < VIRGIN_COPY_GEN + 0x200005; x++)
820      {
821         ROM[x] ^= 0x40;
822      }
823
824      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN + 4], 0x200000); /* default rom */
825
826      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, redclif_prot2_r);
827      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, redclif_prot_r);
828   }
829
830   if (cart_type == RADICA)
831   {
832      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x400000); // keep a copy for later banking.. making use of huge ROM_REGION allocated to genesis driver
833      memcpy(&ROM[0x800000], &ROM[VIRGIN_COPY_GEN], 0x400000); // wraparound banking (from hazemd code)
834      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x400000);
835      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa1307f, 0, 0, radica_bank_select);
836   }
837
838   if (cart_type == KOF99)
839   {
840      //memcpy(&ROM[0x000000],&ROM[VIRGIN_COPY_GEN],0x300000);
841      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, kof99_0xA13000_r);
842      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13002, 0xa13003, 0, 0, kof99_0xA13002_r);
843      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa1303e, 0xa1303f, 0, 0, kof99_00A1303E_r);
844   }
845
846   if (cart_type == SOULBLAD)
847   {
848      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, soulb_0x400002_r);
849      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, soulb_0x400004_r);
850      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400006, 0x400007, 0, 0, soulb_0x400006_r);
851   }
852
853   if (cart_type == MJLOVER)
854   {
855      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, mjlovr_prot_1_r);
856      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x401000, 0x401001, 0, 0, mjlovr_prot_2_r);
857   }
858
859   if (cart_type == SQUIRRELK)
860   {
861      squirrel_king_extra = 0;
862      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400007, 0, 0, squirrel_king_extra_r);
863      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400007, 0, 0, squirrel_king_extra_w);
864   }
865
866   if (cart_type == SMOUSE)
867   {
868      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400007, 0, 0, smous_prot_r);
869   }
870
871   if (cart_type == SMB)
872   {
873      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, smbro_prot_r);
874   }
875
876   if (cart_type == SMB2)
877   {
878      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, smb2_extra_r);
879   }
880
881   if (cart_type == KAIJU)
882   {
883      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x200000);
884      memcpy(&ROM[0x600000], &ROM[VIRGIN_COPY_GEN], 0x200000);
885      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x200000);
886
887      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x700000, 0x7fffff, 0, 0, g_kaiju_bank_w);
888   }
889
890   if (cart_type == CHINFIGHT3)
891   {
892      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x200000);
893      memcpy(&ROM[0x600000], &ROM[VIRGIN_COPY_GEN], 0x200000);
894      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x200000);
895
896      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x4fffff, 0, 0, g_chifi3_prot_r);
897      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x600000, 0x6fffff, 0, 0, g_chifi3_bank_w);
898   }
899
900   if (cart_type == LIONK2)
901   {
902      lion2_prot1_data = lion2_prot2_data = 0;
903
904      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, lion2_prot1_r);
905      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400006, 0x400007, 0, 0, lion2_prot2_r);
906      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, lion2_prot1_w);
907      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, lion2_prot2_w);
908   }
909
910   if (cart_type == BUGSLIFE)
911   {
912      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, bugl_extra_r);
913   }
914
915   if (cart_type == ELFWOR)
916   {
917   /* It return (0x55 @ 0x400000 OR 0xc9 @ 0x400004) AND (0x0f @ 0x400002 OR 0x18 @ 0x400006). It is probably best to add handlers for all 4 addresses. */
918
919      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, elfwor_0x400000_r);
920      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, elfwor_0x400002_r);
921      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, elfwor_0x400004_r);
922      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400006, 0x400007, 0, 0, elfwor_0x400006_r);
923   }
924
925   if (cart_type == ROCKMANX3)
926   {
927      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, rx3_extra_r);
928   }
929
930   if (cart_type == SBUBBOB)
931   {
932      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, sbub_extra1_r);
933      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, sbub_extra2_r);
934   }
935
936   if (cart_type == KOF98)
937   {
938      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x480000, 0x480001, 0, 0, g_kof98_aa_r);
939      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4800e0, 0x4800e1, 0, 0, g_kof98_aa_r);
940      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4824a0, 0x4824a1, 0, 0, g_kof98_aa_r);
941      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x488880, 0x488881, 0, 0, g_kof98_aa_r);
942
943      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4a8820, 0x4a8821, 0, 0, g_kof98_0a_r);
944      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4f8820, 0x4f8821, 0, 0, g_kof98_00_r);
945   }
946
947   if (cart_type == REALTEC)
948   {
949      /* Realtec mapper!*/
950      realtec_bank_addr = realtec_bank_size = 0;
951      realtec_old_bank_addr = -1;
952
953      memcpy(&ROM[0x400000], &ROM[relocate], 0x80000);
954
955      for (mirroraddr = 0; mirroraddr < 0x400000; mirroraddr += 0x2000)
956      {
957         memcpy(ROM + mirroraddr, ROM + relocate + 0x7e000, 0x002000); /* copy last 8kb across the whole rom region */
958      }
959
960      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, realtec_400000_w);
961      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x402000, 0x402001, 0, 0, realtec_402000_w);
962      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x404000, 0x404001, 0, 0, realtec_404000_w);
963   }
964
965   if (cart_type == SUP19IN1)
966   {
967      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x400000); // allow hard reset to menu
968      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13039, 0, 0, s19in1_bank);
969   }
970
971   if (cart_type == SUP15IN1)
972   {
973      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x200000); // allow hard reset to menu
974      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13039, 0, 0, s19in1_bank);
975   }
976
977   /* Disable SRAM handlers if it's a game using EEPROM to save
978    progresses. Currently EEPROM is not emulated. */
979   if (!has_serial_eeprom)
980   {
981      /* Info from DGen: If SRAM does not overlap main ROM, set it active by
982        default since a few games can't manage to properly switch it on/off. */
983      if (genesis_last_loaded_image_length <= genesis_sram_start)
984         genesis_sram_active = 1;
985
986      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa130f0, 0xa130f1, 0, 0, genesis_sram_toggle);
987
988      /* Sonic 1 included in Sonic Classics doesn't have SRAM and
989        does lots of ROM access at this range, then only install read/
990        write handlers if SRAM is active to not slow down emulation. */
991      if (genesis_sram_active)
992         install_sram_rw_handlers (machine);
993   }
994
995   /* install i2c handlers */
996   if(cart_type == NBA_JAM)
997      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, nba_jam_eeprom_r,nba_jam_eeprom_w);
998
999   if(cart_type == WBOY_V)
1000      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, wboy_v_eeprom_r,wboy_v_eeprom_w);
1001
1002   if(cart_type == NBA_JAM_TE || cart_type == NFL_QB_96 || cart_type == C_SLAM) // same handling but different sizes
1003      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, nba_jam_te_eeprom_r,nba_jam_te_eeprom_w);
1004
1005   if(cart_type == EA_NHLPA)
1006      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, ea_nhlpa_eeprom_r,ea_nhlpa_eeprom_w);
1007
1008   if(cart_type == CODE_MASTERS)
1009   {
1010      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x300000, 0x300001, 0, 0, codemasters_eeprom_w);
1011      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x380000, 0x380001, 0, 0, codemasters_eeprom_r);
1012
1013      // TODO: J-Cart device
1014   }
1015
1016   /* install NOP handler for TMSS */
1017   memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa14000, 0xa14003, 0, 0, genesis_TMSS_bank_w);
1018}
1019
1020
1021MACHINE_RESET( md_mappers )
1022{
1023   setup_megadriv_custom_mappers(machine);
1024}
1025
1026
1027/*************************************
1028 *
1029 *  Cart handling
1030 *
1031 *************************************/
1032
1033
1034/******* Memcmp for both endianness *******/
1035
1036static int allendianmemcmp(const void *s1, const void *s2, size_t n)
1037{
1038   const unsigned char *realbuf;
1039
1040#ifdef LSB_FIRST
1041   unsigned char flippedbuf[64];
1042   unsigned int i;
1043
1044   if ((n & 1) || (n > 63)) return -1 ; // don't want odd sized buffers or too large a compare
1045   for (i = 0; i < n; i++) flippedbuf[i] = *((char *)s2 + (i ^ 1));
1046   realbuf = flippedbuf;
1047
1048#else
1049
1050   realbuf = (unsigned char *)s2;
1051
1052#endif
1053   return memcmp(s1,realbuf,n);
1054}
1055
1056
1057/******* SMD files detection *******/
1058
1059/* code taken directly from GoodGEN by Cowering */
1060static int genesis_is_funky_SMD(unsigned char *buf,unsigned int len)
1061{
1062   /* aq quiz */
1063   if (!strncmp("UZ(-01  ", (const char *) &buf[0xf0], 8))
1064      return 1;
1065
1066    /* Phelios USA redump */
1067   /* target earth */
1068   /* klax (namcot) */
1069   if (buf[0x2080] == ' ' && buf[0x0080] == 'S' && buf[0x2081] == 'E' && buf[0x0081] == 'G')
1070      return 1;
1071
1072    /* jap baseball 94 */
1073   if (!strncmp("OL R-AEAL", (const char *) &buf[0xf0], 9))
1074      return 1;
1075
1076    /* devilish Mahjong Tower */
1077    if (!strncmp("optrEtranet", (const char *) &buf[0xf3], 11))
1078      return 1;
1079
1080   /* golden axe 2 beta */
1081   if (buf[0x0100] == 0x3c && buf[0x0101] == 0 && buf[0x0102] == 0 && buf[0x0103] == 0x3c)
1082      return 1;
1083
1084    /* omega race */
1085   if (!strncmp("OEARC   ", (const char *) &buf[0x90], 8))
1086      return 1;
1087
1088    /* budokan beta */
1089   if ((len >= 0x6708+8) && !strncmp(" NTEBDKN", (const char *) &buf[0x6708], 8))
1090      return 1;
1091
1092    /* cdx pro 1.8 bios */
1093   if (!strncmp("so fCXP", (const char *) &buf[0x2c0], 7))
1094      return 1;
1095
1096    /* ishido (hacked) */
1097   if (!strncmp("sio-Wyo ", (const char *) &buf[0x0090], 8))
1098      return 1;
1099
1100    /* onslaught */
1101   if (!strncmp("SS  CAL ", (const char *) &buf[0x0088], 8))
1102      return 1;
1103
1104    /* tram terror pirate */
1105   if ((len >= 0x3648 + 8) && !strncmp("SG NEPIE", (const char *) &buf[0x3648], 8))
1106      return 1;
1107
1108    /* breath of fire 3 chinese */
1109   if (buf[0x0007] == 0x1c && buf[0x0008] == 0x0a && buf[0x0009] == 0xb8 && buf[0x000a] == 0x0a)
1110      return 1;
1111
1112    /*tetris pirate */
1113   if ((len >= 0x1cbe + 5) && !strncmp("@TTI>", (const char *) &buf[0x1cbe], 5))
1114      return 1;
1115
1116   return 0;
1117}
1118
1119
1120
1121/* code taken directly from GoodGEN by Cowering */
1122static int genesis_is_SMD(unsigned char *buf,unsigned int len)
1123{
1124   if (buf[0x2080] == 'S' && buf[0x80] == 'E' && buf[0x2081] == 'G' && buf[0x81] == 'A')
1125      return 1;
1126   return genesis_is_funky_SMD(buf,len);
1127}
1128
1129
1130/******* Image loading *******/
1131
1132static DEVICE_IMAGE_LOAD( genesis_cart )
1133{
1134   unsigned char *ROM, *rawROM, *tmpROMnew, *tmpROM, *secondhalf;
1135   int relocate, length, ptr, x, sram_detected;
1136#ifdef LSB_FIRST
1137   unsigned char fliptemp;
1138#endif
1139
1140   genesis_sram = NULL;
1141   sram_detected = 0;
1142   genesis_sram_start = genesis_sram_end = 0;
1143   has_serial_eeprom = 0;
1144   cart_type = STANDARD;
1145
1146   if (image.software_entry() == NULL)
1147   {
1148      rawROM = image.device().machine->region("maincpu")->base();
1149      ROM = rawROM /*+ 512 */;
1150
1151      genesis_last_loaded_image_length = -1;
1152
1153      length = image.fread( rawROM + 0x2000, 0x600000);
1154
1155      logerror("image length = 0x%x\n", length);
1156
1157      /* is this a SMD file? */
1158      if (genesis_is_SMD(&rawROM[0x2200], (unsigned)length))
1159      {
1160         tmpROMnew = ROM;
1161         tmpROM = ROM + 0x2000 + 512;
1162
1163         for (ptr = 0; ptr < MAX_MD_CART_SIZE / (8192); ptr += 2)
1164         {
1165            for (x = 0; x < 8192; x++)
1166            {
1167               *tmpROMnew++ = *(tmpROM + ((ptr + 1) * 8192) + x);
1168               *tmpROMnew++ = *(tmpROM + ((ptr + 0) * 8192) + x);
1169            }
1170         }
1171
1172#ifdef LSB_FIRST
1173         tmpROMnew = ROM;
1174         for (ptr = 0; ptr < length; ptr += 2)
1175         {
1176            fliptemp = tmpROMnew[ptr];
1177            tmpROMnew[ptr] = tmpROMnew[ptr + 1];
1178            tmpROMnew[ptr + 1] = fliptemp;
1179         }
1180#endif
1181         genesis_last_loaded_image_length = length - 512;
1182         memcpy(&ROM[VIRGIN_COPY_GEN], &ROM[0x000000], MAX_MD_CART_SIZE);  /* store a copy of data for MACHINE_RESET processing */
1183
1184         relocate = 0;
1185
1186      }
1187
1188      /* is this a MD file? */
1189      else if ((rawROM[0x2080] == 'E') && (rawROM[0x2081] == 'A') &&
1190             (rawROM[0x2082] == 'M' || rawROM[0x2082] == 'G'))
1191      {
1192         tmpROMnew = (unsigned char *)osd_malloc(length);
1193         secondhalf = &tmpROMnew[length >> 1];
1194
1195         if (!tmpROMnew)
1196         {
1197            logerror("Memory allocation failed reading roms!\n");
1198            return IMAGE_INIT_FAIL;
1199         }
1200
1201         memcpy(tmpROMnew, ROM + 0x2000, length);
1202         for (ptr = 0; ptr < length; ptr += 2)
1203         {
1204
1205            ROM[ptr] = secondhalf[ptr >> 1];
1206            ROM[ptr + 1] = tmpROMnew[ptr >> 1];
1207         }
1208         free(tmpROMnew);
1209
1210#ifdef LSB_FIRST
1211         for (ptr = 0; ptr < length; ptr += 2)
1212         {
1213            fliptemp = ROM[ptr];
1214            ROM[ptr] = ROM[ptr+1];
1215            ROM[ptr+1] = fliptemp;
1216         }
1217#endif
1218         genesis_last_loaded_image_length = length;
1219         memcpy(&ROM[VIRGIN_COPY_GEN], &ROM[0x000000], MAX_MD_CART_SIZE);  /* store a copy of data for MACHINE_RESET processing */
1220         relocate = 0;
1221
1222      }
1223
1224      /* BIN it is, then */
1225      else
1226      {
1227         relocate = 0x2000;
1228         genesis_last_loaded_image_length = length;
1229
1230         for (ptr = 0; ptr < MAX_MD_CART_SIZE + relocate; ptr += 2)      /* mangle bytes for little endian machines */
1231         {
1232#ifdef LSB_FIRST
1233            int temp = ROM[relocate + ptr];
1234
1235            ROM[ptr] = ROM[relocate + ptr + 1];
1236            ROM[ptr + 1] = temp;
1237#else
1238            ROM[ptr] = ROM[relocate + ptr];
1239            ROM[ptr + 1] = ROM[relocate + ptr + 1];
1240#endif
1241         }
1242
1243         memcpy(&ROM[VIRGIN_COPY_GEN], &ROM[0x000000], MAX_MD_CART_SIZE);  /* store a copy of data for MACHINE_RESET processing */
1244      }
1245
1246      /* Default cartridge type */
1247      cart_type = STANDARD;
1248
1249      /* Detect carts which need additional handlers */
1250      {
1251        static const unsigned char smouse_sig[] = { 0x4d, 0xf9, 0x00, 0x40, 0x00, 0x02 },
1252         mjlover_sig[]   = { 0x13, 0xf9, 0x00, 0x40, 0x00, 0x00 }, // move.b  ($400000).l,($FFFF0C).l (partial)
1253         squir_sig[]      = { 0x26, 0x79, 0x00, 0xff, 0x00, 0xfa },
1254         bugsl_sig[]      = { 0x20, 0x12, 0x13, 0xc0, 0x00, 0xff },
1255         sbub_sig[]      = { 0x0c, 0x39, 0x00, 0x55, 0x00, 0x40 }, // cmpi.b  #$55,($400000).l
1256         lk3_sig[]      = { 0x0c, 0x01, 0x00, 0x30, 0x66, 0xe4 },
1257         sdk_sig[]      = { 0x48, 0xe7, 0xff, 0xfe, 0x52, 0x79 },
1258         redcliff_sig[]   = { 0x10, 0x39, 0x00, 0x40, 0x00, 0x04 }, // move.b  ($400004).l,d0
1259         redcl_en_sig[]   = { 0x50, 0x79, 0x40, 0x00, 0x40, 0x44 }, // move.b  ($400004).l,d0
1260         smb_sig[]      = { 0x20, 0x4d, 0x41, 0x52, 0x49, 0x4f },
1261         smb2_sig[]      = { 0x4e, 0xb9, 0x00, 0x0f, 0x25, 0x84 },
1262         kaiju_sig[]      = { 0x19, 0x7c, 0x00, 0x01, 0x00, 0x00 },
1263         chifi3_sig[]   = { 0xb6, 0x16, 0x66, 0x00, 0x00, 0x4a },
1264         lionk2_sig[]   = { 0x26, 0x79, 0x00, 0xff, 0x00, 0xf4 },
1265         rx3_sig[]      = { 0x66, 0x00, 0x00, 0x0e, 0x30, 0x3c },
1266         kof98_sig[]      = { 0x9b, 0xfc, 0x00, 0x00, 0x4a, 0x00 },
1267         s15in1_sig[]   = { 0x22, 0x3c, 0x00, 0xa1, 0x30, 0x00 },
1268         kof99_sig[]      = { 0x20, 0x3c, 0x30, 0x00, 0x00, 0xa1 }, // move.l  #$300000A1,d0
1269         radica_sig[]   = { 0x4e, 0xd0, 0x30, 0x39, 0x00, 0xa1 }, // jmp (a0) move.w ($a130xx),d0
1270         soulb_sig[]      = { 0x33, 0xfc, 0x00, 0x0c, 0x00, 0xff }, // move.w  #$C,($FF020A).l (what happens if check fails)
1271         s19in1_sig[]   = { 0x13, 0xc0, 0x00, 0xa1, 0x30, 0x38 },
1272         rockman_sig[]   = { 0xea, 0x80 };
1273
1274         switch (genesis_last_loaded_image_length)
1275         {
1276         case 0x80000:
1277             if (!allendianmemcmp(&ROM[0x08c8], &smouse_sig[0], sizeof(smouse_sig)))
1278            cart_type = SMOUSE;
1279
1280             if (!allendianmemcmp((char *)&ROM[0x7e30e], "SEGA", 4) ||
1281            !allendianmemcmp((char *)&ROM[0x7e100], "SEGA", 4) ||
1282            !allendianmemcmp((char *)&ROM[0x7e1e6], "SEGA", 4))
1283            cart_type = REALTEC;
1284
1285             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-50396", 10)) // NHLPA Hockey 93
1286            cart_type = EA_NHLPA;
1287
1288             if (!allendianmemcmp((char *)&ROM[0x0180], "GM MK-1215", 10)) // Evander Holyfield
1289            cart_type = WBOY_V;
1290
1291             break;
1292
1293         case 0xc0000:
1294
1295             if (!allendianmemcmp((char *)&ROM[0x0180], "GM G-4060 ", 8)) // Wonder Boy V
1296            cart_type = WBOY_V;
1297
1298             break;
1299
1300         case 0x100000:
1301             if (!allendianmemcmp(&ROM[0x01b24], &mjlover_sig[0], sizeof(mjlover_sig)))
1302            cart_type = MJLOVER;
1303
1304             if (!allendianmemcmp(&ROM[0x03b4], &squir_sig[0], sizeof(squir_sig)))
1305            cart_type = SQUIRRELK;
1306
1307             if (!allendianmemcmp(&ROM[0xee0d0], &bugsl_sig[0], sizeof(bugsl_sig)))
1308            cart_type = BUGSLIFE;
1309
1310             if (!allendianmemcmp((char *)&ROM[0x0172], "GAME : ELF WOR", 14))
1311            cart_type = ELFWOR;
1312
1313             if (!allendianmemcmp(&ROM[0x123e4], &sbub_sig[0], sizeof(sbub_sig)))
1314            cart_type = SBUBBOB;
1315
1316             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-50176", 10)) // Rings of Power
1317            cart_type = EA_NHLPA;
1318
1319             if (!allendianmemcmp((char *)&ROM[0x0180], "MK 00001211-00", 14)) // Sports Talk Baseball
1320            cart_type = WBOY_V;
1321
1322             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-120096-", 12)) // Micro Machines 2
1323            cart_type = CODE_MASTERS;
1324
1325             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-120146-", 12)) // Brian Lara Cricket 96 / Shane Wayne Cricket 96
1326            cart_type = CODE_MASTERS;
1327
1328             if (!allendianmemcmp((char *)&ROM[0x0190], "OJKRPTBVFCA     ", 0x10)) // Micro Machines '96 / Military TODO: better way to recognize these?
1329            cart_type = CODE_MASTERS;
1330             break;
1331
1332         case 0x200000:
1333             if (!allendianmemcmp(&ROM[0x18c6], &lk3_sig[0], sizeof(lk3_sig)))
1334            cart_type = LIONK3;
1335
1336             if (!allendianmemcmp(&ROM[0x220], &sdk_sig[0], sizeof(sdk_sig)))
1337            cart_type = SKINGKONG;
1338
1339             if (!allendianmemcmp(&ROM[0xce560], &redcliff_sig[0], sizeof(redcliff_sig)))
1340            cart_type = REDCLIFF;
1341
1342             if (!allendianmemcmp(&ROM[0xc8cb0], &smb_sig[0], sizeof(smb_sig)))
1343            cart_type = SMB;
1344
1345             if (!allendianmemcmp(&ROM[0xf24d6], &smb2_sig[0], sizeof(smb2_sig)))
1346            cart_type = SMB2;
1347
1348             if (!allendianmemcmp(&ROM[0x674e], &kaiju_sig[0], sizeof(kaiju_sig)))
1349            cart_type = KAIJU;
1350
1351             if (!allendianmemcmp(&ROM[0x1780], &chifi3_sig[0], sizeof(chifi3_sig)))
1352            cart_type = CHINFIGHT3;
1353
1354             if (!allendianmemcmp(&ROM[0x03c2], &lionk2_sig[0], sizeof(lionk2_sig)))
1355            cart_type = LIONK2;
1356
1357             if (!allendianmemcmp(&ROM[0xc8b90], &rx3_sig[0], sizeof(rx3_sig)))
1358            cart_type = ROCKMANX3;
1359
1360             if (!allendianmemcmp(&ROM[0x56ae2], &kof98_sig[0], sizeof(kof98_sig)))
1361            cart_type = KOF98;
1362
1363             if (!allendianmemcmp(&ROM[0x17bb2], &s15in1_sig[0], sizeof(s15in1_sig)))
1364            cart_type = SUP15IN1;
1365
1366             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081326 ", 12)) // NBA Jam
1367            cart_type = NBA_JAM;
1368
1369             if (!allendianmemcmp((char *)&ROM[0x0180], "GM MK-1228", 10)) // Greatest Heavyweight of the Ring
1370            cart_type = WBOY_V;
1371
1372             if ((!allendianmemcmp((char *)&ROM[0x0180], "GM T-12046", 10)) || // Mega Man
1373            (!allendianmemcmp((char *)&ROM[0x0180], "GM T-12053", 10) && !allendianmemcmp(&ROM[0x18e], &rockman_sig[0], sizeof(rockman_sig)))) // / Rock Man (EEPROM version)
1374            cart_type = WBOY_V;
1375
1376             break;
1377
1378         case 0x200005:
1379             if (!allendianmemcmp(&ROM[0xce564], &redcl_en_sig[0], sizeof(redcliff_sig)))
1380            cart_type = REDCL_EN;
1381             break;
1382
1383         case 0x300000:
1384             if (!allendianmemcmp(&ROM[0x220], &sdk_sig[0], sizeof(sdk_sig)))
1385            cart_type = SDK99;
1386
1387             if (!allendianmemcmp(&ROM[0x1fd0d2], &kof99_sig[0], sizeof(kof99_sig)))
1388            cart_type = KOF99;
1389
1390             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-81406", 10)) // NBA Jam TE
1391            cart_type = NBA_JAM_TE;
1392
1393             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081276 ", 12)) // NFL Quarterback Club
1394            cart_type = NBA_JAM_TE;
1395
1396             break;
1397
1398         case 0x400000:
1399             if (!allendianmemcmp(&ROM[0x3c031c], &radica_sig[0], sizeof(radica_sig)) ||
1400            !allendianmemcmp(&ROM[0x3f031c], &radica_sig[0], sizeof(radica_sig))) // ssf+gng + radica vol1
1401            cart_type = RADICA;
1402
1403             if (!allendianmemcmp(&ROM[0x028460], &soulb_sig[0], sizeof(soulb_sig)))
1404            cart_type = SOULBLAD;
1405
1406             if (!allendianmemcmp(&ROM[0x1e700], &s19in1_sig[0], sizeof(s19in1_sig)))
1407            cart_type = SUP19IN1;
1408
1409             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081586-", 12)) // NFL Quarterback Club 96
1410            cart_type = NFL_QB_96;
1411
1412             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081576 ", 12)) // College Slam
1413            cart_type = C_SLAM;
1414
1415             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-81476", 10)) // Big Hurt Baseball
1416            cart_type = C_SLAM;
1417
1418             break;
1419
1420         case 0x500000:
1421             if (!allendianmemcmp((char *)&ROM[0x0120], "SUPER STREET FIGHTER2 ", 22))
1422            cart_type = SSF2;
1423             break;
1424
1425         default:
1426             break;
1427         }
1428      }
1429
1430      logerror("cart type: %d\n", cart_type);
1431
1432      genesis_sram = NULL;
1433      sram_detected = 0;
1434      genesis_sram_start = genesis_sram_end = 0;
1435      has_serial_eeprom = 0;
1436
1437      /* check if cart has battery save */
1438
1439      /* For games using SRAM, unfortunately there are ROMs without info
1440           about it in header. The solution adopted is do the mapping anyway,
1441           then active SRAM later if the game will access it. */
1442
1443      if (ROM[0x1b1] == 'R' && ROM[0x1b0] == 'A')
1444      {
1445         /* SRAM info found in header */
1446         genesis_sram_start = (ROM[0x1b5] << 24 | ROM[0x1b4] << 16 | ROM[0x1b7] << 8 | ROM[0x1b6]);
1447         genesis_sram_end = (ROM[0x1b9] << 24 | ROM[0x1b8] << 16 | ROM[0x1bb] << 8 | ROM[0x1ba]);
1448
1449         if ((genesis_sram_start > genesis_sram_end) || ((genesis_sram_end - genesis_sram_start) >= 0x10000))   // we assume at most 64k of SRAM (HazeMD uses at most 64k). is this correct?
1450           genesis_sram_end = genesis_sram_start + 0x0FFFF;
1451
1452         /* for some games using serial EEPROM, difference between SRAM
1453               end to start is 0 or 1. Currently EEPROM is not emulated. */
1454         if ((genesis_sram_end - genesis_sram_start) < 2)
1455           has_serial_eeprom = 1;
1456         else
1457           sram_detected = 1;
1458      }
1459      else
1460      {
1461         /* set default SRAM positions, with size = 64k */
1462         genesis_sram_start = 0x200000;
1463         genesis_sram_end = genesis_sram_start + 0xffff;
1464      }
1465
1466      if (genesis_sram_start & 1)
1467        genesis_sram_start -= 1;
1468
1469      if (!(genesis_sram_end & 1))
1470        genesis_sram_end += 1;
1471
1472      /* calculate backup RAM location */
1473      megadriv_backupram = (UINT16*) (ROM + (genesis_sram_start & 0x3fffff));
1474
1475      /* Until serial EEPROM is emulated, clears one byte at beginning of
1476           backup RAM, but only if the value is 0xFFFF, to not break MLBPA Sports
1477           Talk Baseball. With this hack some games at least run (NBA Jam, Evander
1478           Holyfield's Real Deal Boxing, Greatest Heavyweights of the Ring) */
1479      //if (has_serial_eeprom && megadriv_backupram[0] == 0xffff)
1480      //  megadriv_backupram[0] = 0xff00;
1481
1482      if (sram_detected)
1483        logerror("SRAM detected from header: starting location %X - SRAM Length %X\n", genesis_sram_start, genesis_sram_end - genesis_sram_start + 1);
1484   }
1485   else /* Handle pcb with the softlist info */
1486   {
1487      int      pcb_id;
1488      const char   *pcb_name;
1489
1490      length = image.get_software_region_length("rom");
1491      ROM = image.device().machine->region("maincpu")->base();
1492      memcpy(ROM, image.get_software_region("rom"), length);
1493
1494      genesis_last_loaded_image_length = length;
1495      megadriv_backupram = NULL;
1496      if ((pcb_name = image.get_feature("pcb")) == NULL)
1497        pcb_id = STD_ROM;
1498      else
1499        pcb_id = md_get_pcb_id(pcb_name);
1500
1501      switch (pcb_id)
1502      {
1503         /* Sega PCB */
1504        case SEGA_6584A:
1505         fatalerror("Need Serial EEPROM emulation");
1506         break;
1507        case SEGA_5921:
1508        case SEGA_6278A:
1509         genesis_sram_start = 0x200000;
1510         genesis_sram_end = genesis_sram_start + 0x3fff;
1511         sram_detected = 1;
1512         megadriv_backupram = (UINT16*) (ROM + (genesis_sram_start & 0x3fffff));
1513         break;
1514        case SEGA_6658A:
1515         genesis_sram_start = 0x200000;
1516         genesis_sram_end = genesis_sram_start + 0x3ff;
1517         sram_detected = 1;
1518         megadriv_backupram = (UINT16*) (ROM + (genesis_sram_start & 0x3fffff));
1519         break;
1520
1521         /* Codemasters PCB (J-Carts) */
1522        case CM_JCART:
1523         memory_install_read16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_r);
1524         memory_install_write16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_w);
1525         break;
1526        case CM_JCART_SEPROM:
1527         memory_install_read16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_r);
1528         memory_install_write16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_w);
1529         /* TODO add SEPROM part */
1530         break;
1531      }
1532   }
1533
1534   return IMAGE_INIT_PASS;
1535}
1536
1537/******* Image unloading (with SRAM saving) *******/
1538
1539static DEVICE_IMAGE_UNLOAD( genesis_cart )
1540{
1541   /* Write out the battery file if necessary */
1542   if (genesis_sram != NULL)
1543   {
1544      image.battery_save(genesis_sram, genesis_sram_end - genesis_sram_start + 1);
1545      free(genesis_sram);
1546   }
1547}
1548
1549
1550/******* 32X image loading *******/
1551
1552// FIXME: non-softlist loading should keep using ROM_CART_LOAD in the ROM definitions,
1553// once we better integrate softlist with the old loading procedures
1554static DEVICE_IMAGE_LOAD( _32x_cart )
1555{
1556   UINT32 length;
1557   UINT8 *temp_copy;
1558   UINT16 *ROM16;
1559   UINT32 *ROM32;
1560   int i;
1561
1562   if (image.software_entry() == NULL)
1563   {
1564      length = image.length();
1565      temp_copy = auto_alloc_array(image.device().machine, UINT8, length);
1566      image.fread( temp_copy, length);
1567   }
1568   else
1569   {
1570      length = image.get_software_region_length("rom");
1571      temp_copy = auto_alloc_array(image.device().machine, UINT8, length);
1572      memcpy(temp_copy, image.get_software_region("rom"), length);
1573   }
1574
1575   /* Copy the cart image in the locations the driver expects */
1576   // Notice that, by using pick_integer, we are sure the code works on both LE and BE machines
1577   ROM16 = (UINT16 *) image.device().machine->region("gamecart")->base();
1578   for (i = 0; i < length; i += 2)
1579      ROM16[i / 2] = pick_integer_be(temp_copy, i, 2);
1580
1581   ROM32 = (UINT32 *) image.device().machine->region("gamecart_sh2")->base();
1582   for (i = 0; i < length; i += 4)
1583      ROM32[i / 4] = pick_integer_be(temp_copy, i, 4);
1584
1585   ROM16 = (UINT16 *) image.device().machine->region("maincpu")->base();
1586   for (i = 0x00; i < length; i += 2)
1587      ROM16[i / 2] = pick_integer_be(temp_copy, i, 2);
1588
1589   auto_free(image.device().machine, temp_copy);
1590
1591   cart_type = NBA_JAM_TE;
1592
1593   return IMAGE_INIT_PASS;
1594}
1595
1596/******* Cart getinfo *******/
1597
1598MACHINE_CONFIG_FRAGMENT( genesis_cartslot )
1599   MCFG_CARTSLOT_ADD("cart")
1600   MCFG_CARTSLOT_EXTENSION_LIST("smd,bin,md,gen")
1601   MCFG_CARTSLOT_MANDATORY
1602   MCFG_CARTSLOT_INTERFACE("megadriv_cart")
1603   MCFG_CARTSLOT_LOAD(genesis_cart)
1604   MCFG_CARTSLOT_UNLOAD(genesis_cart)
1605   MCFG_SOFTWARE_LIST_ADD("cart_list","megadriv")
1606MACHINE_CONFIG_END
1607
1608MACHINE_CONFIG_FRAGMENT( _32x_cartslot )
1609   MCFG_CARTSLOT_ADD("cart")
1610   MCFG_CARTSLOT_EXTENSION_LIST("32x,bin")
1611   MCFG_CARTSLOT_MANDATORY
1612   MCFG_CARTSLOT_INTERFACE("_32x_cart")
1613   MCFG_CARTSLOT_LOAD(_32x_cart)
1614   MCFG_SOFTWARE_LIST_ADD("cart_list","32x")
1615MACHINE_CONFIG_END
1616
1617MACHINE_CONFIG_FRAGMENT( pico_cartslot )
1618   MCFG_CARTSLOT_ADD("cart")
1619   MCFG_CARTSLOT_EXTENSION_LIST("bin")
1620   MCFG_CARTSLOT_MANDATORY
1621   MCFG_CARTSLOT_INTERFACE("pico_cart")
1622   MCFG_CARTSLOT_LOAD(genesis_cart)
1623   MCFG_CARTSLOT_UNLOAD(genesis_cart)
1624   MCFG_SOFTWARE_LIST_ADD("cart_list","pico")
1625MACHINE_CONFIG_END
src/mame/includes/megadriv.h
r10033r10034
22#define MASTER_CLOCK_PAL  53203424
33#define SEGACD_CLOCK      12500000
44
5#include "imagedev/cartslot.h"
56
67extern DRIVER_INIT( megadriv_c2 );
78extern DRIVER_INIT( megadrie );
r10033r10034
112113   int mt_bank_partial;
113114   int mt_bank_addr;
114115};
116
117/*----------- defined in machine/megadriv.c -----------*/
118
119MACHINE_RESET( md_mappers );
120
121MACHINE_CONFIG_EXTERN( genesis_cartslot );
122MACHINE_CONFIG_EXTERN( _32x_cartslot );
123MACHINE_CONFIG_EXTERN( pico_cartslot );
124
125WRITE16_HANDLER( jcart_ctrl_w );
126READ16_HANDLER( jcart_ctrl_r );
127
src/mame/drivers/megadriv.c
r10033r10034
92609260   DRIVER_INIT_CALL(megadriv);
92619261}
92629262
9263#if 1
92649263
9265ROM_START( 32x_bios )
9264/*************************************
9265 *
9266 *  Input handlers
9267 *
9268 *************************************/
92669269
9267   ROM_REGION16_BE( 0x400000, "gamecart", ROMREGION_ERASEFF ) /* 68000 Code */
9270/* We need to always initialize 6 buttons pad */
9271static emu_timer *mess_io_timeout[3];
9272static int mess_io_stage[3];
92689273
9269   ROM_REGION32_BE( 0x400000, "gamecart_sh2", 0 ) /* Copy for the SH2 */
9270   ROM_COPY( "gamecart", 0x0, 0x0, 0x400000)
9274static TIMER_CALLBACK( mess_io_timeout_timer_callback )
9275{
9276   mess_io_stage[(int)(FPTR)ptr] = -1;
9277}
92719278
9279/* J-Cart controller port */
9280
9281static UINT8   jcart_io_data[2];
9282
9283WRITE16_HANDLER( jcart_ctrl_w )
9284{
9285   jcart_io_data[0] = (data & 1) << 6;
9286   jcart_io_data[1] = (data & 1) << 6;
9287}
9288
9289READ16_HANDLER( jcart_ctrl_r )
9290{
9291   UINT16   retdata = 0;
9292   UINT8      joy[2];
9293
9294   if (jcart_io_data[0] & 0x40)
9295   {
9296      joy[0] = input_port_read_safe(space->machine, "JCART3_3B", 0);
9297      joy[1] = input_port_read_safe(space->machine, "JCART4_3B", 0);
9298      retdata = (jcart_io_data[0] & 0x40) | joy[0] | (joy[1] << 8);
9299   }
9300   else
9301   {
9302      joy[0] = ((input_port_read_safe(space->machine, "JCART3_3B", 0) & 0xc0) >> 2) |
9303        (input_port_read_safe(space->machine, "JCART3_3B", 0) & 0x03);
9304      joy[1] = ((input_port_read_safe(space->machine, "JCART4_3B", 0) & 0xc0) >> 2) |
9305        (input_port_read_safe(space->machine, "JCART4_3B", 0) & 0x03);
9306      retdata = (jcart_io_data[0] & 0x40) | joy[0] | (joy[1] << 8);
9307   }
9308   return retdata;
9309}
9310
9311static void mess_init_6buttons_pad(running_machine *machine)
9312{
9313   int i;
9314
9315   for (i = 0; i < 3; i++)
9316   {
9317      mess_io_timeout[i] = timer_alloc(machine, mess_io_timeout_timer_callback, (void*)(FPTR)i);
9318      mess_io_stage[i] = -1;
9319   }
9320}
9321
9322/* These overwrite the MAME ones in DRIVER_INIT */
9323/* They're needed to give the users the choice between different controllers */
9324static UINT8 mess_md_io_read_data_port(running_machine *machine, int portnum)
9325{
9326   static const char *const pad6names[2][4] = {
9327      { "PAD1_6B", "PAD2_6B", "UNUSED", "UNUSED" },
9328      { "EXTRA1", "EXTRA2", "UNUSED", "UNUSED" }
9329   };
9330   static const char *const pad3names[4] = { "PAD1_3B", "PAD2_3B", "UNUSED", "UNUSED" };
9331
9332   UINT8 retdata;
9333   int controller;
9334   UINT8 helper_6b = (megadrive_io_ctrl_regs[portnum] & 0x3f) | 0xc0; // bits 6 & 7 always come from megadrive_io_data_regs
9335   UINT8 helper_3b = (megadrive_io_ctrl_regs[portnum] & 0x7f) | 0x80; // bit 7 always comes from megadrive_io_data_regs
9336
9337   switch (portnum)
9338   {
9339      case 0:
9340         controller = (input_port_read(machine, "CTRLSEL") & 0x0f);
9341         break;
9342
9343      case 1:
9344         controller = (input_port_read(machine, "CTRLSEL") & 0xf0);
9345         break;
9346
9347      default:
9348         controller = 0;
9349         break;
9350   }
9351
9352   /* Are we using a 6 buttons Joypad? */
9353   if (controller)
9354   {
9355      if (megadrive_io_data_regs[portnum] & 0x40)
9356      {
9357         if (mess_io_stage[portnum] == 2)
9358         {
9359            /* here we read B, C & the additional buttons */
9360            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
9361                     (((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0x30) |
9362                        (input_port_read_safe(machine, pad6names[1][portnum], 0) & 0x0f)) & ~helper_6b);
9363         }
9364         else
9365         {
9366            /* here we read B, C & the directional buttons */
9367            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
9368                     ((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0x3f) & ~helper_6b);
9369         }
9370      }
9371      else
9372      {
9373         if (mess_io_stage[portnum] == 1)
9374         {
9375            /* here we read ((Start & A) >> 2) | 0x00 */
9376            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
9377                     (((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0xc0) >> 2) & ~helper_6b);
9378         }
9379         else if (mess_io_stage[portnum]==2)
9380         {
9381            /* here we read ((Start & A) >> 2) | 0x0f */
9382            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
9383                     ((((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0xc0) >> 2) | 0x0f) & ~helper_6b);
9384         }
9385         else
9386         {
9387            /* here we read ((Start & A) >> 2) | Up and Down */
9388            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
9389                     ((((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0xc0) >> 2) |
9390                        (input_port_read_safe(machine, pad6names[0][portnum], 0) & 0x03)) & ~helper_6b);
9391         }
9392      }
9393
9394   //  mame_printf_debug("read io data port stage %d port %d %02x\n",mess_io_stage[portnum],portnum,retdata);
9395
9396      retdata |= (retdata << 8);
9397   }
9398   /* Otherwise it's a 3 buttons Joypad */
9399   else
9400   {
9401      if (megadrive_io_data_regs[portnum] & 0x40)
9402      {
9403         /* here we read B, C & the directional buttons */
9404         retdata = (megadrive_io_data_regs[portnum] & helper_3b) |
9405                  (((input_port_read_safe(machine, pad3names[portnum], 0) & 0x3f) | 0x40) & ~helper_3b);
9406      }
9407      else
9408      {
9409         /* here we read ((Start & A) >> 2) | Up and Down */
9410         retdata = (megadrive_io_data_regs[portnum] & helper_3b) |
9411                  ((((input_port_read_safe(machine, pad3names[portnum], 0) & 0xc0) >> 2) |
9412                     (input_port_read_safe(machine, pad3names[portnum], 0) & 0x03) | 0x40) & ~helper_3b);
9413      }
9414   }
9415
9416   return retdata;
9417}
9418
9419
9420static void mess_md_io_write_data_port(running_machine *machine, int portnum, UINT16 data)
9421{
9422   int controller;
9423
9424   switch (portnum)
9425   {
9426      case 0:
9427         controller = (input_port_read(machine, "CTRLSEL") & 0x0f);
9428         break;
9429
9430      case 1:
9431         controller = (input_port_read(machine, "CTRLSEL") & 0xf0);
9432         break;
9433
9434      default:
9435         controller = 0;
9436         break;
9437   }
9438
9439   if (controller)
9440   {
9441      if (megadrive_io_ctrl_regs[portnum] & 0x40)
9442      {
9443         if (((megadrive_io_data_regs[portnum] & 0x40) == 0x00) && ((data & 0x40) == 0x40))
9444         {
9445            mess_io_stage[portnum]++;
9446            timer_adjust_oneshot(mess_io_timeout[portnum], machine->device<cpu_device>("maincpu")->cycles_to_attotime(8192), 0);
9447         }
9448
9449      }
9450   }
9451   megadrive_io_data_regs[portnum] = data;
9452   //mame_printf_debug("Writing IO Data Register #%d data %04x\n",portnum,data);
9453}
9454
9455
9456/*************************************
9457 *
9458 *  Input ports
9459 *
9460 *************************************/
9461
9462
9463static INPUT_PORTS_START( md )
9464   PORT_START("CTRLSEL")   /* Controller selection */
9465   PORT_CATEGORY_CLASS( 0x0f, 0x00, "Player 1 Controller" )
9466   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 10 )
9467   PORT_CATEGORY_ITEM( 0x01, "Joystick 6 Buttons", 11 )
9468//  PORT_CATEGORY_ITEM( 0x02, "Sega Mouse", 12 )
9469/* there exists both a 2 buttons version of the Mouse (Jpn ver, to be used with RPGs, it
9470    can aslo be used as trackball) and a 3 buttons version (US ver, no trackball feats.) */
9471//  PORT_CATEGORY_ITEM( 0x03, "Sega Menacer", 13 )
9472//  PORT_CATEGORY_ITEM( 0x04, "Konami Justifier", 14 )
9473//  PORT_CATEGORY_ITEM( 0x05, "Team Player (Sega Multitap)", 15 )
9474//  PORT_CATEGORY_ITEM( 0x06, "4-Play (EA Multitap)", 16 )
9475//  PORT_CATEGORY_ITEM( 0x07, "J-Cart", 17 )
9476   PORT_CATEGORY_CLASS( 0xf0, 0x00, "Player 2 Controller" )
9477   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 20 )
9478   PORT_CATEGORY_ITEM( 0x10, "Joystick 6 Buttons", 21 )
9479   PORT_CATEGORY_CLASS( 0xf00, 0x00, "Player 3 Controller (J-Cart)" )
9480   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 30 )
9481   PORT_CATEGORY_CLASS( 0xf000, 0x00, "Player 4 Controller (J-Cart)" )
9482   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 40 )
9483
9484   PORT_START("PAD1_3B")      /* Joypad 1 (3 button + start) NOT READ DIRECTLY */
9485   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
9486   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
9487   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
9488   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
9489   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("P1 B") PORT_CATEGORY(10)
9490   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("P1 C") PORT_CATEGORY(10)
9491   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("P1 A") PORT_CATEGORY(10)
9492   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(1) PORT_CATEGORY(10)
9493
9494   PORT_START("PAD2_3B")      /* Joypad 2 (3 button + start) NOT READ DIRECTLY */
9495   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
9496   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
9497   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
9498   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
9499   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2) PORT_NAME("P2 B") PORT_CATEGORY(20)
9500   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2) PORT_NAME("P2 C") PORT_CATEGORY(20)
9501   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P2 A") PORT_CATEGORY(20)
9502   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(2) PORT_CATEGORY(20)
9503
9504   PORT_START("JCART3_3B")      /* Joypad 3 on J-Cart (3 button + start) */
9505   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
9506   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
9507   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
9508   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
9509   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(3) PORT_NAME("P3 B") PORT_CATEGORY(30)
9510   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(3) PORT_NAME("P3 C") PORT_CATEGORY(30)
9511   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(3) PORT_NAME("P3 A") PORT_CATEGORY(30)
9512   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(3) PORT_CATEGORY(30)
9513
9514   PORT_START("JCART4_3B")      /* Joypad 4 on J-Cart (3 button + start) */
9515   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
9516   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
9517   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
9518   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
9519   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(4) PORT_NAME("P4 B") PORT_CATEGORY(40)
9520   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(4) PORT_NAME("P4 C") PORT_CATEGORY(40)
9521   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(4) PORT_NAME("P4 A") PORT_CATEGORY(40)
9522   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(4) PORT_CATEGORY(40)
9523
9524   PORT_START("PAD1_6B")      /* Joypad 1 (6 button + start + mode) NOT READ DIRECTLY */
9525   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
9526   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
9527   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
9528   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
9529   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("P1 B") PORT_CATEGORY(11)
9530   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("P1 C") PORT_CATEGORY(11)
9531   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("P1 A") PORT_CATEGORY(11)
9532   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(1) PORT_CATEGORY(11)
9533
9534   PORT_START("EXTRA1")   /* Extra buttons for Joypad 1 (6 button + start + mode) NOT READ DIRECTLY */
9535   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(1) PORT_NAME("P1 Z") PORT_CATEGORY(11)
9536   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(1) PORT_NAME("P1 Y") PORT_CATEGORY(11)
9537   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(1) PORT_NAME("P1 X") PORT_CATEGORY(11)
9538   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(1) PORT_NAME("P1 Mode") PORT_CATEGORY(11)
9539
9540   PORT_START("PAD2_6B")      /* Joypad 2 (6 button + start + mode) NOT READ DIRECTLY */
9541   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
9542   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
9543   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
9544   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
9545   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2) PORT_NAME("P2 B") PORT_CATEGORY(21)
9546   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2) PORT_NAME("P2 C") PORT_CATEGORY(21)
9547   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P2 A") PORT_CATEGORY(21)
9548   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(2) PORT_CATEGORY(21)
9549
9550   PORT_START("EXTRA2")   /* Extra buttons for Joypad 2 (6 button + start + mode) NOT READ DIRECTLY */
9551   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(2) PORT_NAME("P2 Z") PORT_CATEGORY(21)
9552   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(2) PORT_NAME("P2 Y") PORT_CATEGORY(21)
9553   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(2) PORT_NAME("P2 X") PORT_CATEGORY(21)
9554   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(2) PORT_NAME("P2 Mode") PORT_CATEGORY(21)
9555
9556   PORT_START("RESET")      /* Buttons on Genesis Console */
9557   PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_SERVICE1 ) PORT_NAME("Reset Button") PORT_IMPULSE(1) // reset, resets 68k (and..?)
9558INPUT_PORTS_END
9559
9560
9561/* MegaDrive inputs + Fake Region Selection */
9562/* We need this as long as we only have the US version of the SVP add-on, otherwise we could not play
9563   Non-US Virtua Racing versions. It is also handy to develop add-ons emulation without adding each
9564   region variants, while they do not even work. Once emulation is working this must disappear.  */
9565static INPUT_PORTS_START( md_sel )
9566   PORT_INCLUDE( md )
9567
9568   PORT_START("REGION")
9569   /* Region setting for Console */
9570   PORT_CONFNAME( 0x000f, 0x0000, DEF_STR( Region ) )
9571   PORT_CONFSETTING(      0x0000, "Use Default Choice" )
9572   PORT_CONFSETTING(      0x0001, "US (NTSC, 60fps)" )
9573   PORT_CONFSETTING(      0x0002, "Japan (NTSC, 60fps)" )
9574   PORT_CONFSETTING(      0x0003, "Europe (PAL, 50fps)" )
9575INPUT_PORTS_END
9576
9577
9578/*************************************
9579 *
9580 *  Machine driver
9581 *
9582 *************************************/
9583
9584static MACHINE_START( ms_megadriv )
9585{
9586   mess_init_6buttons_pad(machine);
9587}
9588
9589static MACHINE_RESET( ms_megadriv )
9590{
9591   MACHINE_RESET_CALL( megadriv );
9592   MACHINE_RESET_CALL( md_mappers );
9593}
9594
9595static MACHINE_CONFIG_DERIVED( ms_megadriv, megadriv )
9596
9597   MCFG_MACHINE_START( ms_megadriv )
9598   MCFG_MACHINE_RESET( ms_megadriv )
9599
9600   MCFG_FRAGMENT_ADD( genesis_cartslot )
9601MACHINE_CONFIG_END
9602
9603static MACHINE_CONFIG_DERIVED( ms_megadpal, megadpal )
9604
9605   MCFG_MACHINE_START( ms_megadriv )
9606   MCFG_MACHINE_RESET( ms_megadriv )
9607
9608   MCFG_FRAGMENT_ADD( genesis_cartslot )
9609MACHINE_CONFIG_END
9610
9611static MACHINE_CONFIG_DERIVED( ms_megdsvp, megdsvp )
9612
9613   MCFG_MACHINE_START( ms_megadriv )
9614   MCFG_MACHINE_RESET( ms_megadriv )
9615
9616   MCFG_FRAGMENT_ADD( genesis_cartslot )
9617MACHINE_CONFIG_END
9618
9619
9620
9621/*************************************
9622 *
9623 *  ROM definition(s)
9624 *
9625 *************************************/
9626
9627
9628/* we don't use the bios rom (it's not needed and only provides security on early models) */
9629
9630ROM_START(genesis)
9631   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
9632   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
9633ROM_END
9634
9635ROM_START(gensvp)
9636   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
9637   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
9638ROM_END
9639
9640ROM_START(megadriv)
9641   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
9642   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
9643ROM_END
9644
9645ROM_START(megadrij)
9646   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
9647   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
9648ROM_END
9649
9650
9651/*************************************
9652 *
9653 *  Driver initialization
9654 *
9655 *************************************/
9656
9657static DRIVER_INIT( mess_md_common )
9658{
9659   megadrive_io_read_data_port_ptr = mess_md_io_read_data_port;
9660   megadrive_io_write_data_port_ptr = mess_md_io_write_data_port;
9661}
9662
9663static DRIVER_INIT( genesis )
9664{
9665   DRIVER_INIT_CALL(megadriv);
9666   DRIVER_INIT_CALL(mess_md_common);
9667}
9668
9669static DRIVER_INIT( gensvp )
9670{
9671   DRIVER_INIT_CALL(megadriv);
9672   DRIVER_INIT_CALL(mess_md_common);
9673}
9674
9675static DRIVER_INIT( md_eur )
9676{
9677   DRIVER_INIT_CALL(megadrie);
9678   DRIVER_INIT_CALL(mess_md_common);
9679}
9680
9681static DRIVER_INIT( md_jpn )
9682{
9683   DRIVER_INIT_CALL(megadrij);
9684   DRIVER_INIT_CALL(mess_md_common);
9685}
9686
9687/****************************************** SegaCD & 32X emulation ****************************************/
9688
9689/* Very preliminary skeleton code from David Haywood, borrowed for MESS by Fabio Priuli */
9690/* These are only included to document BIOS informations currently available */
9691
9692static DRIVER_INIT( mess_32x )
9693{
9694   DRIVER_INIT_CALL(_32x);
9695   DRIVER_INIT_CALL(mess_md_common);
9696}
9697
9698static MACHINE_CONFIG_DERIVED( ms_32x, genesis_32x )
9699
9700   MCFG_FRAGMENT_ADD( _32x_cartslot )
9701MACHINE_CONFIG_END
9702
9703
9704ROM_START( 32x )
9705   ROM_REGION16_BE( 0x400000, "gamecart", ROMREGION_ERASE00 ) /* 68000 Code */
9706//  ROM_CART_LOAD("cart", 0x000000, 0x400000, ROM_NOMIRROR)
9707
9708   ROM_REGION32_BE( 0x400000, "gamecart_sh2", ROMREGION_ERASE00 ) /* Copy for the SH2 */
9709//  ROM_CART_LOAD("cart", 0x000000, 0x400000, ROM_NOMIRROR)
9710
92729711   ROM_REGION16_BE( 0x400000, "32x_68k_bios", 0 ) /* 68000 Code */
9273//  ROM_COPY( "gamecart", 0x0, 0x0, 0x400000)
92749712   ROM_LOAD( "32x_g_bios.bin", 0x000000,  0x000100, CRC(5c12eae8) SHA1(dbebd76a448447cb6e524ac3cb0fd19fc065d944) )
92759713
92769714   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
92779715   // temp, rom should only be visible here when one of the regs is set, tempo needs it
9716//  ROM_CART_LOAD("cart", 0x000000, 0x400000, ROM_NOMIRROR)
92789717   ROM_COPY( "32x_68k_bios", 0x0, 0x0, 0x100)
92799718
92809719   ROM_REGION( 0x400000, "32x_master_sh2", 0 ) /* SH2 Code */
r10033r10034
92839722   ROM_SYSTEM_BIOS( 1, "sdk", "Mars Version 1.0 (early sdk)" )
92849723   ROMX_LOAD( "32x_m_bios_sdk.bin", 0x000000,  0x000800, BAD_DUMP CRC(c7102c53) SHA1(ed73a47f186b373b8eff765f84ef26c3d9ef6cb0), ROM_BIOS(2) )
92859724
9286
92879725   ROM_REGION( 0x400000, "32x_slave_sh2", 0 ) /* SH2 Code */
92889726   ROM_LOAD( "32x_s_bios.bin", 0x000000,  0x000400, CRC(bfda1fe5) SHA1(4103668c1bbd66c5e24558e73d4f3f92061a109a) )
92899727ROM_END
92909728
9729/* We need proper names for most of these BIOS ROMs! */
92919730ROM_START( segacd )
92929731   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9293   ROM_LOAD( "segacd_model2_bios_2_11_u.bin", 0x000000,  0x020000, CRC(2e49d72c) SHA1(328a3228c29fba244b9db2055adc1ec4f7a87e6b) )
9732   /* v1.10 */
9733   ROM_LOAD( "segacd_model1_bios_1_10_u.bin", 0x000000,  0x020000, CRC(c6d10268) SHA1(f4f315adcef9b8feb0364c21ab7f0eaf5457f3ed) )
92949734ROM_END
92959735
9736ROM_START( megacd )
9737   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9738   /* v1.00, confirmed good dump */
9739   ROM_LOAD( "megacd_model1_bios_1_00_e.bin", 0x000000,  0x020000, CRC(529ac15a) SHA1(f891e0ea651e2232af0c5c4cb46a0cae2ee8f356) )
9740ROM_END
9741
9742ROM_START( megacdj )
9743   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9744   ROM_SYSTEM_BIOS(0, "v100s", "v1.00S") /* also used for Asia PAL - not handled yet */
9745   ROMX_LOAD( "megacd_model1_bios_1_00s_j.bin", 0x000000,  0x020000, CRC(550f30bb) SHA1(e4193c6ae44c3cea002707d2a88f1fbcced664de), ROM_BIOS(1) )
9746   ROM_SYSTEM_BIOS(1, "v100p", "v1.00P")
9747   ROMX_LOAD( "megacd_model1_bios_1_00p_j.bin", 0x000000,  0x020000, CRC(9d2da8f2) SHA1(4846f448160059a7da0215a5df12ca160f26dd69), ROM_BIOS(2) )
9748ROM_END
9749
9750ROM_START( segacd2 )
9751   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9752   ROM_SYSTEM_BIOS(0, "v211x", "Model 2 v2.11X")
9753   ROMX_LOAD( "mpr-15764-t.bin", 0x000000,  0x020000, CRC(2e49d72c) SHA1(328a3228c29fba244b9db2055adc1ec4f7a87e6b), ROM_BIOS(1) )
9754   ROM_SYSTEM_BIOS(1, "v200", "Model 2 v2.00") /* verified dump */
9755   ROMX_LOAD( "us_scd2_930314.bin", 0x000000,  0x020000, CRC(8af65f58) SHA1(5a8c4b91d3034c1448aac4b5dc9a6484fce51636), ROM_BIOS(2) )
9756   /* this is reportedly a bad dump, it has many differences from the verified dump and does not boot in Kega */
9757   /* ROMX_LOAD( "segacd_model2_bios_2_00_u.bin", 0x000000,  0x020000, CRC(340b4be4) SHA1(bd3ee0c8ab732468748bf98953603ce772612704), ROM_BIOS(2) ) */
9758   ROM_SYSTEM_BIOS(2, "v200w", "Model 2 v2.00W")
9759   ROMX_LOAD( "segacd_model2_bios_2_00w_u.bin", 0x000000,  0x020000, CRC(9f6f6276) SHA1(5adb6c3af218c60868e6b723ec47e36bbdf5e6f0), ROM_BIOS(3) )
9760ROM_END
9761
9762ROM_START( megacd2 )
9763   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9764   ROM_SYSTEM_BIOS(0, "v200w", "v2.00W")   // confirmed good dump
9765   ROMX_LOAD( "mpr-15512a.bin", 0x000000,  0x020000, CRC(2a4b82b5) SHA1(45134ef8655b9d06b130726786efe2f8b1d430a3), ROM_BIOS(1) )
9766   ROM_SYSTEM_BIOS(1, "v200", "v2.00")
9767   ROMX_LOAD( "mpr-15512-t.bin", 0x000000,  0x020000, CRC(62108fff) SHA1(cfcf092e0a70779fc5912da0fbd154838df997da), ROM_BIOS(2) )
9768ROM_END
9769
9770ROM_START( megacd2j )
9771   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9772   ROM_SYSTEM_BIOS(0, "v200c", "v2.00C")
9773   ROMX_LOAD( "mpr-15398.bin", 0x000000,  0x020000, CRC(ba7bf31d) SHA1(762d20ebb85b980c17c53f928c002d747920a281), ROM_BIOS(1) )
9774ROM_END
9775
9776ROM_START( laseract )
9777   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9778   ROM_SYSTEM_BIOS(0, "v104", "v1.04")
9779   ROMX_LOAD( "laseractive_bios_1_04_u.bin", 0x000000,  0x020000, CRC(50cd3d23) SHA1(aa811861f8874775075bd3f53008c8aaf59b07db), ROM_BIOS(1) )
9780   ROM_SYSTEM_BIOS(1, "v102", "v1.02")
9781   ROMX_LOAD( "laseractive_bios_1_02_u.bin", 0x000000,  0x020000, CRC(3b10cf41) SHA1(8af162223bb12fc19b414f126022910372790103), ROM_BIOS(2) )
9782ROM_END
9783
9784ROM_START( laseractj )
9785   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9786   /* v1.02 */
9787   ROM_LOAD( "laseractive_bios_1_02_j.bin", 0x000000,  0x020000, CRC(00eedb3a) SHA1(26237b333db4a4c6770297fa5e655ea95840d5d9) )
9788ROM_END
9789
9790ROM_START( xeye )
9791   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9792   /* v2.00 (US), confirmed good with a chip dump */
9793   ROM_LOAD( "g304.bin", 0x000000,  0x020000, CRC(290f8e33) SHA1(651f14d5a5e0ecb974a60c0f43b1d2006323fb09) )
9794ROM_END
9795
9796ROM_START( wmega )
9797   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9798   /* v1.00 (Japan NTSC) Sega BIOS, chip-dumped */
9799   ROM_LOAD( "g301.bin", 0x000000,  0x020000, CRC(d21fe71d) SHA1(3fc9358072f74bd24e3e297ea11b2bf15a7af891) )
9800ROM_END
9801
9802ROM_START( wmegam2 )
9803   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9804   /* v2.00 */
9805   ROM_LOAD( "wondermega_m2_bios_2_00_j.bin", 0x000000,  0x020000, CRC(2b19972f) SHA1(b3f32e409bd5508c89ed8be33d41a58d791d0e5d) )
9806ROM_END
9807
9808ROM_START( cdx )
9809   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9810   /* v2.21X */
9811   ROM_LOAD( "segacdx_bios_2_21_u.bin", 0x000000,  0x020000, CRC(d48c44b5) SHA1(2b125c0545afa089b617f2558e686ea723bdc06e) )
9812ROM_END
9813
9814ROM_START( multmega )
9815   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
9816   /* v2.21X */
9817   ROM_LOAD( "opr-16140.bin", 0x000000,  0x020000, CRC(aacb851e) SHA1(75548ac9aaa6e81224499f9a1403b2b42433f5b7) )
9818   /* the below was marked "confirmed good dump", but 0x72 and 0x73 are 0xFF, indicating a bad dump made from memory */
9819   /* ROM_LOAD( "multimega_bios_2_21_e.bin", 0x000000,  0x020000, CRC(34d3cce1) SHA1(73fc9c014ad803e9e7d8076b3642a8a5224b3e51) ) */
9820ROM_END
9821
92969822/* some games use the 32x and SegaCD together to give better quality FMV */
92979823ROM_START( 32x_scd )
92989824   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
92999825
93009826   ROM_REGION16_BE( 0x400000, "gamecart", 0 ) /* 68000 Code */
9301   ROM_LOAD( "segacd_model2_bios_2_11_u.bin", 0x000000,  0x020000, CRC(2e49d72c) SHA1(328a3228c29fba244b9db2055adc1ec4f7a87e6b) )
9827   ROM_LOAD( "mpr-15764-t.bin", 0x000000,  0x020000, CRC(2e49d72c) SHA1(328a3228c29fba244b9db2055adc1ec4f7a87e6b) )
93029828
9829   ROM_REGION32_BE( 0x400000, "gamecart_sh2", 0 ) /* Copy for the SH2 */
9830   ROM_COPY( "gamecart", 0x0, 0x0, 0x400000)
9831
93039832   ROM_REGION16_BE( 0x400000, "32x_68k_bios", 0 ) /* 68000 Code */
93049833   ROM_LOAD( "32x_g_bios.bin", 0x000000,  0x000100, CRC(5c12eae8) SHA1(dbebd76a448447cb6e524ac3cb0fd19fc065d944) )
93059834
r10033r10034
93109839   ROM_LOAD( "32x_s_bios.bin", 0x000000,  0x000400, CRC(bfda1fe5) SHA1(4103668c1bbd66c5e24558e73d4f3f92061a109a) )
93119840ROM_END
93129841
9313ROM_START( g_virr )
9314   ROM_REGION( 0x400000, "maincpu", 0 ) /* 68000 Code */
9315   ROM_LOAD( "g_virr.bin", 0x000000, 0x200000, CRC(7e1a324a) SHA1(ff969ae53120cc4e7cb1a8a7e47458f2eb8a2165) )
9842
9843/****************************************** PICO emulation ****************************************/
9844
9845/*
9846   Pico Implementation By ElBarto (Emmanuel Vadot, elbarto@megadrive.org)
9847   Still missing the PCM custom chip
9848   Some game will not boot due to this
9849
9850 Pico Info from Notaz (http://notaz.gp2x.de/docs/picodoc.txt)
9851
9852 addr   acc   description
9853-------+-----+------------
9854800001  byte  Version register.
9855              ?vv? ????, where v can be:
9856                00 - hardware is for Japan
9857                01 - European version
9858                10 - USA version
9859                11 - ?
9860800003  byte  Buttons, 0 for pressed, 1 for released:
9861                bit 0: UP (white)
9862                bit 1: DOWN (orange)
9863                bit 2: LEFT (blue)
9864                bit 3: RIGHT (green)
9865                bit 4: red button
9866                bit 5: unused?
9867                bit 6: unused?
9868                bit 7: pen button
9869800005  byte  Most significant byte of pen x coordinate.
9870800007  byte  Least significant byte of pen x coordinate.
9871800009  byte  Most significant byte of pen y coordinate.
987280000b  byte  Least significant byte of pen y coordinate.
987380000d  byte  Page register. One bit means one uncovered page sensor.
9874                00 - storyware closed
9875                01, 03, 07, 0f, 1f, 3f - pages 1-6
9876                either page 5 or page 6 is often unused.
9877800010  word  PCM data register.
9878        r/w   read returns free bytes left in PCM FIFO buffer
9879              writes write data to buffer.
9880800012  word  PCM control register.
9881        r/w   For writes, it has following possible meanings:
9882              ?p?? ???? ???? ?rrr
9883                p - set to enable playback?
9884                r - sample rate / PCM data type?
9885                  0: 8kHz 4bit ADPCM?
9886                  1-7: 16kHz variants?
9887              For reads, if bit 15 is cleared, it means PCM is 'busy' or
9888              something like that, as games sometimes wait for it to become 1.
9889800019  byte  Games write 'S'
989080001b  byte  Games write 'E'
989180001d  byte  Games write 'G'
989280001f  byte  Games write 'A'
9893
9894*/
9895
9896#define PICO_PENX   1
9897#define PICO_PENY   2
9898
9899static UINT16 pico_read_penpos(running_machine *machine, int pen)
9900{
9901  UINT16 penpos = 0;
9902
9903  switch (pen)
9904    {
9905    case PICO_PENX:
9906      penpos = input_port_read_safe(machine, "PENX", 0);
9907      penpos |= 0x6;
9908      penpos = penpos * 320 / 255;
9909      penpos += 0x3d;
9910      break;
9911    case PICO_PENY:
9912      penpos = input_port_read_safe(machine, "PENY", 0);
9913      penpos |= 0x6;
9914      penpos = penpos * 251 / 255;
9915      penpos += 0x1fc;
9916      break;
9917    }
9918  return penpos;
9919}
9920
9921static READ16_HANDLER( pico_68k_io_read )
9922{
9923   UINT8 retdata;
9924   static UINT8 page_register = 0;
9925
9926
9927   retdata = 0;
9928
9929   switch (offset)
9930     {
9931     case 0:   /* Version register ?XX?????? where XX is 00 for japan, 01 for europe and 10 for USA*/
9932      retdata = (megadrive_region_export << 6) | (megadrive_region_pal << 5);
9933       break;
9934     case 1:
9935       retdata = input_port_read_safe(space->machine, "PAD", 0);
9936       break;
9937
9938       /*
9939           Still notes from notaz for the pen :
9940
9941           The pen can be used to 'draw' either on the drawing pad or on the storyware
9942           itself. Both storyware and drawing pad are mapped on single virtual plane, where
9943           coordinates range:
9944
9945           x: 0x03c - 0x17c
9946           y: 0x1fc - 0x2f7 (drawing pad)
9947              0x2f8 - 0x3f3 (storyware)
9948         */
9949     case 2:
9950       retdata = pico_read_penpos(space->machine, PICO_PENX) >> 8;
9951       break;
9952     case 3:
9953       retdata = pico_read_penpos(space->machine, PICO_PENX) & 0x00ff;
9954       break;
9955     case 4:
9956       retdata = pico_read_penpos(space->machine, PICO_PENY) >> 8;
9957       break;
9958     case 5:
9959       retdata = pico_read_penpos(space->machine, PICO_PENY) & 0x00ff;
9960       break;
9961     case 6:
9962       /* Page register :
9963           00 - storyware closed
9964           01, 03, 07, 0f, 1f, 3f - pages 1-6
9965           either page 5 or page 6 is often unused.
9966        */
9967       {
9968         UINT8 tmp;
9969
9970         tmp = input_port_read_safe(space->machine, "PAGE", 0);
9971         if (tmp == 2 && page_register != 0x3f)
9972      {
9973        page_register <<= 1;
9974        page_register |= 1;
9975      }
9976         if (tmp == 1 && page_register != 0x00)
9977      page_register >>= 1;
9978         retdata = page_register;
9979         break;
9980       }
9981     case 7:
9982       /* Returns free bytes left in the PCM FIFO buffer */
9983       retdata = 0x00;
9984       break;
9985     case 8:
9986       /*
9987           For reads, if bit 15 is cleared, it means PCM is 'busy' or
9988           something like that, as games sometimes wait for it to become 1.
9989        */
9990       retdata = 0x00;
9991     }
9992   return retdata | retdata <<8;
9993}
9994
9995static WRITE16_HANDLER( pico_68k_io_write )
9996{
9997   switch (offset)
9998     {
9999     }
10000}
10001
10002static ADDRESS_MAP_START( _pico_mem, ADDRESS_SPACE_PROGRAM, 16 )
10003   AM_RANGE(0x000000, 0x3fffff) AM_ROM
10004
10005   AM_RANGE(0x800000, 0x80001f) AM_READWRITE(pico_68k_io_read, pico_68k_io_write)
10006
10007   AM_RANGE(0xc00000, 0xc0001f) AM_READWRITE(megadriv_vdp_r, megadriv_vdp_w)
10008   AM_RANGE(0xe00000, 0xe0ffff) AM_RAM AM_MIRROR(0x1f0000) AM_BASE(&megadrive_ram)
10009ADDRESS_MAP_END
10010
10011
10012static INPUT_PORTS_START( pico )
10013   PORT_START("PAD")
10014   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1)
10015   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1)
10016   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1)
10017   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1)
10018   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("Red Button")
10019   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("Pen Button")
10020
10021   PORT_START("PAGE")
10022   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("Increment Page")
10023   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(1) PORT_NAME("Decrement Page")
10024
10025   PORT_START("PENX")
10026   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(30) PORT_KEYDELTA(10) PORT_MINMAX(0, 255) PORT_CATEGORY(5) PORT_PLAYER(1) PORT_NAME("PEN X")
10027
10028   PORT_START("PENY")
10029   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_SENSITIVITY(30) PORT_KEYDELTA(10) PORT_MINMAX(0,255 ) PORT_CATEGORY(5) PORT_PLAYER(1) PORT_NAME("PEN Y")
10030
10031   PORT_START("REGION")
10032   /* Region setting for Console */
10033   PORT_DIPNAME( 0x000f, 0x0000, DEF_STR( Region ) )
10034   PORT_DIPSETTING(      0x0000, "Use HazeMD Default Choice" )
10035   PORT_DIPSETTING(      0x0001, "US (NTSC, 60fps)" )
10036   PORT_DIPSETTING(      0x0002, "JAPAN (NTSC, 60fps)" )
10037   PORT_DIPSETTING(      0x0003, "EUROPE (PAL, 50fps)" )
10038INPUT_PORTS_END
10039
10040
10041static MACHINE_CONFIG_DERIVED( pico, megadriv )
10042
10043   MCFG_CPU_MODIFY("maincpu")
10044   MCFG_CPU_PROGRAM_MAP(_pico_mem)
10045
10046   MCFG_DEVICE_REMOVE("genesis_snd_z80")
10047
10048   MCFG_MACHINE_RESET( ms_megadriv )
10049
10050   MCFG_FRAGMENT_ADD( pico_cartslot )
10051MACHINE_CONFIG_END
10052
10053static MACHINE_CONFIG_DERIVED( picopal, megadpal )
10054
10055   MCFG_CPU_MODIFY("maincpu")
10056   MCFG_CPU_PROGRAM_MAP(_pico_mem)
10057
10058   MCFG_DEVICE_REMOVE("genesis_snd_z80")
10059
10060   MCFG_MACHINE_RESET( ms_megadriv )
10061
10062   MCFG_FRAGMENT_ADD( pico_cartslot )
10063MACHINE_CONFIG_END
10064
10065
10066
10067ROM_START( pico )
10068   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
10069   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
931610070ROM_END
9317ROM_START( g_virrj )
9318   ROM_REGION( 0x400000, "maincpu", 0 ) /* 68000 Code */
9319   ROM_LOAD( "g_virrj.bin", 0x000000, 0x200000, CRC(53a293b5) SHA1(0ad38a3ab1cc99edac72184f8ae420e13df5cac6) )
10071
10072ROM_START( picou )
10073   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
10074   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
932010075ROM_END
9321ROM_START( g_virre )
9322   ROM_REGION( 0x400000, "maincpu", 0 ) /* 68000 Code */
9323   ROM_LOAD( "g_virre.bin", 0x000000, 0x200000, CRC(9624d4ef) SHA1(2c3812f8a010571e51269a33a989598787d27c2d) )
10076
10077ROM_START( picoj )
10078   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
10079   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
932410080ROM_END
9325ROM_START( g_virrea )
9326   ROM_REGION( 0x400000, "maincpu", 0 ) /* 68000 Code */
9327   ROM_LOAD( "g_virrea.bin", 0x000000, 0x200000, CRC(5a943df9) SHA1(2c08ea556c79d48e88ff5202944c161ae1b41c63) )
9328ROM_END
932910081
9330#ifndef MESS
9331GAME( 1994, 32x_bios,    0,        genesis_32x,        megadriv,    _32x,    ROT0,   "Sega", "32X Bios", GAME_NOT_WORKING )
9332GAME( 1994, segacd,      0,        genesis_scd,        megadriv,    megadriv,ROT0,   "Sega", "Sega-CD Model 2 BIOS V2.11 (U)", GAME_NOT_WORKING )
9333GAME( 1994, 32x_scd,     0,        genesis_32x_scd,    megadriv,    _32x,    ROT0,   "Sega", "Sega-CD Model 2 BIOS V2.11 (U) (with 32X)", GAME_NOT_WORKING )
9334GAME( 1994, g_virr,      0,        megdsvp,            megadriv,   megadriv, ROT0,   "Sega", "Virtua Racing (U) [!]", 0 )
9335GAME( 1994, g_virrj,     g_virr,   megdsvp,            megadriv,   megadrij, ROT0,   "Sega", "Virtua Racing (J) [!]", 0 )
9336GAME( 1994, g_virre,     g_virr,   megdsvppal,         megadriv,   megadrie, ROT0,   "Sega", "Virtua Racing (E) [!]", 0 )
9337GAME( 1994, g_virrea,    g_virr,   megdsvppal,         megadriv,   megadrie, ROT0,   "Sega", "Virtua Racing (E) [a1]", 0 )
9338#endif
10082/***************************************************************************
933910083
9340#endif
10084  Game driver(s)
10085
10086***************************************************************************/
10087
10088/*    YEAR  NAME        PARENT     COMPAT  MACHINE          INPUT   INIT       COMPANY   FULLNAME */
10089CONS( 1989, genesis,    0,         0,      ms_megadriv,     md,     genesis,   "Sega",   "Genesis (USA, NTSC)", 0)
10090CONS( 1993, gensvp,     genesis,   0,      ms_megdsvp,      md_sel, gensvp,    "Sega",   "Genesis (USA, NTSC, w/SVP)", 0)
10091CONS( 1990, megadriv,   genesis,   0,      ms_megadpal,     md,     md_eur,    "Sega",   "Mega Drive (Europe, PAL)", 0)
10092CONS( 1988, megadrij,   genesis,   0,      ms_megadriv,     md,     md_jpn,    "Sega",   "Mega Drive (Japan, NTSC)", 0)
10093CONS( 1994, pico,       0,         0,      picopal,         pico,   md_eur,    "Sega",   "Pico (Europe, PAL)", 0)
10094CONS( 1994, picou,      pico,      0,      pico,            pico,   genesis,   "Sega",   "Pico (USA, NTSC)", 0)
10095CONS( 1993, picoj,      pico,      0,      pico,            pico,   md_jpn,    "Sega",   "Pico (Japan, NTSC)", 0)
10096
10097/* Not Working */
10098CONS( 1994, 32x,        0,         0,      ms_32x,          md_sel, mess_32x,  "Sega",   "32X", GAME_NOT_WORKING )
10099CONS( 1992, segacd,     0,         0,      genesis_scd,     md,     genesis,   "Sega",   "Sega CD (USA, NTSC)", GAME_NOT_WORKING )
10100CONS( 1993, megacd,     segacd,    0,      genesis_scd,     md,     md_eur,    "Sega",   "Mega-CD (Europe, PAL)", GAME_NOT_WORKING )
10101CONS( 1991, megacdj,    segacd,    0,      genesis_scd,     md,     md_jpn,    "Sega",   "Mega-CD (Japan, NTSC)", GAME_NOT_WORKING )
10102CONS( 1993, segacd2,    0,         0,      genesis_scd,     md,     genesis,   "Sega",   "Sega CD 2 (USA, NTSC)", GAME_NOT_WORKING )
10103CONS( 1993, megacd2,    segacd2,   0,      genesis_scd,     md,     md_eur,    "Sega",   "Mega-CD 2 (Europe, PAL)", GAME_NOT_WORKING )
10104CONS( 1993, megacd2j,   segacd2,   0,      genesis_scd,     md,     md_jpn,    "Sega",   "Mega-CD 2 (Japan, NTSC)", GAME_NOT_WORKING )
10105CONS( 1993, laseract,   0,         0,      genesis_scd,     md,     genesis,   "Pioneer","LaserActive (USA, NTSC)", GAME_NOT_WORKING )
10106CONS( 1993, laseractj,  laseract,  0,      genesis_scd,     md,     md_jpn,    "Pioneer","LaserActive (Japan, NTSC)", GAME_NOT_WORKING )
10107CONS( 1993, xeye,       0,         0,      genesis_scd,     md,     genesis,   "JVC",    "X'eye (USA, NTSC)", GAME_NOT_WORKING )
10108CONS( 1992, wmega,      xeye,      0,      genesis_scd,     md,     md_jpn,    "Sega",   "Wondermega (Japan, NTSC)", GAME_NOT_WORKING )
10109CONS( 1993, wmegam2,    xeye,      0,      genesis_scd,     md,     md_jpn,    "Victor", "Wondermega M2 (Japan, NTSC)", GAME_NOT_WORKING )
10110CONS( 1994, cdx,        0,         0,      genesis_scd,     md,     genesis,   "Sega",   "CDX (USA, NTSC)", GAME_NOT_WORKING )
10111CONS( 1994, multmega,   cdx,       0,      genesis_scd,     md,     md_eur,    "Sega",   "Multi-Mega (Europe, PAL)", GAME_NOT_WORKING )
10112CONS( 1994, 32x_scd,    segacd,    0,      genesis_32x_scd, md_sel, mess_32x,  "Sega",   "Sega CD (USA, NTSC, w/32X)", GAME_NOT_WORKING )
src/mess/machine/gen_pcb.h
r10033r10034
1#ifndef __GENESIS_PCB_H
2# define __GENESIS_PCB_H
3
4/* PCB */
5enum
6  {
7    STD_ROM = 0,
8
9    /* Sega PCB */
10    SEGA_5878, SEGA_6584A, SEGA_5921, SEGA_6278A, SEGA_6658A,
11
12    /* Codemasters PCB (J-Carts and SEPROM) */
13    CM_JCART, CM_JCART_SEPROM
14  };
15
16int md_get_pcb_id(const char *pcb);
17
18#endif
19
src/mess/machine/genesis.c
r10033r10034
1/***************************************************************************
2
3  genesis.c
4
5  Machine file to handle emulation of the Sega Mega Drive & Genesis.
6
7    i2c games mapping table:
8
9    game name                         |   SDA_IN   |  SDA_OUT   |     SCL    |  SIZE_MASK     | PAGE_MASK |
10    ----------------------------------|------------|------------|------------|----------------|-----------|
11    NBA Jam                           | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x00ff (24C02) |   0x03    | xx
12    NBA Jam TE                        | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    | xx
13    NBA Jam TE (32x)                  | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    |
14    NFL Quarterback Club              | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    | xx
15    NFL Quarterback Club 96           | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x07ff (24C16) |   0x07    | xx
16    College Slam                      | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x1fff (24C64) |   0x07    | xx
17    Frank Thomas Big Hurt Baseball    | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x1fff (24C64) |   0x07    | xx
18    NHLPA Hockey 93                   | 0x200001-7 | 0x200001-7 | 0x200001-6 | 0x007f (24C01) |   0x03    | xx
19    Rings of Power                    | 0x200001-7 | 0x200001-7 | 0x200001-6 | 0x007f (24C01) |   0x03    | xx
20    Evander Holyfield's Boxing        | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
21    Greatest Heavyweights of the Ring | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
22    Wonder Boy V                      | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
23    Sports Talk Baseball              | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx
24    Megaman - the Wily Wars           | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    | xx **
25    Micro Machines 2                  | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x03ff (24C08) |   0x0f    |
26    Micro Machines Military           | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x03ff (24C08) |   0x0f    |
27    Micro Machines 96                 | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x07ff (24C16) |   0x0f    |
28    Brian Lara Cricket 96             | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x1fff (24C64) |   0x??*   |
29    ----------------------------------|------------|------------|------------|----------------|-----------|
30
31    * Notes: check these
32    ** original Rockman Mega World (J) set uses normal backup RAM
33
34    2008-09: Moved here cart code and custom mapper handlers. Hopefully,
35        it will make painless the future merging with HazeMD
36    2008-10: Fixed SRAM, matching as much as possible HazeMD. Some game is
37        not detected, however. E.g. Sonic 3. Also some games seem false
38        positives (e.g. Wonderboy 5: according to its headers it should
39        have 1byte of SRAM). Better detection routines would be welcome.
40    2009-06: Changed SRAM code, fixing more games, including Sonic 3. The
41        false positives seem only some of the games using EEPROM. Todo
42        list in mess/drivers/genesis.c includes EEPROM emulation.
43
44***************************************************************************/
45
46
47#include "emu.h"
48#include "imageutl.h"
49#include "cpu/m68000/m68000.h"
50
51#include "imagedev/cartslot.h"
52#include "../includes/genesis.h"
53#include "gen_pcb.h"
54
55
56#define MAX_MD_CART_SIZE 0x500000
57
58/* where a fresh copy of rom is stashed for reset and banking setup */
59#define VIRGIN_COPY_GEN 0xd00000
60
61enum t_cart_type
62{
63   STANDARD = 0,
64   SSF2,               /* Super Street Fighter 2 */
65   LIONK3,               /* Lion King 3 */
66   SKINGKONG,            /* Super King Kong 99 */
67   SDK99,               /* Super Donkey Kong 99 */
68   REDCLIFF,            /* Romance of the Three Kingdoms - Battle of Red Cliffs, already decoded from .mdx format */
69   REDCL_EN,            /* The encoded version... */
70   RADICA,               /* Radica TV games.. these probably should be a seperate driver since they are a seperate 'console' */
71   KOF99,               /* King of Fighters '99 */
72   SOULBLAD,            /* Soul Blade */
73   MJLOVER,            /* Mahjong Lover */
74   SQUIRRELK,            /* Squirrel King */
75   SMOUSE,               /* Smart Mouse */
76   SMB,               /* Super Mario Bros. */
77   SMB2,               /* Super Mario Bros. 2 */
78   KAIJU,               /* Pokemon Stadium */
79   CHINFIGHT3,            /* Chinese Fighters 3 */
80   LIONK2,               /* Lion King 2 */
81   BUGSLIFE,            /* A Bug's Life */
82   ELFWOR,               /* Elf Wor */
83   ROCKMANX3,            /* Rockman X3 */
84   SBUBBOB,            /* Super Bubble Bobble */
85   KOF98,               /* King of Fighters '98 */
86   REALTEC,            /* Whac a Critter/Mallet legend, Defend the Earth, Funnyworld/Ballonboy */
87   SUP19IN1,            /* Super 19 in 1 */
88   SUP15IN1,            /* Super 15 in 1 */
89   NBA_JAM,            /* NBA Jam */
90   NBA_JAM_TE,            /* NBA Jam TE / NFL Quarterback Club */
91   NFL_QB_96,            /* NFL Quarterback Club '96 */
92   C_SLAM,               /* College Slam / Frank Thomas Big Hurt Baseball */
93   EA_NHLPA,            /* NHLPA Hockey 93 / Rings of Power */
94   WBOY_V,               /* Wonder Boy V / Evander Holyfield's Boxing / Greatest Heavyweights of the Ring / Sports Talk Baseball / Megaman */
95   CODE_MASTERS         /* Micro Machines 2 / Military / 96 / Brian Lara Cricket */
96};
97
98
99static enum t_cart_type cart_type;
100
101
102static UINT16 squirrel_king_extra;
103static UINT16 lion2_prot1_data, lion2_prot2_data;
104static UINT16 realtec_bank_addr, realtec_bank_size, realtec_old_bank_addr;
105static UINT16 g_l3alt_pdat;
106static UINT16 g_l3alt_pcmd;
107
108static int genesis_last_loaded_image_length;
109
110static UINT16 *genesis_sram, *megadriv_backupram;
111static int genesis_sram_start, genesis_sram_end;
112static int genesis_sram_active, genesis_sram_readonly;
113static int sram_handlers_installed;
114
115static int has_serial_eeprom;
116
117
118/*************************************
119 *
120 *  Handlers for custom mappers
121 *
122 *************************************/
123
124static WRITE16_HANDLER( genesis_ssf2_bank_w )
125{
126   static int lastoffset = -1,lastdata = -1;
127   UINT8 *ROM = space->machine->region("maincpu")->base();
128
129   if ((lastoffset != offset) || (lastdata != data))
130   {
131      lastoffset = offset; lastdata = data;
132      switch (offset << 1)
133      {
134         case 0x00: /* write protect register */ // this is not a write protect, but seems to do nothing useful but reset bank0 after the checksum test (red screen otherwise)
135            if (data == 2)
136            {
137               memcpy(ROM + 0x000000, ROM + 0x400000 + (((data & 0xf) - 2) * 0x080000), 0x080000);
138            }
139            break;
140         case 0x02: /* 0x080000 - 0x0FFFFF */
141            memcpy(ROM + 0x080000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
142            break;
143         case 0x04: /* 0x100000 - 0x17FFFF */
144            memcpy(ROM + 0x100000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
145            break;
146         case 0x06: /* 0x180000 - 0x1FFFFF */
147            memcpy(ROM + 0x180000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
148            break;
149         case 0x08: /* 0x200000 - 0x27FFFF */
150            memcpy(ROM + 0x200000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
151            break;
152         case 0x0a: /* 0x280000 - 0x2FFFFF */
153            memcpy(ROM + 0x280000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
154            break;
155         case 0x0c: /* 0x300000 - 0x37FFFF */
156            memcpy(ROM + 0x300000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
157            break;
158         case 0x0e: /* 0x380000 - 0x3FFFFF */
159            memcpy(ROM + 0x380000, ROM + 0x400000 + ((data & 0xf) * 0x080000), 0x080000);
160            break;
161      }
162   }
163}
164
165#ifdef UNUSED_FUNCTION
166static WRITE16_HANDLER( g_l3alt_pdat_w )
167{
168   g_l3alt_pdat = data;
169}
170
171static WRITE16_HANDLER( g_l3alt_pcmd_w )
172{
173   g_l3alt_pcmd = data;
174}
175#endif
176
177static READ16_HANDLER( g_l3alt_prot_r )
178{
179   int retdata = 0;
180
181   offset &= 0x07;
182
183   switch (offset)
184   {
185
186      case 2:
187
188         switch (g_l3alt_pcmd)
189         {
190            case 1:
191               retdata = g_l3alt_pdat >> 1;
192               break;
193
194            case 2:
195               retdata = g_l3alt_pdat >> 4;
196               retdata |= (g_l3alt_pdat & 0x0f) << 4;
197               break;
198
199            default:
200               /* printf("unk prot case %d\n", g_l3alt_pcmd); */
201               retdata =  (((g_l3alt_pdat >> 7) & 1) << 0);
202               retdata |= (((g_l3alt_pdat >> 6) & 1) << 1);
203               retdata |= (((g_l3alt_pdat >> 5) & 1) << 2);
204               retdata |= (((g_l3alt_pdat >> 4) & 1) << 3);
205               retdata |= (((g_l3alt_pdat >> 3) & 1) << 4);
206               retdata |= (((g_l3alt_pdat >> 2) & 1) << 5);
207               retdata |= (((g_l3alt_pdat >> 1) & 1) << 6);
208               retdata |= (((g_l3alt_pdat >> 0) & 1) << 7);
209               break;
210         }
211         break;
212
213      default:
214
215         printf("protection read, unknown offset\n");
216         break;
217   }
218
219/*  printf("%06x: g_l3alt_pdat_w %04x g_l3alt_pcmd_w %04x return %04x\n", activecpu_get_pc(), g_l3alt_pdat, g_l3alt_pcmd, retdata); */
220
221   return retdata;
222}
223
224static WRITE16_HANDLER( g_l3alt_prot_w )
225{
226   offset &= 0x7;
227
228   switch (offset)
229   {
230      case 0x0:
231         g_l3alt_pdat = data;
232         break;
233      case 0x1:
234         g_l3alt_pcmd = data;
235         break;
236      default:
237         printf("protection write, unknown offst\n");
238         break;
239   }
240}
241
242static WRITE16_HANDLER( g_l3alt_bank_w )
243{
244   offset &= 0x7;
245
246   switch (offset)
247   {
248      case 0:
249      {
250      UINT8 *ROM = space->machine->region("maincpu")->base();
251      /* printf("%06x data %04x\n",activecpu_get_pc(), data); */
252      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN + (data & 0xffff) * 0x8000], 0x8000);
253      }
254      break;
255
256      default:
257      printf("unk bank w\n");
258      break;
259
260   }
261
262}
263
264
265static WRITE16_HANDLER( realtec_402000_w )
266{
267   realtec_bank_addr = 0;
268   realtec_bank_size = (data >> 8) & 0x1f;
269}
270
271static WRITE16_HANDLER( realtec_400000_w )
272{
273   int bankdata = (data >> 9) & 0x7;
274
275   UINT8 *ROM = space->machine->region("maincpu")->base();
276
277   realtec_old_bank_addr = realtec_bank_addr;
278   realtec_bank_addr = (realtec_bank_addr & 0x7) | bankdata << 3;
279
280   memcpy(ROM, ROM + (realtec_bank_addr * 0x20000) + 0x400000, realtec_bank_size * 0x20000);
281   memcpy(ROM + realtec_bank_size * 0x20000, ROM + (realtec_bank_addr * 0x20000) + 0x400000, realtec_bank_size * 0x20000);
282}
283
284static WRITE16_HANDLER( realtec_404000_w )
285{
286   int bankdata = (data >> 8) & 0x3;
287   UINT8 *ROM = space->machine->region("maincpu")->base();
288
289   realtec_old_bank_addr = realtec_bank_addr;
290   realtec_bank_addr = (realtec_bank_addr & 0xf8) | bankdata;
291
292   if (realtec_old_bank_addr != realtec_bank_addr)
293   {
294      memcpy(ROM, ROM + (realtec_bank_addr * 0x20000)+ 0x400000, realtec_bank_size * 0x20000);
295      memcpy(ROM + realtec_bank_size * 0x20000, ROM + (realtec_bank_addr * 0x20000) + 0x400000, realtec_bank_size * 0x20000);
296   }
297}
298
299static WRITE16_HANDLER( g_chifi3_bank_w )
300{
301   UINT8 *ROM = space->machine->region("maincpu")->base();
302
303   if (data == 0xf100) // *hit player
304   {
305      int x;
306      for (x = 0; x < 0x100000; x += 0x10000)
307      {
308         memcpy(ROM + x, ROM + 0x410000, 0x10000);
309      }
310   }
311   else if (data == 0xd700) // title screen..
312   {
313      int x;
314      for (x = 0; x < 0x100000; x += 0x10000)
315      {
316         memcpy(ROM + x, ROM + 0x470000, 0x10000);
317      }
318   }
319   else if (data == 0xd300) // character hits floor
320   {
321      int x;
322      for (x = 0; x < 0x100000; x += 0x10000)
323      {
324         memcpy(ROM + x, ROM + 0x430000, 0x10000);
325      }
326   }
327   else if (data == 0x0000)
328   {
329      int x;
330      for (x = 0; x < 0x100000; x += 0x10000)
331      {
332         memcpy(ROM + x, ROM + 0x400000 + x, 0x10000);
333      }
334   }
335   else
336   {
337      logerror("%06x chifi3, bankw? %04x %04x\n", cpu_get_pc(space->cpu), offset, data);
338   }
339
340}
341
342static READ16_HANDLER( g_chifi3_prot_r )
343{
344   UINT32 retdat;
345
346   /* not 100% correct, there may be some relationship between the reads here
347    and the writes made at the start of the game.. */
348
349   /*
350    04dc10 chifi3, prot_r? 2800
351    04cefa chifi3, prot_r? 65262
352    */
353
354   if (cpu_get_pc(space->cpu) == 0x01782) // makes 'VS' screen appear
355   {
356      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
357      retdat <<= 8;
358      return retdat;
359   }
360   else if (cpu_get_pc(space->cpu) == 0x1c24) // background gfx etc.
361   {
362      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
363      retdat <<= 8;
364      return retdat;
365   }
366   else if (cpu_get_pc(space->cpu) == 0x10c4a) // unknown
367   {
368      return space->machine->rand();
369   }
370   else if (cpu_get_pc(space->cpu) == 0x10c50) // unknown
371   {
372      return space->machine->rand();
373   }
374   else if (cpu_get_pc(space->cpu) == 0x10c52) // relates to the game speed..
375   {
376      retdat = cpu_get_reg(space->cpu, M68K_D4) & 0xff;
377      retdat <<= 8;
378      return retdat;
379   }
380   else if (cpu_get_pc(space->cpu) == 0x061ae)
381   {
382      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
383      retdat <<= 8;
384      return retdat;
385   }
386   else if (cpu_get_pc(space->cpu) == 0x061b0)
387   {
388      retdat = cpu_get_reg(space->cpu, M68K_D3) & 0xff;
389      retdat <<= 8;
390      return retdat;
391   }
392   else
393   {
394      logerror("%06x chifi3, prot_r? %04x\n", cpu_get_pc(space->cpu), offset);
395   }
396
397   return 0;
398}
399
400static WRITE16_HANDLER( s19in1_bank )
401{
402   UINT8 *ROM = space->machine->region("maincpu")->base();
403   memcpy(ROM + 0x000000, ROM + 0x400000 + ((offset << 1) * 0x10000), 0x80000);
404}
405
406// Kaiju? (Pokemon Stadium) handler from HazeMD
407static WRITE16_HANDLER( g_kaiju_bank_w )
408{
409   UINT8 *ROM = space->machine->region("maincpu")->base();
410   memcpy(ROM + 0x000000, ROM + 0x400000 + (data & 0x7f) * 0x8000, 0x8000);
411}
412
413// Soulblade handler from HazeMD
414static READ16_HANDLER( soulb_0x400006_r )
415{
416   return 0xf000;
417}
418
419static READ16_HANDLER( soulb_0x400002_r )
420{
421   return 0x9800;
422}
423
424static READ16_HANDLER( soulb_0x400004_r )
425{
426   return 0xc900;
427}
428
429// Mahjong Lover handler from HazeMD
430static READ16_HANDLER( mjlovr_prot_1_r )
431{
432   return 0x9000;
433}
434
435static READ16_HANDLER( mjlovr_prot_2_r )
436{
437   return 0xd300;
438}
439
440// Super Mario Bros handler from HazeMD
441static READ16_HANDLER( smbro_prot_r )
442{
443   return 0xc;
444}
445
446
447// Smart Mouse handler from HazeMD
448static READ16_HANDLER( smous_prot_r )
449{
450   switch (offset)
451   {
452      case 0: return 0x5500;
453      case 1: return 0x0f00;
454      case 2: return 0xaa00;
455      case 3: return 0xf000;
456   }
457
458   return -1;
459}
460
461// Super Bubble Bobble MD handler from HazeMD
462static READ16_HANDLER( sbub_extra1_r )
463{
464   return 0x5500;
465}
466
467static READ16_HANDLER( sbub_extra2_r )
468{
469   return 0x0f00;
470}
471
472// KOF99 handler from HazeMD
473static READ16_HANDLER( kof99_0xA13002_r )
474{
475   // write 02 to a13002.. shift right 1?
476   return 0x01;
477}
478
479static READ16_HANDLER( kof99_00A1303E_r )
480{
481   // write 3e to a1303e.. shift right 1?
482   return 0x1f;
483}
484
485static READ16_HANDLER( kof99_0xA13000_r )
486{
487   // no write, startup check, chinese message if != 0
488   return 0x0;
489}
490
491// Radica handler from HazeMD
492
493static READ16_HANDLER( radica_bank_select )
494{
495   int bank = offset&0x3f;
496   UINT8 *ROM = space->machine->region("maincpu")->base();
497   memcpy(ROM, ROM +  (bank * 0x10000) + 0x400000, 0x400000);
498   return 0;
499}
500
501// ROTK Red Cliff handler from HazeMD
502
503static READ16_HANDLER( redclif_prot_r )
504{
505   return -0x56 << 8;
506}
507
508static READ16_HANDLER( redclif_prot2_r )
509{
510   return 0x55 << 8;
511}
512
513// Squirrel King handler from HazeMD, this does not give screen garbage like HazeMD compile If you reset it twice
514static READ16_HANDLER( squirrel_king_extra_r )
515{
516   return squirrel_king_extra;
517
518}
519static WRITE16_HANDLER( squirrel_king_extra_w )
520{
521   squirrel_king_extra = data;
522}
523
524// Lion King 2 handler from HazeMD
525static READ16_HANDLER( lion2_prot1_r )
526{
527   return lion2_prot1_data;
528}
529
530static READ16_HANDLER( lion2_prot2_r )
531{
532   return lion2_prot2_data;
533}
534
535static WRITE16_HANDLER ( lion2_prot1_w )
536{
537   lion2_prot1_data = data;
538}
539
540static WRITE16_HANDLER ( lion2_prot2_w )
541{
542   lion2_prot2_data = data;
543}
544
545// Rockman X3 handler from HazeMD
546static READ16_HANDLER( rx3_extra_r )
547{
548   return 0xc;
549}
550
551// King of Fighters 98 handler from HazeMD
552static READ16_HANDLER( g_kof98_aa_r )
553{
554   return 0xaa00;
555}
556
557static READ16_HANDLER( g_kof98_0a_r )
558{
559   return 0x0a00;
560}
561
562static READ16_HANDLER( g_kof98_00_r )
563{
564   return 0x0000;
565}
566
567// Super Mario Bros 2 handler from HazeMD
568static READ16_HANDLER( smb2_extra_r )
569{
570   return 0xa;
571}
572
573// Bug's Life handler from HazeMD
574static READ16_HANDLER( bugl_extra_r )
575{
576   return 0x28;
577}
578
579// Elf Wor handler from HazeMD (DFJustin says the title is 'Linghuan Daoshi Super Magician')
580static READ16_HANDLER( elfwor_0x400000_r )
581{
582   return 0x5500;
583}
584
585static READ16_HANDLER( elfwor_0x400002_r )
586{
587   return 0x0f00;
588}
589
590static READ16_HANDLER( elfwor_0x400004_r )
591{
592   return 0xc900;
593}
594
595static READ16_HANDLER( elfwor_0x400006_r )
596{
597   return 0x1800;
598}
599
600static WRITE16_HANDLER( genesis_TMSS_bank_w )
601{
602   /* this probably should do more, like make Genesis V2 'die' if the SEGA string is not written promptly */
603}
604
605
606
607/*************************************
608 *
609 *  Handlers for SRAM
610 *
611 *************************************/
612
613static void alloc_sram(running_machine *machine)
614{
615   genesis_sram = auto_alloc_array(machine, UINT16, (genesis_sram_end - genesis_sram_start + 1) / sizeof(UINT16));
616   device_image_interface *image = dynamic_cast<device_image_interface *>(machine->device("cart"));
617   image->battery_load(genesis_sram, genesis_sram_end - genesis_sram_start + 1, 0xff); // Dino Dini's Soccer needs backup RAM to be 1fill
618   memcpy(megadriv_backupram, genesis_sram, genesis_sram_end - genesis_sram_start + 1);
619}
620
621static READ16_HANDLER( genesis_sram_read )
622{
623   UINT8 *ROM;
624   int rom_offset;
625
626   if (genesis_sram_active)
627   {
628      if (genesis_sram == NULL)
629         alloc_sram(space->machine);
630      return genesis_sram[offset];
631   }
632   else
633   {
634      ROM = space->machine->region("maincpu")->base();
635      rom_offset = genesis_sram_start + (offset << 1);
636
637      return (UINT16) ROM[rom_offset] | (ROM[rom_offset + 1] << 8);
638   }
639}
640
641static WRITE16_HANDLER( genesis_sram_write )
642{
643   if (genesis_sram_active)
644   {
645      if (genesis_sram == NULL)
646         alloc_sram(space->machine);
647      if (!genesis_sram_readonly)
648         genesis_sram[offset] = data;
649   }
650}
651
652static void install_sram_rw_handlers(running_machine *machine)
653{
654   memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), genesis_sram_start & 0x3fffff, genesis_sram_end & 0x3fffff, 0, 0, genesis_sram_read);
655   memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), genesis_sram_start & 0x3fffff, genesis_sram_end & 0x3fffff, 0, 0, genesis_sram_write);
656   sram_handlers_installed = 1;
657}
658
659static WRITE16_HANDLER( genesis_sram_toggle )
660{
661
662   /* unsure if this is actually supposed to toggle or just switch on?
663    * Yet to encounter game that utilizes
664    */
665   genesis_sram_active = (data & 1) ? 1 : 0;
666   genesis_sram_readonly = (data & 2) ? 1 : 0;
667
668   if (genesis_sram_active && !sram_handlers_installed)
669      install_sram_rw_handlers (space->machine);
670}
671
672/*
673I2c bare minimum handlings (TODO: supports proper device interfacing and parameter change!)
674*/
675
676static UINT8 i2c_mem,i2c_clk;
677
678static READ16_HANDLER( nba_jam_eeprom_r )
679{
680//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
681
682   return i2c_mem & 1;
683}
684
685static WRITE16_HANDLER( nba_jam_eeprom_w )
686{
687   i2c_clk = (data & 0x0002) >> 1;
688   i2c_mem = (data & 0x0001);
689
690//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
691//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
692}
693
694static READ16_HANDLER( nba_jam_te_eeprom_r )
695{
696//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
697
698   return (i2c_mem & 1);
699}
700
701static WRITE16_HANDLER( nba_jam_te_eeprom_w )
702{
703   i2c_clk = ((data & 0x0100) >> 8);
704   i2c_mem = data & 0x0001;
705
706//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
707//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
708}
709
710static READ16_HANDLER( ea_nhlpa_eeprom_r )
711{
712//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
713
714   return (i2c_mem & 1) << 7;
715}
716
717static WRITE16_HANDLER( ea_nhlpa_eeprom_w )
718{
719   i2c_clk = ((data & 0x0040) >> 6);
720   i2c_mem = ((data & 0x0080) >> 7);
721
722//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
723//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
724}
725
726/* TODO: identical as NBA Jam, used as kludge */
727static READ16_HANDLER( wboy_v_eeprom_r )
728{
729//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
730
731   return ~i2c_mem & 1;
732}
733
734static WRITE16_HANDLER( wboy_v_eeprom_w )
735{
736   i2c_clk = (data & 0x0002) >> 1;
737   i2c_mem = (data & 0x0001);
738
739//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
740//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
741}
742
743static READ16_HANDLER( codemasters_eeprom_r )
744{
745//  i2c_mem = (i2cmem_sda_read(space->machine->device("i2cmem")) & 1);
746
747   return (i2c_mem & 1) << 7;
748}
749
750static WRITE16_HANDLER( codemasters_eeprom_w )
751{
752   i2c_clk = (data & 0x0200) >> 9;
753   i2c_mem = (data & 0x0100) >> 8;
754
755//  i2cmem_sda_write(space->machine->device("i2cmem"), i2c_clk);
756//  i2cmem_scl_write(space->machine->device("i2cmem"), i2c_mem);
757}
758
759/*************************************
760 *
761 *  Machine driver reset
762 *
763 *************************************/
764
765
766static void setup_megadriv_custom_mappers(running_machine *machine)
767{
768   static int relocate = VIRGIN_COPY_GEN;
769   unsigned char *ROM;
770   UINT32 mirroraddr;
771
772   /* SRAM data */
773   sram_handlers_installed = 0;
774   genesis_sram_readonly = 0;
775   genesis_sram_active = 0;
776
777   ROM = machine->region("maincpu")->base();
778
779   if (cart_type == SSF2)
780   {
781      memcpy(&ROM[0x800000], &ROM[VIRGIN_COPY_GEN + 0x400000], 0x100000);
782      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x400000);
783      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x400000);
784
785      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa130f0, 0xa130ff, 0, 0, genesis_ssf2_bank_w);
786   }
787
788   if (cart_type == LIONK3 || cart_type == SKINGKONG)
789   {
790      g_l3alt_pdat = g_l3alt_pcmd = 0;
791      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x200000); /* default rom */
792      memcpy(&ROM[0x200000], &ROM[VIRGIN_COPY_GEN], 0x200000); /* default rom */
793
794      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x600000, 0x6fffff, 0, 0, g_l3alt_prot_r, g_l3alt_prot_w);
795      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x700000, 0x7fffff, 0, 0, g_l3alt_bank_w);
796   }
797
798   if (cart_type == SDK99)
799   {
800      g_l3alt_pdat = g_l3alt_pcmd = 0;
801
802      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x300000); /* default rom */
803      memcpy(&ROM[0x300000], &ROM[VIRGIN_COPY_GEN], 0x100000); /* default rom */
804
805      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x600000, 0x6fffff, 0, 0, g_l3alt_prot_r, g_l3alt_prot_w);
806      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x700000, 0x7fffff, 0, 0, g_l3alt_bank_w);
807   }
808
809   if (cart_type == REDCLIFF)
810   {
811      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, redclif_prot2_r);
812      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, redclif_prot_r);
813   }
814
815   if (cart_type == REDCL_EN)
816   {
817      int x;
818
819      for (x = VIRGIN_COPY_GEN; x < VIRGIN_COPY_GEN + 0x200005; x++)
820      {
821         ROM[x] ^= 0x40;
822      }
823
824      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN + 4], 0x200000); /* default rom */
825
826      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, redclif_prot2_r);
827      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, redclif_prot_r);
828   }
829
830   if (cart_type == RADICA)
831   {
832      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x400000); // keep a copy for later banking.. making use of huge ROM_REGION allocated to genesis driver
833      memcpy(&ROM[0x800000], &ROM[VIRGIN_COPY_GEN], 0x400000); // wraparound banking (from hazemd code)
834      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x400000);
835      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa1307f, 0, 0, radica_bank_select);
836   }
837
838   if (cart_type == KOF99)
839   {
840      //memcpy(&ROM[0x000000],&ROM[VIRGIN_COPY_GEN],0x300000);
841      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, kof99_0xA13000_r);
842      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13002, 0xa13003, 0, 0, kof99_0xA13002_r);
843      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa1303e, 0xa1303f, 0, 0, kof99_00A1303E_r);
844   }
845
846   if (cart_type == SOULBLAD)
847   {
848      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, soulb_0x400002_r);
849      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, soulb_0x400004_r);
850      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400006, 0x400007, 0, 0, soulb_0x400006_r);
851   }
852
853   if (cart_type == MJLOVER)
854   {
855      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, mjlovr_prot_1_r);
856      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x401000, 0x401001, 0, 0, mjlovr_prot_2_r);
857   }
858
859   if (cart_type == SQUIRRELK)
860   {
861      squirrel_king_extra = 0;
862      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400007, 0, 0, squirrel_king_extra_r);
863      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400007, 0, 0, squirrel_king_extra_w);
864   }
865
866   if (cart_type == SMOUSE)
867   {
868      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400007, 0, 0, smous_prot_r);
869   }
870
871   if (cart_type == SMB)
872   {
873      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, smbro_prot_r);
874   }
875
876   if (cart_type == SMB2)
877   {
878      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, smb2_extra_r);
879   }
880
881   if (cart_type == KAIJU)
882   {
883      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x200000);
884      memcpy(&ROM[0x600000], &ROM[VIRGIN_COPY_GEN], 0x200000);
885      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x200000);
886
887      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x700000, 0x7fffff, 0, 0, g_kaiju_bank_w);
888   }
889
890   if (cart_type == CHINFIGHT3)
891   {
892      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x200000);
893      memcpy(&ROM[0x600000], &ROM[VIRGIN_COPY_GEN], 0x200000);
894      memcpy(&ROM[0x000000], &ROM[VIRGIN_COPY_GEN], 0x200000);
895
896      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x4fffff, 0, 0, g_chifi3_prot_r);
897      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x600000, 0x6fffff, 0, 0, g_chifi3_bank_w);
898   }
899
900   if (cart_type == LIONK2)
901   {
902      lion2_prot1_data = lion2_prot2_data = 0;
903
904      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, lion2_prot1_r);
905      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400006, 0x400007, 0, 0, lion2_prot2_r);
906      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, lion2_prot1_w);
907      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, lion2_prot2_w);
908   }
909
910   if (cart_type == BUGSLIFE)
911   {
912      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, bugl_extra_r);
913   }
914
915   if (cart_type == ELFWOR)
916   {
917   /* It return (0x55 @ 0x400000 OR 0xc9 @ 0x400004) AND (0x0f @ 0x400002 OR 0x18 @ 0x400006). It is probably best to add handlers for all 4 addresses. */
918
919      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, elfwor_0x400000_r);
920      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, elfwor_0x400002_r);
921      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400004, 0x400005, 0, 0, elfwor_0x400004_r);
922      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400006, 0x400007, 0, 0, elfwor_0x400006_r);
923   }
924
925   if (cart_type == ROCKMANX3)
926   {
927      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13001, 0, 0, rx3_extra_r);
928   }
929
930   if (cart_type == SBUBBOB)
931   {
932      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, sbub_extra1_r);
933      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400002, 0x400003, 0, 0, sbub_extra2_r);
934   }
935
936   if (cart_type == KOF98)
937   {
938      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x480000, 0x480001, 0, 0, g_kof98_aa_r);
939      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4800e0, 0x4800e1, 0, 0, g_kof98_aa_r);
940      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4824a0, 0x4824a1, 0, 0, g_kof98_aa_r);
941      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x488880, 0x488881, 0, 0, g_kof98_aa_r);
942
943      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4a8820, 0x4a8821, 0, 0, g_kof98_0a_r);
944      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x4f8820, 0x4f8821, 0, 0, g_kof98_00_r);
945   }
946
947   if (cart_type == REALTEC)
948   {
949      /* Realtec mapper!*/
950      realtec_bank_addr = realtec_bank_size = 0;
951      realtec_old_bank_addr = -1;
952
953      memcpy(&ROM[0x400000], &ROM[relocate], 0x80000);
954
955      for (mirroraddr = 0; mirroraddr < 0x400000; mirroraddr += 0x2000)
956      {
957         memcpy(ROM + mirroraddr, ROM + relocate + 0x7e000, 0x002000); /* copy last 8kb across the whole rom region */
958      }
959
960      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x400000, 0x400001, 0, 0, realtec_400000_w);
961      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x402000, 0x402001, 0, 0, realtec_402000_w);
962      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x404000, 0x404001, 0, 0, realtec_404000_w);
963   }
964
965   if (cart_type == SUP19IN1)
966   {
967      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x400000); // allow hard reset to menu
968      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13039, 0, 0, s19in1_bank);
969   }
970
971   if (cart_type == SUP15IN1)
972   {
973      memcpy(&ROM[0x400000], &ROM[VIRGIN_COPY_GEN], 0x200000); // allow hard reset to menu
974      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa13000, 0xa13039, 0, 0, s19in1_bank);
975   }
976
977   /* Disable SRAM handlers if it's a game using EEPROM to save
978    progresses. Currently EEPROM is not emulated. */
979   if (!has_serial_eeprom)
980   {
981      /* Info from DGen: If SRAM does not overlap main ROM, set it active by
982        default since a few games can't manage to properly switch it on/off. */
983      if (genesis_last_loaded_image_length <= genesis_sram_start)
984         genesis_sram_active = 1;
985
986      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa130f0, 0xa130f1, 0, 0, genesis_sram_toggle);
987
988      /* Sonic 1 included in Sonic Classics doesn't have SRAM and
989        does lots of ROM access at this range, then only install read/
990        write handlers if SRAM is active to not slow down emulation. */
991      if (genesis_sram_active)
992         install_sram_rw_handlers (machine);
993   }
994
995   /* install i2c handlers */
996   if(cart_type == NBA_JAM)
997      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, nba_jam_eeprom_r,nba_jam_eeprom_w);
998
999   if(cart_type == WBOY_V)
1000      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, wboy_v_eeprom_r,wboy_v_eeprom_w);
1001
1002   if(cart_type == NBA_JAM_TE || cart_type == NFL_QB_96 || cart_type == C_SLAM) // same handling but different sizes
1003      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, nba_jam_te_eeprom_r,nba_jam_te_eeprom_w);
1004
1005   if(cart_type == EA_NHLPA)
1006      memory_install_readwrite16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x200000, 0x200001, 0, 0, ea_nhlpa_eeprom_r,ea_nhlpa_eeprom_w);
1007
1008   if(cart_type == CODE_MASTERS)
1009   {
1010      memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x300000, 0x300001, 0, 0, codemasters_eeprom_w);
1011      memory_install_read16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x380000, 0x380001, 0, 0, codemasters_eeprom_r);
1012
1013      // TODO: J-Cart device
1014   }
1015
1016   /* install NOP handler for TMSS */
1017   memory_install_write16_handler(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0xa14000, 0xa14003, 0, 0, genesis_TMSS_bank_w);
1018}
1019
1020
1021MACHINE_RESET( md_mappers )
1022{
1023   setup_megadriv_custom_mappers(machine);
1024}
1025
1026
1027/*************************************
1028 *
1029 *  Cart handling
1030 *
1031 *************************************/
1032
1033
1034/******* Memcmp for both endianness *******/
1035
1036static int allendianmemcmp(const void *s1, const void *s2, size_t n)
1037{
1038   const unsigned char *realbuf;
1039
1040#ifdef LSB_FIRST
1041   unsigned char flippedbuf[64];
1042   unsigned int i;
1043
1044   if ((n & 1) || (n > 63)) return -1 ; // don't want odd sized buffers or too large a compare
1045   for (i = 0; i < n; i++) flippedbuf[i] = *((char *)s2 + (i ^ 1));
1046   realbuf = flippedbuf;
1047
1048#else
1049
1050   realbuf = (unsigned char *)s2;
1051
1052#endif
1053   return memcmp(s1,realbuf,n);
1054}
1055
1056
1057/******* SMD files detection *******/
1058
1059/* code taken directly from GoodGEN by Cowering */
1060static int genesis_is_funky_SMD(unsigned char *buf,unsigned int len)
1061{
1062   /* aq quiz */
1063   if (!strncmp("UZ(-01  ", (const char *) &buf[0xf0], 8))
1064      return 1;
1065
1066    /* Phelios USA redump */
1067   /* target earth */
1068   /* klax (namcot) */
1069   if (buf[0x2080] == ' ' && buf[0x0080] == 'S' && buf[0x2081] == 'E' && buf[0x0081] == 'G')
1070      return 1;
1071
1072    /* jap baseball 94 */
1073   if (!strncmp("OL R-AEAL", (const char *) &buf[0xf0], 9))
1074      return 1;
1075
1076    /* devilish Mahjong Tower */
1077    if (!strncmp("optrEtranet", (const char *) &buf[0xf3], 11))
1078      return 1;
1079
1080   /* golden axe 2 beta */
1081   if (buf[0x0100] == 0x3c && buf[0x0101] == 0 && buf[0x0102] == 0 && buf[0x0103] == 0x3c)
1082      return 1;
1083
1084    /* omega race */
1085   if (!strncmp("OEARC   ", (const char *) &buf[0x90], 8))
1086      return 1;
1087
1088    /* budokan beta */
1089   if ((len >= 0x6708+8) && !strncmp(" NTEBDKN", (const char *) &buf[0x6708], 8))
1090      return 1;
1091
1092    /* cdx pro 1.8 bios */
1093   if (!strncmp("so fCXP", (const char *) &buf[0x2c0], 7))
1094      return 1;
1095
1096    /* ishido (hacked) */
1097   if (!strncmp("sio-Wyo ", (const char *) &buf[0x0090], 8))
1098      return 1;
1099
1100    /* onslaught */
1101   if (!strncmp("SS  CAL ", (const char *) &buf[0x0088], 8))
1102      return 1;
1103
1104    /* tram terror pirate */
1105   if ((len >= 0x3648 + 8) && !strncmp("SG NEPIE", (const char *) &buf[0x3648], 8))
1106      return 1;
1107
1108    /* breath of fire 3 chinese */
1109   if (buf[0x0007] == 0x1c && buf[0x0008] == 0x0a && buf[0x0009] == 0xb8 && buf[0x000a] == 0x0a)
1110      return 1;
1111
1112    /*tetris pirate */
1113   if ((len >= 0x1cbe + 5) && !strncmp("@TTI>", (const char *) &buf[0x1cbe], 5))
1114      return 1;
1115
1116   return 0;
1117}
1118
1119
1120
1121/* code taken directly from GoodGEN by Cowering */
1122static int genesis_is_SMD(unsigned char *buf,unsigned int len)
1123{
1124   if (buf[0x2080] == 'S' && buf[0x80] == 'E' && buf[0x2081] == 'G' && buf[0x81] == 'A')
1125      return 1;
1126   return genesis_is_funky_SMD(buf,len);
1127}
1128
1129
1130/******* Image loading *******/
1131
1132static DEVICE_IMAGE_LOAD( genesis_cart )
1133{
1134   unsigned char *ROM, *rawROM, *tmpROMnew, *tmpROM, *secondhalf;
1135   int relocate, length, ptr, x, sram_detected;
1136#ifdef LSB_FIRST
1137   unsigned char fliptemp;
1138#endif
1139
1140   genesis_sram = NULL;
1141   sram_detected = 0;
1142   genesis_sram_start = genesis_sram_end = 0;
1143   has_serial_eeprom = 0;
1144   cart_type = STANDARD;
1145
1146   if (image.software_entry() == NULL)
1147   {
1148      rawROM = image.device().machine->region("maincpu")->base();
1149      ROM = rawROM /*+ 512 */;
1150
1151      genesis_last_loaded_image_length = -1;
1152
1153      length = image.fread( rawROM + 0x2000, 0x600000);
1154
1155      logerror("image length = 0x%x\n", length);
1156
1157      /* is this a SMD file? */
1158      if (genesis_is_SMD(&rawROM[0x2200], (unsigned)length))
1159      {
1160         tmpROMnew = ROM;
1161         tmpROM = ROM + 0x2000 + 512;
1162
1163         for (ptr = 0; ptr < MAX_MD_CART_SIZE / (8192); ptr += 2)
1164         {
1165            for (x = 0; x < 8192; x++)
1166            {
1167               *tmpROMnew++ = *(tmpROM + ((ptr + 1) * 8192) + x);
1168               *tmpROMnew++ = *(tmpROM + ((ptr + 0) * 8192) + x);
1169            }
1170         }
1171
1172#ifdef LSB_FIRST
1173         tmpROMnew = ROM;
1174         for (ptr = 0; ptr < length; ptr += 2)
1175         {
1176            fliptemp = tmpROMnew[ptr];
1177            tmpROMnew[ptr] = tmpROMnew[ptr + 1];
1178            tmpROMnew[ptr + 1] = fliptemp;
1179         }
1180#endif
1181         genesis_last_loaded_image_length = length - 512;
1182         memcpy(&ROM[VIRGIN_COPY_GEN], &ROM[0x000000], MAX_MD_CART_SIZE);  /* store a copy of data for MACHINE_RESET processing */
1183
1184         relocate = 0;
1185
1186      }
1187
1188      /* is this a MD file? */
1189      else if ((rawROM[0x2080] == 'E') && (rawROM[0x2081] == 'A') &&
1190             (rawROM[0x2082] == 'M' || rawROM[0x2082] == 'G'))
1191      {
1192         tmpROMnew = (unsigned char *)osd_malloc(length);
1193         secondhalf = &tmpROMnew[length >> 1];
1194
1195         if (!tmpROMnew)
1196         {
1197            logerror("Memory allocation failed reading roms!\n");
1198            return IMAGE_INIT_FAIL;
1199         }
1200
1201         memcpy(tmpROMnew, ROM + 0x2000, length);
1202         for (ptr = 0; ptr < length; ptr += 2)
1203         {
1204
1205            ROM[ptr] = secondhalf[ptr >> 1];
1206            ROM[ptr + 1] = tmpROMnew[ptr >> 1];
1207         }
1208         free(tmpROMnew);
1209
1210#ifdef LSB_FIRST
1211         for (ptr = 0; ptr < length; ptr += 2)
1212         {
1213            fliptemp = ROM[ptr];
1214            ROM[ptr] = ROM[ptr+1];
1215            ROM[ptr+1] = fliptemp;
1216         }
1217#endif
1218         genesis_last_loaded_image_length = length;
1219         memcpy(&ROM[VIRGIN_COPY_GEN], &ROM[0x000000], MAX_MD_CART_SIZE);  /* store a copy of data for MACHINE_RESET processing */
1220         relocate = 0;
1221
1222      }
1223
1224      /* BIN it is, then */
1225      else
1226      {
1227         relocate = 0x2000;
1228         genesis_last_loaded_image_length = length;
1229
1230         for (ptr = 0; ptr < MAX_MD_CART_SIZE + relocate; ptr += 2)      /* mangle bytes for little endian machines */
1231         {
1232#ifdef LSB_FIRST
1233            int temp = ROM[relocate + ptr];
1234
1235            ROM[ptr] = ROM[relocate + ptr + 1];
1236            ROM[ptr + 1] = temp;
1237#else
1238            ROM[ptr] = ROM[relocate + ptr];
1239            ROM[ptr + 1] = ROM[relocate + ptr + 1];
1240#endif
1241         }
1242
1243         memcpy(&ROM[VIRGIN_COPY_GEN], &ROM[0x000000], MAX_MD_CART_SIZE);  /* store a copy of data for MACHINE_RESET processing */
1244      }
1245
1246      /* Default cartridge type */
1247      cart_type = STANDARD;
1248
1249      /* Detect carts which need additional handlers */
1250      {
1251        static const unsigned char smouse_sig[] = { 0x4d, 0xf9, 0x00, 0x40, 0x00, 0x02 },
1252         mjlover_sig[]   = { 0x13, 0xf9, 0x00, 0x40, 0x00, 0x00 }, // move.b  ($400000).l,($FFFF0C).l (partial)
1253         squir_sig[]      = { 0x26, 0x79, 0x00, 0xff, 0x00, 0xfa },
1254         bugsl_sig[]      = { 0x20, 0x12, 0x13, 0xc0, 0x00, 0xff },
1255         sbub_sig[]      = { 0x0c, 0x39, 0x00, 0x55, 0x00, 0x40 }, // cmpi.b  #$55,($400000).l
1256         lk3_sig[]      = { 0x0c, 0x01, 0x00, 0x30, 0x66, 0xe4 },
1257         sdk_sig[]      = { 0x48, 0xe7, 0xff, 0xfe, 0x52, 0x79 },
1258         redcliff_sig[]   = { 0x10, 0x39, 0x00, 0x40, 0x00, 0x04 }, // move.b  ($400004).l,d0
1259         redcl_en_sig[]   = { 0x50, 0x79, 0x40, 0x00, 0x40, 0x44 }, // move.b  ($400004).l,d0
1260         smb_sig[]      = { 0x20, 0x4d, 0x41, 0x52, 0x49, 0x4f },
1261         smb2_sig[]      = { 0x4e, 0xb9, 0x00, 0x0f, 0x25, 0x84 },
1262         kaiju_sig[]      = { 0x19, 0x7c, 0x00, 0x01, 0x00, 0x00 },
1263         chifi3_sig[]   = { 0xb6, 0x16, 0x66, 0x00, 0x00, 0x4a },
1264         lionk2_sig[]   = { 0x26, 0x79, 0x00, 0xff, 0x00, 0xf4 },
1265         rx3_sig[]      = { 0x66, 0x00, 0x00, 0x0e, 0x30, 0x3c },
1266         kof98_sig[]      = { 0x9b, 0xfc, 0x00, 0x00, 0x4a, 0x00 },
1267         s15in1_sig[]   = { 0x22, 0x3c, 0x00, 0xa1, 0x30, 0x00 },
1268         kof99_sig[]      = { 0x20, 0x3c, 0x30, 0x00, 0x00, 0xa1 }, // move.l  #$300000A1,d0
1269         radica_sig[]   = { 0x4e, 0xd0, 0x30, 0x39, 0x00, 0xa1 }, // jmp (a0) move.w ($a130xx),d0
1270         soulb_sig[]      = { 0x33, 0xfc, 0x00, 0x0c, 0x00, 0xff }, // move.w  #$C,($FF020A).l (what happens if check fails)
1271         s19in1_sig[]   = { 0x13, 0xc0, 0x00, 0xa1, 0x30, 0x38 },
1272         rockman_sig[]   = { 0xea, 0x80 };
1273
1274         switch (genesis_last_loaded_image_length)
1275         {
1276         case 0x80000:
1277             if (!allendianmemcmp(&ROM[0x08c8], &smouse_sig[0], sizeof(smouse_sig)))
1278            cart_type = SMOUSE;
1279
1280             if (!allendianmemcmp((char *)&ROM[0x7e30e], "SEGA", 4) ||
1281            !allendianmemcmp((char *)&ROM[0x7e100], "SEGA", 4) ||
1282            !allendianmemcmp((char *)&ROM[0x7e1e6], "SEGA", 4))
1283            cart_type = REALTEC;
1284
1285             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-50396", 10)) // NHLPA Hockey 93
1286            cart_type = EA_NHLPA;
1287
1288             if (!allendianmemcmp((char *)&ROM[0x0180], "GM MK-1215", 10)) // Evander Holyfield
1289            cart_type = WBOY_V;
1290
1291             break;
1292
1293         case 0xc0000:
1294
1295             if (!allendianmemcmp((char *)&ROM[0x0180], "GM G-4060 ", 8)) // Wonder Boy V
1296            cart_type = WBOY_V;
1297
1298             break;
1299
1300         case 0x100000:
1301             if (!allendianmemcmp(&ROM[0x01b24], &mjlover_sig[0], sizeof(mjlover_sig)))
1302            cart_type = MJLOVER;
1303
1304             if (!allendianmemcmp(&ROM[0x03b4], &squir_sig[0], sizeof(squir_sig)))
1305            cart_type = SQUIRRELK;
1306
1307             if (!allendianmemcmp(&ROM[0xee0d0], &bugsl_sig[0], sizeof(bugsl_sig)))
1308            cart_type = BUGSLIFE;
1309
1310             if (!allendianmemcmp((char *)&ROM[0x0172], "GAME : ELF WOR", 14))
1311            cart_type = ELFWOR;
1312
1313             if (!allendianmemcmp(&ROM[0x123e4], &sbub_sig[0], sizeof(sbub_sig)))
1314            cart_type = SBUBBOB;
1315
1316             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-50176", 10)) // Rings of Power
1317            cart_type = EA_NHLPA;
1318
1319             if (!allendianmemcmp((char *)&ROM[0x0180], "MK 00001211-00", 14)) // Sports Talk Baseball
1320            cart_type = WBOY_V;
1321
1322             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-120096-", 12)) // Micro Machines 2
1323            cart_type = CODE_MASTERS;
1324
1325             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-120146-", 12)) // Brian Lara Cricket 96 / Shane Wayne Cricket 96
1326            cart_type = CODE_MASTERS;
1327
1328             if (!allendianmemcmp((char *)&ROM[0x0190], "OJKRPTBVFCA     ", 0x10)) // Micro Machines '96 / Military TODO: better way to recognize these?
1329            cart_type = CODE_MASTERS;
1330             break;
1331
1332         case 0x200000:
1333             if (!allendianmemcmp(&ROM[0x18c6], &lk3_sig[0], sizeof(lk3_sig)))
1334            cart_type = LIONK3;
1335
1336             if (!allendianmemcmp(&ROM[0x220], &sdk_sig[0], sizeof(sdk_sig)))
1337            cart_type = SKINGKONG;
1338
1339             if (!allendianmemcmp(&ROM[0xce560], &redcliff_sig[0], sizeof(redcliff_sig)))
1340            cart_type = REDCLIFF;
1341
1342             if (!allendianmemcmp(&ROM[0xc8cb0], &smb_sig[0], sizeof(smb_sig)))
1343            cart_type = SMB;
1344
1345             if (!allendianmemcmp(&ROM[0xf24d6], &smb2_sig[0], sizeof(smb2_sig)))
1346            cart_type = SMB2;
1347
1348             if (!allendianmemcmp(&ROM[0x674e], &kaiju_sig[0], sizeof(kaiju_sig)))
1349            cart_type = KAIJU;
1350
1351             if (!allendianmemcmp(&ROM[0x1780], &chifi3_sig[0], sizeof(chifi3_sig)))
1352            cart_type = CHINFIGHT3;
1353
1354             if (!allendianmemcmp(&ROM[0x03c2], &lionk2_sig[0], sizeof(lionk2_sig)))
1355            cart_type = LIONK2;
1356
1357             if (!allendianmemcmp(&ROM[0xc8b90], &rx3_sig[0], sizeof(rx3_sig)))
1358            cart_type = ROCKMANX3;
1359
1360             if (!allendianmemcmp(&ROM[0x56ae2], &kof98_sig[0], sizeof(kof98_sig)))
1361            cart_type = KOF98;
1362
1363             if (!allendianmemcmp(&ROM[0x17bb2], &s15in1_sig[0], sizeof(s15in1_sig)))
1364            cart_type = SUP15IN1;
1365
1366             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081326 ", 12)) // NBA Jam
1367            cart_type = NBA_JAM;
1368
1369             if (!allendianmemcmp((char *)&ROM[0x0180], "GM MK-1228", 10)) // Greatest Heavyweight of the Ring
1370            cart_type = WBOY_V;
1371
1372             if ((!allendianmemcmp((char *)&ROM[0x0180], "GM T-12046", 10)) || // Mega Man
1373            (!allendianmemcmp((char *)&ROM[0x0180], "GM T-12053", 10) && !allendianmemcmp(&ROM[0x18e], &rockman_sig[0], sizeof(rockman_sig)))) // / Rock Man (EEPROM version)
1374            cart_type = WBOY_V;
1375
1376             break;
1377
1378         case 0x200005:
1379             if (!allendianmemcmp(&ROM[0xce564], &redcl_en_sig[0], sizeof(redcliff_sig)))
1380            cart_type = REDCL_EN;
1381             break;
1382
1383         case 0x300000:
1384             if (!allendianmemcmp(&ROM[0x220], &sdk_sig[0], sizeof(sdk_sig)))
1385            cart_type = SDK99;
1386
1387             if (!allendianmemcmp(&ROM[0x1fd0d2], &kof99_sig[0], sizeof(kof99_sig)))
1388            cart_type = KOF99;
1389
1390             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-81406", 10)) // NBA Jam TE
1391            cart_type = NBA_JAM_TE;
1392
1393             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081276 ", 12)) // NFL Quarterback Club
1394            cart_type = NBA_JAM_TE;
1395
1396             break;
1397
1398         case 0x400000:
1399             if (!allendianmemcmp(&ROM[0x3c031c], &radica_sig[0], sizeof(radica_sig)) ||
1400            !allendianmemcmp(&ROM[0x3f031c], &radica_sig[0], sizeof(radica_sig))) // ssf+gng + radica vol1
1401            cart_type = RADICA;
1402
1403             if (!allendianmemcmp(&ROM[0x028460], &soulb_sig[0], sizeof(soulb_sig)))
1404            cart_type = SOULBLAD;
1405
1406             if (!allendianmemcmp(&ROM[0x1e700], &s19in1_sig[0], sizeof(s19in1_sig)))
1407            cart_type = SUP19IN1;
1408
1409             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081586-", 12)) // NFL Quarterback Club 96
1410            cart_type = NFL_QB_96;
1411
1412             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-081576 ", 12)) // College Slam
1413            cart_type = C_SLAM;
1414
1415             if (!allendianmemcmp((char *)&ROM[0x0180], "GM T-81476", 10)) // Big Hurt Baseball
1416            cart_type = C_SLAM;
1417
1418             break;
1419
1420         case 0x500000:
1421             if (!allendianmemcmp((char *)&ROM[0x0120], "SUPER STREET FIGHTER2 ", 22))
1422            cart_type = SSF2;
1423             break;
1424
1425         default:
1426             break;
1427         }
1428      }
1429
1430      logerror("cart type: %d\n", cart_type);
1431
1432      genesis_sram = NULL;
1433      sram_detected = 0;
1434      genesis_sram_start = genesis_sram_end = 0;
1435      has_serial_eeprom = 0;
1436
1437      /* check if cart has battery save */
1438
1439      /* For games using SRAM, unfortunately there are ROMs without info
1440           about it in header. The solution adopted is do the mapping anyway,
1441           then active SRAM later if the game will access it. */
1442
1443      if (ROM[0x1b1] == 'R' && ROM[0x1b0] == 'A')
1444      {
1445         /* SRAM info found in header */
1446         genesis_sram_start = (ROM[0x1b5] << 24 | ROM[0x1b4] << 16 | ROM[0x1b7] << 8 | ROM[0x1b6]);
1447         genesis_sram_end = (ROM[0x1b9] << 24 | ROM[0x1b8] << 16 | ROM[0x1bb] << 8 | ROM[0x1ba]);
1448
1449         if ((genesis_sram_start > genesis_sram_end) || ((genesis_sram_end - genesis_sram_start) >= 0x10000))   // we assume at most 64k of SRAM (HazeMD uses at most 64k). is this correct?
1450           genesis_sram_end = genesis_sram_start + 0x0FFFF;
1451
1452         /* for some games using serial EEPROM, difference between SRAM
1453               end to start is 0 or 1. Currently EEPROM is not emulated. */
1454         if ((genesis_sram_end - genesis_sram_start) < 2)
1455           has_serial_eeprom = 1;
1456         else
1457           sram_detected = 1;
1458      }
1459      else
1460      {
1461         /* set default SRAM positions, with size = 64k */
1462         genesis_sram_start = 0x200000;
1463         genesis_sram_end = genesis_sram_start + 0xffff;
1464      }
1465
1466      if (genesis_sram_start & 1)
1467        genesis_sram_start -= 1;
1468
1469      if (!(genesis_sram_end & 1))
1470        genesis_sram_end += 1;
1471
1472      /* calculate backup RAM location */
1473      megadriv_backupram = (UINT16*) (ROM + (genesis_sram_start & 0x3fffff));
1474
1475      /* Until serial EEPROM is emulated, clears one byte at beginning of
1476           backup RAM, but only if the value is 0xFFFF, to not break MLBPA Sports
1477           Talk Baseball. With this hack some games at least run (NBA Jam, Evander
1478           Holyfield's Real Deal Boxing, Greatest Heavyweights of the Ring) */
1479      //if (has_serial_eeprom && megadriv_backupram[0] == 0xffff)
1480      //  megadriv_backupram[0] = 0xff00;
1481
1482      if (sram_detected)
1483        logerror("SRAM detected from header: starting location %X - SRAM Length %X\n", genesis_sram_start, genesis_sram_end - genesis_sram_start + 1);
1484   }
1485   else /* Handle pcb with the softlist info */
1486   {
1487      int      pcb_id;
1488      const char   *pcb_name;
1489
1490      length = image.get_software_region_length("rom");
1491      ROM = image.device().machine->region("maincpu")->base();
1492      memcpy(ROM, image.get_software_region("rom"), length);
1493
1494      genesis_last_loaded_image_length = length;
1495      megadriv_backupram = NULL;
1496      if ((pcb_name = image.get_feature("pcb")) == NULL)
1497        pcb_id = STD_ROM;
1498      else
1499        pcb_id = md_get_pcb_id(pcb_name);
1500
1501      switch (pcb_id)
1502      {
1503         /* Sega PCB */
1504        case SEGA_6584A:
1505         fatalerror("Need Serial EEPROM emulation");
1506         break;
1507        case SEGA_5921:
1508        case SEGA_6278A:
1509         genesis_sram_start = 0x200000;
1510         genesis_sram_end = genesis_sram_start + 0x3fff;
1511         sram_detected = 1;
1512         megadriv_backupram = (UINT16*) (ROM + (genesis_sram_start & 0x3fffff));
1513         break;
1514        case SEGA_6658A:
1515         genesis_sram_start = 0x200000;
1516         genesis_sram_end = genesis_sram_start + 0x3ff;
1517         sram_detected = 1;
1518         megadriv_backupram = (UINT16*) (ROM + (genesis_sram_start & 0x3fffff));
1519         break;
1520
1521         /* Codemasters PCB (J-Carts) */
1522        case CM_JCART:
1523         memory_install_read16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_r);
1524         memory_install_write16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_w);
1525         break;
1526        case CM_JCART_SEPROM:
1527         memory_install_read16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_r);
1528         memory_install_write16_handler(cputag_get_address_space(image.device().machine, "maincpu", ADDRESS_SPACE_PROGRAM), 0x38FFFE, 0x38FFFF, 0, 0, jcart_ctrl_w);
1529         /* TODO add SEPROM part */
1530         break;
1531      }
1532   }
1533
1534   return IMAGE_INIT_PASS;
1535}
1536
1537/******* Image unloading (with SRAM saving) *******/
1538
1539static DEVICE_IMAGE_UNLOAD( genesis_cart )
1540{
1541   /* Write out the battery file if necessary */
1542   if (genesis_sram != NULL)
1543   {
1544      image.battery_save(genesis_sram, genesis_sram_end - genesis_sram_start + 1);
1545      free(genesis_sram);
1546   }
1547}
1548
1549
1550/******* 32X image loading *******/
1551
1552// FIXME: non-softlist loading should keep using ROM_CART_LOAD in the ROM definitions,
1553// once we better integrate softlist with the old loading procedures
1554static DEVICE_IMAGE_LOAD( _32x_cart )
1555{
1556   UINT32 length;
1557   UINT8 *temp_copy;
1558   UINT16 *ROM16;
1559   UINT32 *ROM32;
1560   int i;
1561
1562   if (image.software_entry() == NULL)
1563   {
1564      length = image.length();
1565      temp_copy = auto_alloc_array(image.device().machine, UINT8, length);
1566      image.fread( temp_copy, length);
1567   }
1568   else
1569   {
1570      length = image.get_software_region_length("rom");
1571      temp_copy = auto_alloc_array(image.device().machine, UINT8, length);
1572      memcpy(temp_copy, image.get_software_region("rom"), length);
1573   }
1574
1575   /* Copy the cart image in the locations the driver expects */
1576   // Notice that, by using pick_integer, we are sure the code works on both LE and BE machines
1577   ROM16 = (UINT16 *) image.device().machine->region("gamecart")->base();
1578   for (i = 0; i < length; i += 2)
1579      ROM16[i / 2] = pick_integer_be(temp_copy, i, 2);
1580
1581   ROM32 = (UINT32 *) image.device().machine->region("gamecart_sh2")->base();
1582   for (i = 0; i < length; i += 4)
1583      ROM32[i / 4] = pick_integer_be(temp_copy, i, 4);
1584
1585   ROM16 = (UINT16 *) image.device().machine->region("maincpu")->base();
1586   for (i = 0x00; i < length; i += 2)
1587      ROM16[i / 2] = pick_integer_be(temp_copy, i, 2);
1588
1589   auto_free(image.device().machine, temp_copy);
1590
1591   cart_type = NBA_JAM_TE;
1592
1593   return IMAGE_INIT_PASS;
1594}
1595
1596/******* Cart getinfo *******/
1597
1598MACHINE_CONFIG_FRAGMENT( genesis_cartslot )
1599   MCFG_CARTSLOT_ADD("cart")
1600   MCFG_CARTSLOT_EXTENSION_LIST("smd,bin,md,gen")
1601   MCFG_CARTSLOT_MANDATORY
1602   MCFG_CARTSLOT_INTERFACE("megadriv_cart")
1603   MCFG_CARTSLOT_LOAD(genesis_cart)
1604   MCFG_CARTSLOT_UNLOAD(genesis_cart)
1605   MCFG_SOFTWARE_LIST_ADD("cart_list","megadriv")
1606MACHINE_CONFIG_END
1607
1608MACHINE_CONFIG_FRAGMENT( _32x_cartslot )
1609   MCFG_CARTSLOT_ADD("cart")
1610   MCFG_CARTSLOT_EXTENSION_LIST("32x,bin")
1611   MCFG_CARTSLOT_MANDATORY
1612   MCFG_CARTSLOT_INTERFACE("_32x_cart")
1613   MCFG_CARTSLOT_LOAD(_32x_cart)
1614   MCFG_SOFTWARE_LIST_ADD("cart_list","32x")
1615MACHINE_CONFIG_END
1616
1617MACHINE_CONFIG_FRAGMENT( pico_cartslot )
1618   MCFG_CARTSLOT_ADD("cart")
1619   MCFG_CARTSLOT_EXTENSION_LIST("bin")
1620   MCFG_CARTSLOT_MANDATORY
1621   MCFG_CARTSLOT_INTERFACE("pico_cart")
1622   MCFG_CARTSLOT_LOAD(genesis_cart)
1623   MCFG_CARTSLOT_UNLOAD(genesis_cart)
1624   MCFG_SOFTWARE_LIST_ADD("cart_list","pico")
1625MACHINE_CONFIG_END
src/mess/machine/gen_pcb.c
r10033r10034
1/*****************************************************************************************
2
3    Genesis/MegaDrive Cart PCBs Emulation
4
5****************************************************************************************/
6
7#include "emu.h"
8#include "gen_pcb.h"
9
10typedef struct _md_pcb  md_pcb;
11struct _md_pcb
12{
13   const char              *pcb_name;
14   int                     pcb_id;
15};
16
17// Here, we take the feature attribute from .xml (i.e. the PCB name) and we assign a unique ID to it
18static const md_pcb pcb_list[] =
19{
20   /* Standard Sega PCB (one rom) */
21   {"171-5703", STD_ROM},
22   {"171-5927", STD_ROM},
23   {"171-5978B", STD_ROM},
24   {"171-5978BA", STD_ROM},
25   {"171-6329A", STD_ROM},
26
27   /* Sega PCB with two roms */
28   {"171-5841", STD_ROM},
29
30   /* Sega PCB with Serial EEPROM */
31   {"171-5878", SEGA_5878},
32   {"171-6584A", SEGA_6584A},
33
34   /* Sega PCB with sram */
35   {"171-5921", SEGA_5921},
36   {"171-6278A", SEGA_6278A},
37
38   /* Sega PCB with fram */
39   {"171-6658A", SEGA_6658A},
40
41   /* Namcot PCB (blob epoxy chip) */
42   {"837-8861", STD_ROM},
43
44   /* Codemasters PCB */
45   {"SRJCV1-2", CM_JCART},
46   {"SRJCV2-1", CM_JCART_SEPROM},
47   {"SRJCV2-2", CM_JCART_SEPROM}
48};
49
50int md_get_pcb_id(const char *pcb)
51{
52   int   i;
53
54   for (i = 0; i < ARRAY_LENGTH(pcb_list); i++)
55   {
56      if (!mame_stricmp(pcb_list[i].pcb_name, pcb))
57         return pcb_list[i].pcb_id;
58   }
59
60   return STD_ROM;
61}
src/mess/includes/genesis.h
r10033r10034
1/*****************************************************************************
2 *
3 *  includes/genesis.h
4 *
5 ****************************************************************************/
6
7#ifndef GENESIS_H_
8#define GENESIS_H_
9
10#include "imagedev/cartslot.h"
11
12/*----------- defined in machine/genesis.c -----------*/
13
14MACHINE_RESET( md_mappers );
15
16MACHINE_CONFIG_EXTERN( genesis_cartslot );
17MACHINE_CONFIG_EXTERN( _32x_cartslot );
18MACHINE_CONFIG_EXTERN( pico_cartslot );
19
20WRITE16_HANDLER( jcart_ctrl_w );
21READ16_HANDLER( jcart_ctrl_r );
22
23#endif /* GENESIS_H_ */
src/mess/mess.mak
r10033r10034
466466   $(MAME_VIDEO)/stvvdp2.o      \
467467   $(MAME_DRIVERS)/megadriv.o  \
468468   $(MAME_DRIVERS)/megadrvb.o  \
469   $(MAME_MACHINE)/megadriv.o   \
470   $(MAME_MACHINE)/md_pcb.o   \
469471   $(MAME_MACHINE)/dc.o      \
470472   $(MAME_MACHINE)/naomibd.o   \
471473   $(MAME_MACHINE)/gdcrypt.o   \
r10033r10034
13741376   $(MESS_DRIVERS)/phc25.o      \
13751377
13761378$(MESSOBJ)/sega.a:            \
1377   $(MESS_DRIVERS)/genesis.o   \
1378   $(MESS_MACHINE)/genesis.o   \
1379   $(MESS_MACHINE)/gen_pcb.o   \
13801379   $(MESS_DRIVERS)/saturn.o   \
13811380   $(MESS_VIDEO)/smsvdp.o      \
13821381   $(MESS_MACHINE)/sms.o      \
src/mess/drivers/genesis.c
r10033r10034
1/*
2
3Megadrive / Genesis Rewrite, Take 65498465432356345250432.3  August 06
4
5Thanks to:
6Charles Macdonald for much useful information (cgfm2.emuviews.com)
7
8Long Description names mostly taken from the GoodGen database
9
10ToDo:
11
12The Code here is terrible for now, this is just for testing
13Fix HV Counter & Raster Implementation (One line errors in some games, others not working eg. Dracula)
14Fix Horizontal timings (currently a kludge, currently doesn't change with resolution changes)
15Add Real DMA timings (using a timer)
16Add All VDP etc. Mirror addresses (not done yet as I prefer to catch odd cases for now)
17Investigate other Bugs (see list below)
18Rewrite (again) using cleaner, more readable and better optimized code with knowledge gained
19Add support for other peripherals (6 player pad, Teamplay Adapters, Lightguns, Sega Mouse etc.)
20Sort out set info, making sure all games have right manufacturers, dates etc.
21Make sure everything that needs backup RAM has it setup and working
22Fix Reset glitches
23Add 32X / SegaCD support
24Add Megaplay, Megatech support (needs SMS emulation too)
25Add other obscure features (palette flicker for mid-screen CRAM changes, undocumented register behavior)
26Figure out how sprite masking *really* works
27Add EEprom support in games that need it
28
29Known Issues:
30    Bass Masters Classic Pro Edition (U) [!] - Sega Logo is corrupt
31    Bram Stoker's Dracula (U) [!] - Doesn't Work (HV Timing)
32    Double Dragon 2 - The Revenge (J) [!] - Too Slow?
33    International Superstar Soccer Deluxe (E) [!] - Single line Raster Glitch
34    Lemmings (JU) (REV01) [!] - Rasters off by ~7-8 lines (strange case)
35    Mercs - Sometimes sound doesn't work..
36
37    Some beta games without proper sound programs seem to crash because the z80 crashes
38
39Known Non-Issues (confirmed on Real Genesis)
40    Castlevania - Bloodlines (U) [!] - Pause text is missing on upside down level
41    Blood Shot (E) (M4) [!] - corrupt texture in level 1 is correct...
42
43----------------------
44
45Cartridge support by Gareth S. Long
46
47MESS adaptation by R. Belmont
48
49*/
50
51#include "emu.h"
52#include "sound/2612intf.h"
53#include "includes/megadriv.h"
54
55/* cart device, custom mappers & sram init */
56#include "../includes/genesis.h"
57
58
59static int megadrive_region_export = 0;
60static int megadrive_region_pal = 0;
61
62
63/*************************************
64 *
65 *  Input handlers
66 *
67 *************************************/
68
69/* We need to always initialize 6 buttons pad */
70static emu_timer *mess_io_timeout[3];
71static int mess_io_stage[3];
72
73static TIMER_CALLBACK( mess_io_timeout_timer_callback )
74{
75   mess_io_stage[(int)(FPTR)ptr] = -1;
76}
77
78/* J-Cart controller port */
79
80static UINT8   jcart_io_data[2];
81
82WRITE16_HANDLER( jcart_ctrl_w )
83{
84   jcart_io_data[0] = (data & 1) << 6;
85   jcart_io_data[1] = (data & 1) << 6;
86}
87
88READ16_HANDLER( jcart_ctrl_r )
89{
90   UINT16   retdata = 0;
91   UINT8      joy[2];
92
93   if (jcart_io_data[0] & 0x40)
94   {
95      joy[0] = input_port_read_safe(space->machine, "JCART3_3B", 0);
96      joy[1] = input_port_read_safe(space->machine, "JCART4_3B", 0);
97      retdata = (jcart_io_data[0] & 0x40) | joy[0] | (joy[1] << 8);
98   }
99   else
100   {
101      joy[0] = ((input_port_read_safe(space->machine, "JCART3_3B", 0) & 0xc0) >> 2) |
102        (input_port_read_safe(space->machine, "JCART3_3B", 0) & 0x03);
103      joy[1] = ((input_port_read_safe(space->machine, "JCART4_3B", 0) & 0xc0) >> 2) |
104        (input_port_read_safe(space->machine, "JCART4_3B", 0) & 0x03);
105      retdata = (jcart_io_data[0] & 0x40) | joy[0] | (joy[1] << 8);
106   }
107   return retdata;
108}
109
110static void mess_init_6buttons_pad(running_machine *machine)
111{
112   int i;
113
114   for (i = 0; i < 3; i++)
115   {
116      mess_io_timeout[i] = timer_alloc(machine, mess_io_timeout_timer_callback, (void*)(FPTR)i);
117      mess_io_stage[i] = -1;
118   }
119}
120
121/* These overwrite the MAME ones in DRIVER_INIT */
122/* They're needed to give the users the choice between different controllers */
123static UINT8 mess_md_io_read_data_port(running_machine *machine, int portnum)
124{
125   static const char *const pad6names[2][4] = {
126      { "PAD1_6B", "PAD2_6B", "UNUSED", "UNUSED" },
127      { "EXTRA1", "EXTRA2", "UNUSED", "UNUSED" }
128   };
129   static const char *const pad3names[4] = { "PAD1_3B", "PAD2_3B", "UNUSED", "UNUSED" };
130
131   UINT8 retdata;
132   int controller;
133   UINT8 helper_6b = (megadrive_io_ctrl_regs[portnum] & 0x3f) | 0xc0; // bits 6 & 7 always come from megadrive_io_data_regs
134   UINT8 helper_3b = (megadrive_io_ctrl_regs[portnum] & 0x7f) | 0x80; // bit 7 always comes from megadrive_io_data_regs
135
136   switch (portnum)
137   {
138      case 0:
139         controller = (input_port_read(machine, "CTRLSEL") & 0x0f);
140         break;
141
142      case 1:
143         controller = (input_port_read(machine, "CTRLSEL") & 0xf0);
144         break;
145
146      default:
147         controller = 0;
148         break;
149   }
150
151   /* Are we using a 6 buttons Joypad? */
152   if (controller)
153   {
154      if (megadrive_io_data_regs[portnum] & 0x40)
155      {
156         if (mess_io_stage[portnum] == 2)
157         {
158            /* here we read B, C & the additional buttons */
159            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
160                     (((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0x30) |
161                        (input_port_read_safe(machine, pad6names[1][portnum], 0) & 0x0f)) & ~helper_6b);
162         }
163         else
164         {
165            /* here we read B, C & the directional buttons */
166            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
167                     ((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0x3f) & ~helper_6b);
168         }
169      }
170      else
171      {
172         if (mess_io_stage[portnum] == 1)
173         {
174            /* here we read ((Start & A) >> 2) | 0x00 */
175            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
176                     (((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0xc0) >> 2) & ~helper_6b);
177         }
178         else if (mess_io_stage[portnum]==2)
179         {
180            /* here we read ((Start & A) >> 2) | 0x0f */
181            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
182                     ((((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0xc0) >> 2) | 0x0f) & ~helper_6b);
183         }
184         else
185         {
186            /* here we read ((Start & A) >> 2) | Up and Down */
187            retdata = (megadrive_io_data_regs[portnum] & helper_6b) |
188                     ((((input_port_read_safe(machine, pad6names[0][portnum], 0) & 0xc0) >> 2) |
189                        (input_port_read_safe(machine, pad6names[0][portnum], 0) & 0x03)) & ~helper_6b);
190         }
191      }
192
193   //  mame_printf_debug("read io data port stage %d port %d %02x\n",mess_io_stage[portnum],portnum,retdata);
194
195      retdata |= (retdata << 8);
196   }
197   /* Otherwise it's a 3 buttons Joypad */
198   else
199   {
200      if (megadrive_io_data_regs[portnum] & 0x40)
201      {
202         /* here we read B, C & the directional buttons */
203         retdata = (megadrive_io_data_regs[portnum] & helper_3b) |
204                  (((input_port_read_safe(machine, pad3names[portnum], 0) & 0x3f) | 0x40) & ~helper_3b);
205      }
206      else
207      {
208         /* here we read ((Start & A) >> 2) | Up and Down */
209         retdata = (megadrive_io_data_regs[portnum] & helper_3b) |
210                  ((((input_port_read_safe(machine, pad3names[portnum], 0) & 0xc0) >> 2) |
211                     (input_port_read_safe(machine, pad3names[portnum], 0) & 0x03) | 0x40) & ~helper_3b);
212      }
213   }
214
215   return retdata;
216}
217
218
219static void mess_md_io_write_data_port(running_machine *machine, int portnum, UINT16 data)
220{
221   int controller;
222
223   switch (portnum)
224   {
225      case 0:
226         controller = (input_port_read(machine, "CTRLSEL") & 0x0f);
227         break;
228
229      case 1:
230         controller = (input_port_read(machine, "CTRLSEL") & 0xf0);
231         break;
232
233      default:
234         controller = 0;
235         break;
236   }
237
238   if (controller)
239   {
240      if (megadrive_io_ctrl_regs[portnum] & 0x40)
241      {
242         if (((megadrive_io_data_regs[portnum] & 0x40) == 0x00) && ((data & 0x40) == 0x40))
243         {
244            mess_io_stage[portnum]++;
245            timer_adjust_oneshot(mess_io_timeout[portnum], machine->device<cpu_device>("maincpu")->cycles_to_attotime(8192), 0);
246         }
247
248      }
249   }
250   megadrive_io_data_regs[portnum] = data;
251   //mame_printf_debug("Writing IO Data Register #%d data %04x\n",portnum,data);
252}
253
254
255/*************************************
256 *
257 *  Input ports
258 *
259 *************************************/
260
261
262static INPUT_PORTS_START( md )
263   PORT_START("CTRLSEL")   /* Controller selection */
264   PORT_CATEGORY_CLASS( 0x0f, 0x00, "Player 1 Controller" )
265   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 10 )
266   PORT_CATEGORY_ITEM( 0x01, "Joystick 6 Buttons", 11 )
267//  PORT_CATEGORY_ITEM( 0x02, "Sega Mouse", 12 )
268/* there exists both a 2 buttons version of the Mouse (Jpn ver, to be used with RPGs, it
269    can aslo be used as trackball) and a 3 buttons version (US ver, no trackball feats.) */
270//  PORT_CATEGORY_ITEM( 0x03, "Sega Menacer", 13 )
271//  PORT_CATEGORY_ITEM( 0x04, "Konami Justifier", 14 )
272//  PORT_CATEGORY_ITEM( 0x05, "Team Player (Sega Multitap)", 15 )
273//  PORT_CATEGORY_ITEM( 0x06, "4-Play (EA Multitap)", 16 )
274//  PORT_CATEGORY_ITEM( 0x07, "J-Cart", 17 )
275   PORT_CATEGORY_CLASS( 0xf0, 0x00, "Player 2 Controller" )
276   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 20 )
277   PORT_CATEGORY_ITEM( 0x10, "Joystick 6 Buttons", 21 )
278   PORT_CATEGORY_CLASS( 0xf00, 0x00, "Player 3 Controller (J-Cart)" )
279   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 30 )
280   PORT_CATEGORY_CLASS( 0xf000, 0x00, "Player 4 Controller (J-Cart)" )
281   PORT_CATEGORY_ITEM( 0x00, "Joystick 3 Buttons", 40 )
282
283   PORT_START("PAD1_3B")      /* Joypad 1 (3 button + start) NOT READ DIRECTLY */
284   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
285   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
286   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
287   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(10)
288   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("P1 B") PORT_CATEGORY(10)
289   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("P1 C") PORT_CATEGORY(10)
290   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("P1 A") PORT_CATEGORY(10)
291   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(1) PORT_CATEGORY(10)
292
293   PORT_START("PAD2_3B")      /* Joypad 2 (3 button + start) NOT READ DIRECTLY */
294   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
295   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
296   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
297   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(20)
298   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2) PORT_NAME("P2 B") PORT_CATEGORY(20)
299   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2) PORT_NAME("P2 C") PORT_CATEGORY(20)
300   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P2 A") PORT_CATEGORY(20)
301   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(2) PORT_CATEGORY(20)
302
303   PORT_START("JCART3_3B")      /* Joypad 3 on J-Cart (3 button + start) */
304   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
305   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
306   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
307   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(3) PORT_CATEGORY(30)
308   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(3) PORT_NAME("P3 B") PORT_CATEGORY(30)
309   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(3) PORT_NAME("P3 C") PORT_CATEGORY(30)
310   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(3) PORT_NAME("P3 A") PORT_CATEGORY(30)
311   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(3) PORT_CATEGORY(30)
312
313   PORT_START("JCART4_3B")      /* Joypad 4 on J-Cart (3 button + start) */
314   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
315   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
316   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
317   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(4) PORT_CATEGORY(40)
318   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(4) PORT_NAME("P4 B") PORT_CATEGORY(40)
319   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(4) PORT_NAME("P4 C") PORT_CATEGORY(40)
320   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(4) PORT_NAME("P4 A") PORT_CATEGORY(40)
321   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(4) PORT_CATEGORY(40)
322
323   PORT_START("PAD1_6B")      /* Joypad 1 (6 button + start + mode) NOT READ DIRECTLY */
324   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
325   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
326   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
327   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1) PORT_CATEGORY(11)
328   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("P1 B") PORT_CATEGORY(11)
329   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("P1 C") PORT_CATEGORY(11)
330   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("P1 A") PORT_CATEGORY(11)
331   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(1) PORT_CATEGORY(11)
332
333   PORT_START("EXTRA1")   /* Extra buttons for Joypad 1 (6 button + start + mode) NOT READ DIRECTLY */
334   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(1) PORT_NAME("P1 Z") PORT_CATEGORY(11)
335   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(1) PORT_NAME("P1 Y") PORT_CATEGORY(11)
336   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(1) PORT_NAME("P1 X") PORT_CATEGORY(11)
337   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(1) PORT_NAME("P1 Mode") PORT_CATEGORY(11)
338
339   PORT_START("PAD2_6B")      /* Joypad 2 (6 button + start + mode) NOT READ DIRECTLY */
340   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
341   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
342   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
343   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(2) PORT_CATEGORY(21)
344   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2) PORT_NAME("P2 B") PORT_CATEGORY(21)
345   PORT_BIT( 0x0020, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2) PORT_NAME("P2 C") PORT_CATEGORY(21)
346   PORT_BIT( 0x0040, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P2 A") PORT_CATEGORY(21)
347   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_START ) PORT_PLAYER(2) PORT_CATEGORY(21)
348
349   PORT_START("EXTRA2")   /* Extra buttons for Joypad 2 (6 button + start + mode) NOT READ DIRECTLY */
350   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(2) PORT_NAME("P2 Z") PORT_CATEGORY(21)
351   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(2) PORT_NAME("P2 Y") PORT_CATEGORY(21)
352   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(2) PORT_NAME("P2 X") PORT_CATEGORY(21)
353   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(2) PORT_NAME("P2 Mode") PORT_CATEGORY(21)
354
355   PORT_START("RESET")      /* Buttons on Genesis Console */
356   PORT_BIT( 0x0001, IP_ACTIVE_HIGH, IPT_SERVICE1 ) PORT_NAME("Reset Button") PORT_IMPULSE(1) // reset, resets 68k (and..?)
357INPUT_PORTS_END
358
359
360/* MegaDrive inputs + Fake Region Selection */
361/* We need this as long as we only have the US version of the SVP add-on, otherwise we could not play
362   Non-US Virtua Racing versions. It is also handy to develop add-ons emulation without adding each
363   region variants, while they do not even work. Once emulation is working this must disappear.  */
364static INPUT_PORTS_START( md_sel )
365   PORT_INCLUDE( md )
366
367   PORT_START("REGION")
368   /* Region setting for Console */
369   PORT_CONFNAME( 0x000f, 0x0000, DEF_STR( Region ) )
370   PORT_CONFSETTING(      0x0000, "Use Default Choice" )
371   PORT_CONFSETTING(      0x0001, "US (NTSC, 60fps)" )
372   PORT_CONFSETTING(      0x0002, "Japan (NTSC, 60fps)" )
373   PORT_CONFSETTING(      0x0003, "Europe (PAL, 50fps)" )
374INPUT_PORTS_END
375
376
377/*************************************
378 *
379 *  Machine driver
380 *
381 *************************************/
382
383static MACHINE_START( ms_megadriv )
384{
385   mess_init_6buttons_pad(machine);
386}
387
388static MACHINE_RESET( ms_megadriv )
389{
390   MACHINE_RESET_CALL( megadriv );
391   MACHINE_RESET_CALL( md_mappers );
392}
393
394static MACHINE_CONFIG_DERIVED( ms_megadriv, megadriv )
395
396   MCFG_MACHINE_START( ms_megadriv )
397   MCFG_MACHINE_RESET( ms_megadriv )
398
399   MCFG_FRAGMENT_ADD( genesis_cartslot )
400MACHINE_CONFIG_END
401
402static MACHINE_CONFIG_DERIVED( ms_megadpal, megadpal )
403
404   MCFG_MACHINE_START( ms_megadriv )
405   MCFG_MACHINE_RESET( ms_megadriv )
406
407   MCFG_FRAGMENT_ADD( genesis_cartslot )
408MACHINE_CONFIG_END
409
410static MACHINE_CONFIG_DERIVED( ms_megdsvp, megdsvp )
411
412   MCFG_MACHINE_START( ms_megadriv )
413   MCFG_MACHINE_RESET( ms_megadriv )
414
415   MCFG_FRAGMENT_ADD( genesis_cartslot )
416MACHINE_CONFIG_END
417
418
419
420/*************************************
421 *
422 *  ROM definition(s)
423 *
424 *************************************/
425
426
427/* we don't use the bios rom (it's not needed and only provides security on early models) */
428
429ROM_START(genesis)
430   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
431   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
432ROM_END
433
434ROM_START(gensvp)
435   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
436   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
437ROM_END
438
439ROM_START(megadriv)
440   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
441   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
442ROM_END
443
444ROM_START(megadrij)
445   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
446   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
447ROM_END
448
449
450/*************************************
451 *
452 *  Driver initialization
453 *
454 *************************************/
455
456static DRIVER_INIT( mess_md_common )
457{
458   megadrive_io_read_data_port_ptr = mess_md_io_read_data_port;
459   megadrive_io_write_data_port_ptr = mess_md_io_write_data_port;
460}
461
462static DRIVER_INIT( genesis )
463{
464   DRIVER_INIT_CALL(megadriv);
465   DRIVER_INIT_CALL(mess_md_common);
466}
467
468static DRIVER_INIT( gensvp )
469{
470   DRIVER_INIT_CALL(megadriv);
471   DRIVER_INIT_CALL(mess_md_common);
472}
473
474static DRIVER_INIT( md_eur )
475{
476   DRIVER_INIT_CALL(megadrie);
477   DRIVER_INIT_CALL(mess_md_common);
478}
479
480static DRIVER_INIT( md_jpn )
481{
482   DRIVER_INIT_CALL(megadrij);
483   DRIVER_INIT_CALL(mess_md_common);
484}
485
486/****************************************** SegaCD & 32X emulation ****************************************/
487
488/* Very preliminary skeleton code from David Haywood, borrowed for MESS by Fabio Priuli */
489/* These are only included to document BIOS informations currently available */
490
491static DRIVER_INIT( mess_32x )
492{
493   DRIVER_INIT_CALL(_32x);
494   DRIVER_INIT_CALL(mess_md_common);
495}
496
497static MACHINE_CONFIG_DERIVED( ms_32x, genesis_32x )
498
499   MCFG_FRAGMENT_ADD( _32x_cartslot )
500MACHINE_CONFIG_END
501
502
503ROM_START( 32x )
504   ROM_REGION16_BE( 0x400000, "gamecart", ROMREGION_ERASE00 ) /* 68000 Code */
505//  ROM_CART_LOAD("cart", 0x000000, 0x400000, ROM_NOMIRROR)
506
507   ROM_REGION32_BE( 0x400000, "gamecart_sh2", ROMREGION_ERASE00 ) /* Copy for the SH2 */
508//  ROM_CART_LOAD("cart", 0x000000, 0x400000, ROM_NOMIRROR)
509
510   ROM_REGION16_BE( 0x400000, "32x_68k_bios", 0 ) /* 68000 Code */
511   ROM_LOAD( "32x_g_bios.bin", 0x000000,  0x000100, CRC(5c12eae8) SHA1(dbebd76a448447cb6e524ac3cb0fd19fc065d944) )
512
513   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
514   // temp, rom should only be visible here when one of the regs is set, tempo needs it
515//  ROM_CART_LOAD("cart", 0x000000, 0x400000, ROM_NOMIRROR)
516   ROM_COPY( "32x_68k_bios", 0x0, 0x0, 0x100)
517
518   ROM_REGION( 0x400000, "32x_master_sh2", 0 ) /* SH2 Code */
519   ROM_SYSTEM_BIOS( 0, "retail", "Mars Version 1.0 (retail)" )
520   ROMX_LOAD( "32x_m_bios.bin", 0x000000,  0x000800, CRC(dd9c46b8) SHA1(1e5b0b2441a4979b6966d942b20cc76c413b8c5e), ROM_BIOS(1) )
521   ROM_SYSTEM_BIOS( 1, "sdk", "Mars Version 1.0 (early sdk)" )
522   ROMX_LOAD( "32x_m_bios_sdk.bin", 0x000000,  0x000800, BAD_DUMP CRC(c7102c53) SHA1(ed73a47f186b373b8eff765f84ef26c3d9ef6cb0), ROM_BIOS(2) )
523
524   ROM_REGION( 0x400000, "32x_slave_sh2", 0 ) /* SH2 Code */
525   ROM_LOAD( "32x_s_bios.bin", 0x000000,  0x000400, CRC(bfda1fe5) SHA1(4103668c1bbd66c5e24558e73d4f3f92061a109a) )
526ROM_END
527
528/* We need proper names for most of these BIOS ROMs! */
529ROM_START( segacd )
530   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
531   /* v1.10 */
532   ROM_LOAD( "segacd_model1_bios_1_10_u.bin", 0x000000,  0x020000, CRC(c6d10268) SHA1(f4f315adcef9b8feb0364c21ab7f0eaf5457f3ed) )
533ROM_END
534
535ROM_START( megacd )
536   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
537   /* v1.00, confirmed good dump */
538   ROM_LOAD( "megacd_model1_bios_1_00_e.bin", 0x000000,  0x020000, CRC(529ac15a) SHA1(f891e0ea651e2232af0c5c4cb46a0cae2ee8f356) )
539ROM_END
540
541ROM_START( megacdj )
542   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
543   ROM_SYSTEM_BIOS(0, "v100s", "v1.00S") /* also used for Asia PAL - not handled yet */
544   ROMX_LOAD( "megacd_model1_bios_1_00s_j.bin", 0x000000,  0x020000, CRC(550f30bb) SHA1(e4193c6ae44c3cea002707d2a88f1fbcced664de), ROM_BIOS(1) )
545   ROM_SYSTEM_BIOS(1, "v100p", "v1.00P")
546   ROMX_LOAD( "megacd_model1_bios_1_00p_j.bin", 0x000000,  0x020000, CRC(9d2da8f2) SHA1(4846f448160059a7da0215a5df12ca160f26dd69), ROM_BIOS(2) )
547ROM_END
548
549ROM_START( segacd2 )
550   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
551   ROM_SYSTEM_BIOS(0, "v211x", "Model 2 v2.11X")
552   ROMX_LOAD( "mpr-15764-t.bin", 0x000000,  0x020000, CRC(2e49d72c) SHA1(328a3228c29fba244b9db2055adc1ec4f7a87e6b), ROM_BIOS(1) )
553   ROM_SYSTEM_BIOS(1, "v200", "Model 2 v2.00") /* verified dump */
554   ROMX_LOAD( "us_scd2_930314.bin", 0x000000,  0x020000, CRC(8af65f58) SHA1(5a8c4b91d3034c1448aac4b5dc9a6484fce51636), ROM_BIOS(2) )
555   /* this is reportedly a bad dump, it has many differences from the verified dump and does not boot in Kega */
556   /* ROMX_LOAD( "segacd_model2_bios_2_00_u.bin", 0x000000,  0x020000, CRC(340b4be4) SHA1(bd3ee0c8ab732468748bf98953603ce772612704), ROM_BIOS(2) ) */
557   ROM_SYSTEM_BIOS(2, "v200w", "Model 2 v2.00W")
558   ROMX_LOAD( "segacd_model2_bios_2_00w_u.bin", 0x000000,  0x020000, CRC(9f6f6276) SHA1(5adb6c3af218c60868e6b723ec47e36bbdf5e6f0), ROM_BIOS(3) )
559ROM_END
560
561ROM_START( megacd2 )
562   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
563   ROM_SYSTEM_BIOS(0, "v200w", "v2.00W")   // confirmed good dump
564   ROMX_LOAD( "mpr-15512a.bin", 0x000000,  0x020000, CRC(2a4b82b5) SHA1(45134ef8655b9d06b130726786efe2f8b1d430a3), ROM_BIOS(1) )
565   ROM_SYSTEM_BIOS(1, "v200", "v2.00")
566   ROMX_LOAD( "mpr-15512-t.bin", 0x000000,  0x020000, CRC(62108fff) SHA1(cfcf092e0a70779fc5912da0fbd154838df997da), ROM_BIOS(2) )
567ROM_END
568
569ROM_START( megacd2j )
570   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
571   ROM_SYSTEM_BIOS(0, "v200c", "v2.00C")
572   ROMX_LOAD( "mpr-15398.bin", 0x000000,  0x020000, CRC(ba7bf31d) SHA1(762d20ebb85b980c17c53f928c002d747920a281), ROM_BIOS(1) )
573ROM_END
574
575ROM_START( laseract )
576   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
577   ROM_SYSTEM_BIOS(0, "v104", "v1.04")
578   ROMX_LOAD( "laseractive_bios_1_04_u.bin", 0x000000,  0x020000, CRC(50cd3d23) SHA1(aa811861f8874775075bd3f53008c8aaf59b07db), ROM_BIOS(1) )
579   ROM_SYSTEM_BIOS(1, "v102", "v1.02")
580   ROMX_LOAD( "laseractive_bios_1_02_u.bin", 0x000000,  0x020000, CRC(3b10cf41) SHA1(8af162223bb12fc19b414f126022910372790103), ROM_BIOS(2) )
581ROM_END
582
583ROM_START( laseractj )
584   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
585   /* v1.02 */
586   ROM_LOAD( "laseractive_bios_1_02_j.bin", 0x000000,  0x020000, CRC(00eedb3a) SHA1(26237b333db4a4c6770297fa5e655ea95840d5d9) )
587ROM_END
588
589ROM_START( xeye )
590   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
591   /* v2.00 (US), confirmed good with a chip dump */
592   ROM_LOAD( "g304.bin", 0x000000,  0x020000, CRC(290f8e33) SHA1(651f14d5a5e0ecb974a60c0f43b1d2006323fb09) )
593ROM_END
594
595ROM_START( wmega )
596   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
597   /* v1.00 (Japan NTSC) Sega BIOS, chip-dumped */
598   ROM_LOAD( "g301.bin", 0x000000,  0x020000, CRC(d21fe71d) SHA1(3fc9358072f74bd24e3e297ea11b2bf15a7af891) )
599ROM_END
600
601ROM_START( wmegam2 )
602   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
603   /* v2.00 */
604   ROM_LOAD( "wondermega_m2_bios_2_00_j.bin", 0x000000,  0x020000, CRC(2b19972f) SHA1(b3f32e409bd5508c89ed8be33d41a58d791d0e5d) )
605ROM_END
606
607ROM_START( cdx )
608   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
609   /* v2.21X */
610   ROM_LOAD( "segacdx_bios_2_21_u.bin", 0x000000,  0x020000, CRC(d48c44b5) SHA1(2b125c0545afa089b617f2558e686ea723bdc06e) )
611ROM_END
612
613ROM_START( multmega )
614   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
615   /* v2.21X */
616   ROM_LOAD( "opr-16140.bin", 0x000000,  0x020000, CRC(aacb851e) SHA1(75548ac9aaa6e81224499f9a1403b2b42433f5b7) )
617   /* the below was marked "confirmed good dump", but 0x72 and 0x73 are 0xFF, indicating a bad dump made from memory */
618   /* ROM_LOAD( "multimega_bios_2_21_e.bin", 0x000000,  0x020000, CRC(34d3cce1) SHA1(73fc9c014ad803e9e7d8076b3642a8a5224b3e51) ) */
619ROM_END
620
621/* some games use the 32x and SegaCD together to give better quality FMV */
622ROM_START( 32x_scd )
623   ROM_REGION16_BE( 0x400000, "maincpu", ROMREGION_ERASE00 )
624
625   ROM_REGION16_BE( 0x400000, "gamecart", 0 ) /* 68000 Code */
626   ROM_LOAD( "mpr-15764-t.bin", 0x000000,  0x020000, CRC(2e49d72c) SHA1(328a3228c29fba244b9db2055adc1ec4f7a87e6b) )
627
628   ROM_REGION32_BE( 0x400000, "gamecart_sh2", 0 ) /* Copy for the SH2 */
629   ROM_COPY( "gamecart", 0x0, 0x0, 0x400000)
630
631   ROM_REGION16_BE( 0x400000, "32x_68k_bios", 0 ) /* 68000 Code */
632   ROM_LOAD( "32x_g_bios.bin", 0x000000,  0x000100, CRC(5c12eae8) SHA1(dbebd76a448447cb6e524ac3cb0fd19fc065d944) )
633
634   ROM_REGION( 0x400000, "32x_master_sh2", 0 ) /* SH2 Code */
635   ROM_LOAD( "32x_m_bios.bin", 0x000000,  0x000800, CRC(dd9c46b8) SHA1(1e5b0b2441a4979b6966d942b20cc76c413b8c5e) )
636
637   ROM_REGION( 0x400000, "32x_slave_sh2", 0 ) /* SH2 Code */
638   ROM_LOAD( "32x_s_bios.bin", 0x000000,  0x000400, CRC(bfda1fe5) SHA1(4103668c1bbd66c5e24558e73d4f3f92061a109a) )
639ROM_END
640
641
642/****************************************** PICO emulation ****************************************/
643
644/*
645   Pico Implementation By ElBarto (Emmanuel Vadot, elbarto@megadrive.org)
646   Still missing the PCM custom chip
647   Some game will not boot due to this
648
649 Pico Info from Notaz (http://notaz.gp2x.de/docs/picodoc.txt)
650
651 addr   acc   description
652-------+-----+------------
653800001  byte  Version register.
654              ?vv? ????, where v can be:
655                00 - hardware is for Japan
656                01 - European version
657                10 - USA version
658                11 - ?
659800003  byte  Buttons, 0 for pressed, 1 for released:
660                bit 0: UP (white)
661                bit 1: DOWN (orange)
662                bit 2: LEFT (blue)
663                bit 3: RIGHT (green)
664                bit 4: red button
665                bit 5: unused?
666                bit 6: unused?
667                bit 7: pen button
668800005  byte  Most significant byte of pen x coordinate.
669800007  byte  Least significant byte of pen x coordinate.
670800009  byte  Most significant byte of pen y coordinate.
67180000b  byte  Least significant byte of pen y coordinate.
67280000d  byte  Page register. One bit means one uncovered page sensor.
673                00 - storyware closed
674                01, 03, 07, 0f, 1f, 3f - pages 1-6
675                either page 5 or page 6 is often unused.
676800010  word  PCM data register.
677        r/w   read returns free bytes left in PCM FIFO buffer
678              writes write data to buffer.
679800012  word  PCM control register.
680        r/w   For writes, it has following possible meanings:
681              ?p?? ???? ???? ?rrr
682                p - set to enable playback?
683                r - sample rate / PCM data type?
684                  0: 8kHz 4bit ADPCM?
685                  1-7: 16kHz variants?
686              For reads, if bit 15 is cleared, it means PCM is 'busy' or
687              something like that, as games sometimes wait for it to become 1.
688800019  byte  Games write 'S'
68980001b  byte  Games write 'E'
69080001d  byte  Games write 'G'
69180001f  byte  Games write 'A'
692
693*/
694
695#define PICO_PENX   1
696#define PICO_PENY   2
697
698static UINT16 pico_read_penpos(running_machine *machine, int pen)
699{
700  UINT16 penpos = 0;
701
702  switch (pen)
703    {
704    case PICO_PENX:
705      penpos = input_port_read_safe(machine, "PENX", 0);
706      penpos |= 0x6;
707      penpos = penpos * 320 / 255;
708      penpos += 0x3d;
709      break;
710    case PICO_PENY:
711      penpos = input_port_read_safe(machine, "PENY", 0);
712      penpos |= 0x6;
713      penpos = penpos * 251 / 255;
714      penpos += 0x1fc;
715      break;
716    }
717  return penpos;
718}
719
720static READ16_HANDLER( pico_68k_io_read )
721{
722   UINT8 retdata;
723   static UINT8 page_register = 0;
724
725
726   retdata = 0;
727
728   switch (offset)
729     {
730     case 0:   /* Version register ?XX?????? where XX is 00 for japan, 01 for europe and 10 for USA*/
731      retdata = (megadrive_region_export << 6) | (megadrive_region_pal << 5);
732       break;
733     case 1:
734       retdata = input_port_read_safe(space->machine, "PAD", 0);
735       break;
736
737       /*
738           Still notes from notaz for the pen :
739
740           The pen can be used to 'draw' either on the drawing pad or on the storyware
741           itself. Both storyware and drawing pad are mapped on single virtual plane, where
742           coordinates range:
743
744           x: 0x03c - 0x17c
745           y: 0x1fc - 0x2f7 (drawing pad)
746              0x2f8 - 0x3f3 (storyware)
747         */
748     case 2:
749       retdata = pico_read_penpos(space->machine, PICO_PENX) >> 8;
750       break;
751     case 3:
752       retdata = pico_read_penpos(space->machine, PICO_PENX) & 0x00ff;
753       break;
754     case 4:
755       retdata = pico_read_penpos(space->machine, PICO_PENY) >> 8;
756       break;
757     case 5:
758       retdata = pico_read_penpos(space->machine, PICO_PENY) & 0x00ff;
759       break;
760     case 6:
761       /* Page register :
762           00 - storyware closed
763           01, 03, 07, 0f, 1f, 3f - pages 1-6
764           either page 5 or page 6 is often unused.
765        */
766       {
767         UINT8 tmp;
768
769         tmp = input_port_read_safe(space->machine, "PAGE", 0);
770         if (tmp == 2 && page_register != 0x3f)
771      {
772        page_register <<= 1;
773        page_register |= 1;
774      }
775         if (tmp == 1 && page_register != 0x00)
776      page_register >>= 1;
777         retdata = page_register;
778         break;
779       }
780     case 7:
781       /* Returns free bytes left in the PCM FIFO buffer */
782       retdata = 0x00;
783       break;
784     case 8:
785       /*
786           For reads, if bit 15 is cleared, it means PCM is 'busy' or
787           something like that, as games sometimes wait for it to become 1.
788        */
789       retdata = 0x00;
790     }
791   return retdata | retdata <<8;
792}
793
794static WRITE16_HANDLER( pico_68k_io_write )
795{
796   switch (offset)
797     {
798     }
799}
800
801static ADDRESS_MAP_START( _pico_mem, ADDRESS_SPACE_PROGRAM, 16 )
802   AM_RANGE(0x000000, 0x3fffff) AM_ROM
803
804   AM_RANGE(0x800000, 0x80001f) AM_READWRITE(pico_68k_io_read, pico_68k_io_write)
805
806   AM_RANGE(0xc00000, 0xc0001f) AM_READWRITE(megadriv_vdp_r, megadriv_vdp_w)
807   AM_RANGE(0xe00000, 0xe0ffff) AM_RAM AM_MIRROR(0x1f0000) AM_BASE(&megadrive_ram)
808ADDRESS_MAP_END
809
810
811static INPUT_PORTS_START( pico )
812   PORT_START("PAD")
813   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_8WAY PORT_PLAYER(1)
814   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_8WAY PORT_PLAYER(1)
815   PORT_BIT( 0x0004, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_8WAY PORT_PLAYER(1)
816   PORT_BIT( 0x0008, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_8WAY PORT_PLAYER(1)
817   PORT_BIT( 0x0010, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("Red Button")
818   PORT_BIT( 0x0080, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("Pen Button")
819
820   PORT_START("PAGE")
821   PORT_BIT( 0x0001, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("Increment Page")
822   PORT_BIT( 0x0002, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(1) PORT_NAME("Decrement Page")
823
824   PORT_START("PENX")
825   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(30) PORT_KEYDELTA(10) PORT_MINMAX(0, 255) PORT_CATEGORY(5) PORT_PLAYER(1) PORT_NAME("PEN X")
826
827   PORT_START("PENY")
828   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_SENSITIVITY(30) PORT_KEYDELTA(10) PORT_MINMAX(0,255 ) PORT_CATEGORY(5) PORT_PLAYER(1) PORT_NAME("PEN Y")
829
830   PORT_START("REGION")
831   /* Region setting for Console */
832   PORT_DIPNAME( 0x000f, 0x0000, DEF_STR( Region ) )
833   PORT_DIPSETTING(      0x0000, "Use HazeMD Default Choice" )
834   PORT_DIPSETTING(      0x0001, "US (NTSC, 60fps)" )
835   PORT_DIPSETTING(      0x0002, "JAPAN (NTSC, 60fps)" )
836   PORT_DIPSETTING(      0x0003, "EUROPE (PAL, 50fps)" )
837INPUT_PORTS_END
838
839
840static MACHINE_CONFIG_DERIVED( pico, megadriv )
841
842   MCFG_CPU_MODIFY("maincpu")
843   MCFG_CPU_PROGRAM_MAP(_pico_mem)
844
845   MCFG_DEVICE_REMOVE("genesis_snd_z80")
846
847   MCFG_MACHINE_RESET( ms_megadriv )
848
849   MCFG_FRAGMENT_ADD( pico_cartslot )
850MACHINE_CONFIG_END
851
852static MACHINE_CONFIG_DERIVED( picopal, megadpal )
853
854   MCFG_CPU_MODIFY("maincpu")
855   MCFG_CPU_PROGRAM_MAP(_pico_mem)
856
857   MCFG_DEVICE_REMOVE("genesis_snd_z80")
858
859   MCFG_MACHINE_RESET( ms_megadriv )
860
861   MCFG_FRAGMENT_ADD( pico_cartslot )
862MACHINE_CONFIG_END
863
864
865
866ROM_START( pico )
867   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
868   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
869ROM_END
870
871ROM_START( picou )
872   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
873   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
874ROM_END
875
876ROM_START( picoj )
877   ROM_REGION(0x1415000, "maincpu", ROMREGION_ERASEFF)
878   ROM_REGION( 0x10000, "soundcpu", ROMREGION_ERASEFF)
879ROM_END
880
881/***************************************************************************
882
883  Game driver(s)
884
885***************************************************************************/
886
887/*    YEAR  NAME        PARENT     COMPAT  MACHINE          INPUT   INIT       COMPANY   FULLNAME */
888CONS( 1989, genesis,    0,         0,      ms_megadriv,     md,     genesis,   "Sega",   "Genesis (USA, NTSC)", 0)
889CONS( 1993, gensvp,     genesis,   0,      ms_megdsvp,      md_sel, gensvp,    "Sega",   "Genesis (USA, NTSC, w/SVP)", 0)
890CONS( 1990, megadriv,   genesis,   0,      ms_megadpal,     md,     md_eur,    "Sega",   "Mega Drive (Europe, PAL)", 0)
891CONS( 1988, megadrij,   genesis,   0,      ms_megadriv,     md,     md_jpn,    "Sega",   "Mega Drive (Japan, NTSC)", 0)
892CONS( 1994, pico,       0,         0,      picopal,         pico,   md_eur,    "Sega",   "Pico (Europe, PAL)", 0)
893CONS( 1994, picou,      pico,      0,      pico,            pico,   genesis,   "Sega",   "Pico (USA, NTSC)", 0)
894CONS( 1993, picoj,      pico,      0,      pico,            pico,   md_jpn,    "Sega",   "Pico (Japan, NTSC)", 0)
895
896/* Not Working */
897CONS( 1994, 32x,        0,         0,      ms_32x,          md_sel, mess_32x,  "Sega",   "32X", GAME_NOT_WORKING )
898CONS( 1992, segacd,     0,         0,      genesis_scd,     md,     genesis,   "Sega",   "Sega CD (USA, NTSC)", GAME_NOT_WORKING )
899CONS( 1993, megacd,     segacd,    0,      genesis_scd,     md,     md_eur,    "Sega",   "Mega-CD (Europe, PAL)", GAME_NOT_WORKING )
900CONS( 1991, megacdj,    segacd,    0,      genesis_scd,     md,     md_jpn,    "Sega",   "Mega-CD (Japan, NTSC)", GAME_NOT_WORKING )
901CONS( 1993, segacd2,    0,         0,      genesis_scd,     md,     genesis,   "Sega",   "Sega CD 2 (USA, NTSC)", GAME_NOT_WORKING )
902CONS( 1993, megacd2,    segacd2,   0,      genesis_scd,     md,     md_eur,    "Sega",   "Mega-CD 2 (Europe, PAL)", GAME_NOT_WORKING )
903CONS( 1993, megacd2j,   segacd2,   0,      genesis_scd,     md,     md_jpn,    "Sega",   "Mega-CD 2 (Japan, NTSC)", GAME_NOT_WORKING )
904CONS( 1993, laseract,   0,         0,      genesis_scd,     md,     genesis,   "Pioneer","LaserActive (USA, NTSC)", GAME_NOT_WORKING )
905CONS( 1993, laseractj,  laseract,  0,      genesis_scd,     md,     md_jpn,    "Pioneer","LaserActive (Japan, NTSC)", GAME_NOT_WORKING )
906CONS( 1993, xeye,       0,         0,      genesis_scd,     md,     genesis,   "JVC",    "X'eye (USA, NTSC)", GAME_NOT_WORKING )
907CONS( 1992, wmega,      xeye,      0,      genesis_scd,     md,     md_jpn,    "Sega",   "Wondermega (Japan, NTSC)", GAME_NOT_WORKING )
908CONS( 1993, wmegam2,    xeye,      0,      genesis_scd,     md,     md_jpn,    "Victor", "Wondermega M2 (Japan, NTSC)", GAME_NOT_WORKING )
909CONS( 1994, cdx,        0,         0,      genesis_scd,     md,     genesis,   "Sega",   "CDX (USA, NTSC)", GAME_NOT_WORKING )
910CONS( 1994, multmega,   cdx,       0,      genesis_scd,     md,     md_eur,    "Sega",   "Multi-Mega (Europe, PAL)", GAME_NOT_WORKING )
911CONS( 1994, 32x_scd,    segacd,    0,      genesis_32x_scd, md_sel, mess_32x,  "Sega",   "Sega CD (USA, NTSC, w/32X)", GAME_NOT_WORKING )

Previous 15994 Revisions Next


© 1998-2018 The MESS Team