/***************************************************************************

Grobda (Namco 1984)

	Manuel Abadia (emumanu@hotmail.com)

Grobda Memory Map (preliminary)

CPU #1: (MAIN CPU)
0000-03ff   video RAM
0400-07ff   color RAM
0800-0f7f   RAM
0f80-0fff   sprite RAM 1 (sprite number and color)
1000-177f   RAM
1780-17ff   sprite RAM 2 (sprite x and y position)
1800-1f7f   RAM
1f80-1fff   sprite RAM 3 (sprite control)
2000		???
4040-43ff   shared RAM with CPU #2
4800-481f	custon IO chips
8000		watchdog reset
a000-bfff   ROM
c000-dfff   ROM
e000-ffff   ROM

CPU #2: (SOUND CPU)
0000-0040   sound registers
0040-03ff   shared RAM with CPU #1
2000-2001	IRQ enable?
2006-2007	sound enable?
e000-ffff   ROM

TODO:
	- cocktail mode

***************************************************************************/

#include "driver.h"
#include "vidhrdw/generic.h"

extern unsigned char *grobda_snd_sharedram;
extern unsigned char *grobda_spriteram;
extern unsigned char *grobda_customio_1, *grobda_customio_2;
extern unsigned char *mappy_soundregs;

/* memory functions */
int grobda_spriteram_r(int offset);
int grobda_snd_sharedram_r(int offset);
void grobda_spriteram_w(int offset,int data);
void grobda_snd_sharedram_w(int offset,int data);

/* custom IO chips functions */
void grobda_customio_w_1(int offset,int data);
void grobda_customio_w_2(int offset,int data);
int grobda_customio_r_1(int offset);
int grobda_customio_r_2(int offset);

int grobda_interrupt_1(void);
int grobda_interrupt_2(void);
void grobda_cpu2_enable_w(int offset,int data);
void grobda_interrupt_ctrl_1_w(int offset,int data);
void grobda_interrupt_ctrl_2_w(int offset,int data);
void grobda_init_machine(void);

/* video functions */
int grobda_vh_start( void );
void grobda_vh_stop( void );
void grobda_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom);
void grobda_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh);


static void grobda_DAC_w(int offset, int data)
{
	DAC_data_w(0, (data << 4) | data);
}
	/* CPU 1 (MAIN CPU) read addresses */
static struct MemoryReadAddress readmem_cpu1[] =
{
	{ 0x0000, 0x03ff, videoram_r, &videoram, &videoram_size },			/* video RAM */
	{ 0x0400, 0x07ff, colorram_r },										/* color RAM */
	{ 0x0800, 0x1fff, grobda_spriteram_r, &grobda_spriteram },			/* RAM & sprite RAM */
	{ 0x4040, 0x43ff, grobda_snd_sharedram_r, &grobda_snd_sharedram },  /* shared RAM with CPU #2 */
	{ 0x4800, 0x480f, grobda_customio_r_1, &grobda_customio_1 },		/* custom I/O chip #1 interface */
	{ 0x4810, 0x481f, grobda_customio_r_2, &grobda_customio_2 },		/* custom I/O chip #2 interface */
	{ 0xa000, 0xffff, MRA_ROM },										/* ROM */
	{ -1 }																/* end of table */
};

	/* CPU 1 (MAIN CPU) write addresses */
static struct MemoryWriteAddress writemem_cpu1[] =
{
	{ 0x0000, 0x03ff, videoram_w },				/* video RAM */
	{ 0x0400, 0x07ff, colorram_w, &colorram },  /* color RAM */
	{ 0x0800, 0x1fff, grobda_spriteram_w },		/* RAM & sprite RAM */
//	{ 0x2000, 0x2000, MWA_NOP },				/* ??? */
	{ 0x4040, 0x43ff, grobda_snd_sharedram_w }, /* shared RAM with CPU #2 */
	{ 0x4800, 0x480f, grobda_customio_w_1 },	/* custom I/O chip #1 interface */
	{ 0x4810, 0x481f, grobda_customio_w_2 },	/* custom I/O chip #2 interface */
	{ 0x5002, 0x5003, grobda_interrupt_ctrl_1_w },
//	{ 0x5008, 0x5009, MWA_NOP },				/* ??? */
	{ 0x500a, 0x500b, grobda_cpu2_enable_w },	/* sound CPU enable? */
	{ 0x8000, 0x8000, watchdog_reset_w },	 	/* watchdog reset */
	{ 0xa000, 0xffff, MWA_ROM },				/* ROM */
	{ -1 }										/* end of table */
};
	/* CPU 2 (SOUND CPU) read addresses */
static struct MemoryReadAddress readmem_cpu2[] =
{
	{ 0x0000, 0x003f, MRA_RAM },				/* sound registers */
	{ 0x0040, 0x03ff, grobda_snd_sharedram_r }, /* shared RAM with CPU #1 */
	{ 0xe000, 0xffff, MRA_ROM },				/* ROM */
	{ -1 }										/* end of table */
};

	/* CPU 2 (SOUND CPU) write addresses */
static struct MemoryWriteAddress writemem_cpu2[] =
{
	{ 0x0002, 0x0002, grobda_DAC_w },	 /* $12, $22 and $32 are DAC locations as well */
	{ 0x0000, 0x003f, mappy_sound_w, &mappy_soundregs },/* sound registers */
	{ 0x0040, 0x03ff, grobda_snd_sharedram_w },			/* shared RAM with the main CPU */
	{ 0x2000, 0x2001, grobda_interrupt_ctrl_2_w },
	{ 0x2006, 0x2007, mappy_sound_enable_w },			/* sound enable? */
	{ 0xe000, 0xffff, MWA_ROM },						/* ROM */
	{ -1 }												/* end of table */
};

/* The dipswitches and player inputs are not memory mapped, they are handled by an I/O chip. */
INPUT_PORTS_START( grobda_input_ports )
	PORT_START  /* DSW0 */
	PORT_DIPNAME( 0x07, 0x03, DEF_STR( Coin_A ) )
	PORT_DIPSETTING(    0x07, DEF_STR( 4C_1C ) )
	PORT_DIPSETTING(    0x06, DEF_STR( 3C_1C ) )
	PORT_DIPSETTING(    0x04, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x03, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x05, DEF_STR( 2C_3C ) )
	PORT_DIPSETTING(    0x02, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_3C ) )
	PORT_DIPSETTING(    0x01, DEF_STR( 1C_4C ) )
	PORT_DIPNAME( 0x18, 0x00, DEF_STR( Difficulty ) )
	PORT_DIPSETTING(    0x00, "a" )
	PORT_DIPSETTING(    0x08, "b" )
	PORT_DIPSETTING(    0x10, "c" )
	PORT_DIPSETTING(    0x18, "d" )
	PORT_DIPNAME( 0xe0, 0x60, DEF_STR( Coin_B ) )
	PORT_DIPSETTING(    0xe0, DEF_STR( 4C_1C ) )
	PORT_DIPSETTING(    0xc0, DEF_STR( 3C_1C ) )
	PORT_DIPSETTING(    0x80, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x60, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0xa0, DEF_STR( 2C_3C ) )
	PORT_DIPSETTING(    0x40, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_3C ) )
	PORT_DIPSETTING(    0x20, DEF_STR( 1C_4C ) )

	PORT_START  /* DSW1 */
	PORT_DIPNAME( 0x03, 0x00, DEF_STR( Lives ) )
	PORT_DIPSETTING(    0x01, "1" )
	PORT_DIPSETTING(    0x02, "2" )
	PORT_DIPSETTING(    0x00, "3" )
	PORT_DIPSETTING(    0x03, "5" )
	PORT_DIPNAME( 0x04, 0x04, "Select Mode" )
	PORT_DIPSETTING(    0x04, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_SERVICE( 0x08, IP_ACTIVE_HIGH )
	PORT_DIPNAME( 0xc0, 0xc0, DEF_STR( Bonus_Life ) )
	PORT_DIPSETTING(    0xc0, "10k 50k and every 50k" )
	PORT_DIPSETTING(    0x80, "10k 30k" )
	PORT_DIPSETTING(    0x00, "10k" )
	PORT_DIPSETTING(    0x40, "none" )

	PORT_START  /* IN0 */
	PORT_BIT_IMPULSE( 0x01, IP_ACTIVE_HIGH, IPT_START1, 1 )
	PORT_BIT_IMPULSE( 0x02, IP_ACTIVE_HIGH, IPT_START2, 1 )
	PORT_BIT_IMPULSE( 0x10, IP_ACTIVE_HIGH, IPT_COIN1, 1 )
	PORT_BIT_IMPULSE( 0x20, IP_ACTIVE_HIGH, IPT_COIN2, 1 )
	PORT_DIPNAME( 0x40, 0x00, DEF_STR( Demo_Sounds ) )
	PORT_DIPSETTING(    0x40, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x00, DEF_STR( On ) )
	PORT_DIPNAME( 0x80, 0x80, DEF_STR( Cabinet ) )
	PORT_DIPSETTING(    0x80, DEF_STR( Upright ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Cocktail ) )

	PORT_START  /* IN1 */
	PORT_BIT(   0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP    | IPF_8WAY  )
	PORT_BIT(   0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT | IPF_8WAY  )
	PORT_BIT(   0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN  | IPF_8WAY )
	PORT_BIT(   0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT  | IPF_8WAY )
	PORT_BIT(   0x10, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP    | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT(   0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT(   0x40, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN  | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT(   0x80, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT  | IPF_8WAY | IPF_COCKTAIL )

	PORT_START  /* IN2 */
	PORT_BIT_IMPULSE( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON1, 1 )
	PORT_BITX(  0x02, IP_ACTIVE_HIGH, IPT_BUTTON1, 0, IP_KEY_PREVIOUS, IP_JOY_PREVIOUS )
	PORT_BIT_IMPULSE( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1 | IPF_COCKTAIL, 1 )
	PORT_BITX(  0x08, IP_ACTIVE_HIGH, IPT_BUTTON1 | IPF_COCKTAIL, 0, IP_KEY_PREVIOUS, IP_JOY_PREVIOUS )
	PORT_BIT_IMPULSE( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON2, 1 )
	PORT_BITX(  0x20, IP_ACTIVE_HIGH, IPT_BUTTON2, 0, IP_KEY_PREVIOUS, IP_JOY_PREVIOUS )
INPUT_PORTS_END

static struct GfxLayout charlayout =
{
	8,8,            /* 8*8 characters */
	256,            /* 256 characters */
	2,				/* 2 bits per pixel */
	{ 0, 4 },		/* the bitplanes are packed into one byte */
	{ 8*8+0, 8*8+1, 8*8+2, 8*8+3, 0, 1, 2, 3 },   /* bits are packed in groups of four */
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },   /* characters are rotated 90 degrees */
	16*8			/* every char takes 16 bytes */
};

static struct GfxLayout spritelayout =
{
	16,16,                                         /* 16*16 sprites */
	256,                                           /* 128 sprites */
	2,                                             /* 2 bits per pixel */
	{ 0, 4 },                                      /* the bitplanes are packed into one byte */
	{ 0, 1, 2, 3, 8*8, 8*8+1, 8*8+2, 8*8+3,
		16*8+0, 16*8+1, 16*8+2, 16*8+3, 24*8+0, 24*8+1, 24*8+2, 24*8+3 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
		32*8, 33*8, 34*8, 35*8, 36*8, 37*8, 38*8, 39*8 },
	64*8                                           /* every sprite takes 64 bytes */
};

static struct GfxDecodeInfo gfxdecodeinfo[] =
{
	{ 1, 0x0000, &charlayout,		0, 64 },
	{ 1, 0x1000, &spritelayout,	 64*4, 64 },
	{ -1 } /* end of table */
};

static struct namco_interface namco_interface =
{
	23920,	/* sample rate (approximate value) */
	8,		/* number of voices */
	100,	/* playback volume */
	4		/* memory region */
};

static struct DACinterface dac_interface =
{
	1,
	{ 55 }
};

static struct MachineDriver grobda_machine_driver =
{
	/* basic machine hardware  */
	{
		{
			CPU_M6809,			/* MAIN CPU */
			1500000,			/* 1.5 MHz? */
			0,
			readmem_cpu1,writemem_cpu1,0,0,
			grobda_interrupt_1,1
		},
		{
			CPU_M6809,			/* SOUND CPU */
			1500000,			/* 1.5 MHz? */
			2,
			readmem_cpu2,writemem_cpu2,0,0,
			grobda_interrupt_2,1
		},
	},
	60, DEFAULT_60HZ_VBLANK_DURATION,	/* frames per second, vblank duration */
	100,	/* a high value to ensure proper synchronization of the CPUs */
	grobda_init_machine,	/* init machine routine */

	/* video hardware */
	36*8, 28*8, { 0*8, 36*8-1, 0*8, 28*8-1 },
	gfxdecodeinfo,
	32,
	4*(64+64),
	grobda_vh_convert_color_prom,
	VIDEO_TYPE_RASTER,
	0,
	grobda_vh_start,
	grobda_vh_stop,
	grobda_vh_screenrefresh,

	/* sound hardware */
	0,0,0,0,
	{
		{
			SOUND_NAMCO,
			&namco_interface
		},
		{
			SOUND_DAC,
			&dac_interface
		}
	}
};

ROM_START( grobda_rom )
	ROM_REGION(0x10000)     /* 64k for code for the first CPU  */
	ROM_LOAD( "gr1-3.d1",  0xa000, 0x2000, 0x4ef4a7c1 )
	ROM_LOAD( "gr1-2.c1",  0xc000, 0x2000, 0x7dcc6e8e )
	ROM_LOAD( "gr1-1.b1",  0xe000, 0x2000, 0x32d42f22 )

	ROM_REGION_DISPOSE(0x5000)  /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "gr1-7.c3",  0x0000, 0x1000, 0x4ebfabfd )	/* characters */
	ROM_LOAD( "gr1-5.f3",  0x1000, 0x2000, 0xeed43487 )	/* sprites */
	ROM_LOAD( "gr1-6.e3",  0x3000, 0x2000, 0xcebb7362 )	/* sprites */

	ROM_REGION(0x10000)     /* 64k for the second CPU */
	ROM_LOAD( "gr1-4.k1",  0xe000, 0x2000, 0x3fe78c08 )

	ROM_REGION(0x0220)      /* color proms */
	ROM_LOAD( "82s123.4c", 0x0000, 0x0020, 0xc65efa77 )	/* palette */
	ROM_LOAD( "mb7052.4e", 0x0020, 0x0100, 0xa0f66911 )	/* characters */
	ROM_LOAD( "mb7052.3l", 0x0120, 0x0100, 0xf1f2c234 )	/* sprites */

	ROM_REGION(0x0100)      /* sound prom */
	ROM_LOAD( "mb7052.3m", 0x0000, 0x0100, 0x66eb1467 )
ROM_END

ROM_START( grobda2_rom )
	ROM_REGION(0x10000)     /* 64k for code for the first CPU  */
	ROM_LOAD( "gr1-3.d1",  0xa000, 0x2000, 0x4ef4a7c1 )
	ROM_LOAD( "gr2-2.a",   0xc000, 0x2000, 0xf93e82ae )
	ROM_LOAD( "gr1-1.b1",  0xe000, 0x2000, 0x32d42f22 )

	ROM_REGION_DISPOSE(0x5000)  /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "gr1-7.c3",  0x0000, 0x1000, 0x4ebfabfd )	/* characters */
	ROM_LOAD( "gr1-5.f3",  0x1000, 0x2000, 0xeed43487 )	/* sprites */
	ROM_LOAD( "gr1-6.e3",  0x3000, 0x2000, 0xcebb7362 )	/* sprites */

	ROM_REGION(0x10000)     /* 64k for the second CPU */
	ROM_LOAD( "gr1-4.k1",  0xe000, 0x2000, 0x3fe78c08 )

	ROM_REGION(0x0220)      /* color proms */
	ROM_LOAD( "82s123.4c", 0x0000, 0x0020, 0xc65efa77 )	/* palette */
	ROM_LOAD( "mb7052.4e", 0x0020, 0x0100, 0xa0f66911 )	/* characters */
	ROM_LOAD( "mb7052.3l", 0x0120, 0x0100, 0xf1f2c234 )	/* sprites */

	ROM_REGION(0x0100)      /* sound prom */
	ROM_LOAD( "mb7052.3m", 0x0000, 0x0100, 0x66eb1467 )
ROM_END

ROM_START( grobda3_rom )
	ROM_REGION(0x10000)     /* 64k for code for the first CPU  */
	ROM_LOAD( "gr2-3",     0xa000, 0x2000, 0x8e3a23be )
	ROM_LOAD( "gr2-2",     0xc000, 0x2000, 0x19ffa83d )
	ROM_LOAD( "gr2-1",     0xe000, 0x2000, 0x0089b13a )

	ROM_REGION_DISPOSE(0x5000)  /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "gr1-7.c3",  0x0000, 0x1000, 0x4ebfabfd )	/* characters */
	ROM_LOAD( "gr1-5.f3",  0x1000, 0x2000, 0xeed43487 )	/* sprites */
	ROM_LOAD( "gr1-6.e3",  0x3000, 0x2000, 0xcebb7362 )	/* sprites */

	ROM_REGION(0x10000)     /* 64k for the second CPU */
	ROM_LOAD( "gr1-4.k1",  0xe000, 0x2000, 0x3fe78c08 )

	ROM_REGION(0x0220)      /* color proms */
	ROM_LOAD( "82s123.4c", 0x0000, 0x0020, 0xc65efa77 )	/* palette */
	ROM_LOAD( "mb7052.4e", 0x0020, 0x0100, 0xa0f66911 )	/* characters */
	ROM_LOAD( "mb7052.3l", 0x0120, 0x0100, 0xf1f2c234 )	/* sprites */

	ROM_REGION(0x0100)      /* sound prom */
	ROM_LOAD( "mb7052.3m", 0x0000, 0x0100, 0x66eb1467 )
ROM_END


struct GameDriver grobda_driver =
{
	__FILE__,
	0,
	"grobda",
	"Grobda (set 1)",
	"1984",
	"Namco",
	"Manuel Abadia\n",
	0,
	&grobda_machine_driver,
	0,

	grobda_rom,
	0, 0,
	0,
	0,

	grobda_input_ports,

	PROM_MEMORY_REGION(3),0,0,
	ORIENTATION_ROTATE_90,

	0, 0
};

struct GameDriver grobda2_driver =
{
	__FILE__,
	&grobda_driver,
	"grobda2",
	"Grobda (set 2)",
	"1984",
	"Namco",
	"Manuel Abadia\n",
	0,
	&grobda_machine_driver,
	0,

	grobda2_rom,
	0, 0,
	0,
	0,

	grobda_input_ports,

	PROM_MEMORY_REGION(3),0,0,
	ORIENTATION_ROTATE_90,

	0, 0
};

struct GameDriver grobda3_driver =
{
	__FILE__,
	&grobda_driver,
	"grobda3",
	"Grobda (set 3)",
	"1984",
	"Namco",
	"Manuel Abadia\n",
	0,
	&grobda_machine_driver,
	0,

	grobda3_rom,
	0, 0,
	0,
	0,

	grobda_input_ports,

	PROM_MEMORY_REGION(3),0,0,
	ORIENTATION_ROTATE_90,

	0, 0
};
