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

Son Son memory map (preliminary)

MAIN CPU:

0000-0fff RAM
1000-13ff Video RAM
1400-17ff Color RAM
2020-207f Sprites
4000-ffff ROM

read:
3002      IN0
3003      IN1
3004      IN2
3005      DSW0
3006      DSW1
see the input_ports definition below for details on the input bits

write:
3000      horizontal scroll
3008      ? one of these two should be
3018      ? the watchdog reset
3010      command for the audio CPU
3019      signal for the audio CPU?


SOUND CPU:
0000-07ff RAM
e000-ffff ROM

read:
0001      !0 if there is a command waiting
0002      command from the main CPU

write:
2000      8910 #1 control
2001      8910 #1 write
4000      8910 #2 control
4001      8910 #2 write

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

#include "driver.h"
#include "vidhrdw/generic.h"
#include "sndhrdw/generic.h"
#include "sndhrdw/8910intf.h"

#include "M6809.h"
void sonson_init_machine(void)
{
	/* Set optimization flags for M6809 */
	m6809_Flags = M6809_FAST_S | M6809_FAST_U;
}

extern unsigned char *sonson_scrollx;

void sonson_vh_convert_color_prom(unsigned char *palette, unsigned char *colortable,const unsigned char *color_prom);
void sonson_vh_screenrefresh(struct osd_bitmap *bitmap);

int capcom_sh_start(void);
int capcom_sh_interrupt(void);



static struct MemoryReadAddress readmem[] =
{
	{ 0x0000, 0x17ff, MRA_RAM },
	{ 0x4000, 0xffff, MRA_ROM },
	{ 0x3002, 0x3002, input_port_0_r },	/* IN0 */
	{ 0x3003, 0x3003, input_port_1_r },	/* IN1 */
	{ 0x3004, 0x3004, input_port_2_r },	/* IN2 */
	{ 0x3005, 0x3005, input_port_3_r },	/* DSW0 */
	{ 0x3006, 0x3006, input_port_4_r },	/* DSW1 */
	{ -1 }	/* end of table */
};

static struct MemoryWriteAddress writemem[] =
{
	{ 0x0000, 0x0fff, MWA_RAM },
	{ 0x1000, 0x13ff, videoram_w, &videoram, &videoram_size },
	{ 0x1400, 0x17ff, colorram_w, &colorram },
	{ 0x2020, 0x207f, MWA_RAM, &spriteram, &spriteram_size },
	{ 0x3000, 0x3000, MWA_RAM, &sonson_scrollx },
{ 0x3008, 0x3008, MWA_NOP },
	{ 0x3010, 0x3010, sound_command_w },
{ 0x3008, 0x3019, MWA_NOP },
	{ 0x4000, 0xffff, MWA_ROM },
	{ -1 }	/* end of table */
};

static struct MemoryReadAddress sound_readmem[] =
{
	{ 0x0001, 0x0001, sound_pending_commands_r },
	{ 0x0002, 0x0002, sound_command_latch_r },
	{ 0x0000, 0x07ff, MRA_RAM },
	{ 0xe000, 0xffff, MRA_ROM },
	{ -1 }  /* end of table */
};

static struct MemoryWriteAddress sound_writemem[] =
{
	{ 0x0000, 0x07ff, MWA_RAM },
	{ 0x2000, 0x2000, AY8910_control_port_0_w },
	{ 0x2001, 0x2001, AY8910_write_port_0_w },
	{ 0x4000, 0x4000, AY8910_control_port_1_w },
	{ 0x4001, 0x4001, AY8910_write_port_1_w },
	{ 0xe000, 0xffff, MWA_ROM },
	{ -1 }  /* end of table */
};



INPUT_PORTS_START( input_ports )
	PORT_START	/* IN0 */
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON1 )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT | IPF_8WAY )
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT | IPF_8WAY )
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP | IPF_8WAY )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN | IPF_8WAY )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */

	PORT_START	/* IN1 */
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON1 | IPF_PLAYER2 )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT | IPF_8WAY | IPF_PLAYER2 )
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT | IPF_8WAY | IPF_PLAYER2 )
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP | IPF_8WAY | IPF_PLAYER2 )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN | IPF_8WAY | IPF_PLAYER2 )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */

	PORT_START	/* IN2 */
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_START1 )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_START2 )
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_COIN1 )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_COIN2 )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN )	/* probably unused */

	PORT_START	/* DSW0 */
	PORT_DIPNAME( 0x0f, 0x0f, "Coinage", IP_KEY_NONE )
	PORT_DIPSETTING(    0x02, "4 Coins/1 Credit" )
	PORT_DIPSETTING(    0x05, "3 Coins/1 Credit" )
	PORT_DIPSETTING(    0x08, "2 Coins/1 Credit" )
	PORT_DIPSETTING(    0x04, "3 Coins/2 Credits" )
	PORT_DIPSETTING(    0x01, "4 Coins/3 Credits" )
	PORT_DIPSETTING(    0x0f, "1 Coin/1 Credit" )
	PORT_DIPSETTING(    0x03, "3 Coins/4 Credits" )
	PORT_DIPSETTING(    0x07, "2 Coins/3 Credits" )
	PORT_DIPSETTING(    0x0e, "1 Coin/2 Credits" )
	PORT_DIPSETTING(    0x06, "2 Coins/5 Credits" )
	PORT_DIPSETTING(    0x0d, "1 Coin/3 Credits" )
	PORT_DIPSETTING(    0x0c, "1 Coin/4 Credits" )
	PORT_DIPSETTING(    0x0b, "1 Coin/5 Credits" )
	PORT_DIPSETTING(    0x0a, "1 Coin/6 Credits" )
	PORT_DIPSETTING(    0x09, "1 Coin/7 Credits" )
	PORT_DIPSETTING(    0x00, "Free Play" )
	PORT_DIPNAME( 0x10, 0x10, "Coinage affects", IP_KEY_NONE )
	PORT_DIPSETTING(    0x10, "Coin A" )
	PORT_DIPSETTING(    0x00, "Coin B" )
	PORT_DIPNAME( 0x20, 0x00, "Demo Sounds", IP_KEY_NONE )
	PORT_DIPSETTING(    0x20, "Off" )
	PORT_DIPSETTING(    0x00, "On" )
	PORT_BITX(    0x40, 0x40, IPT_DIPSWITCH_NAME | IPF_TOGGLE, "Service Mode", OSD_KEY_F2, IP_JOY_NONE, 0 )
	PORT_DIPSETTING(    0x40, "Off" )
	PORT_DIPSETTING(    0x00, "On" )
	PORT_DIPNAME( 0x80, 0x80, "unknown", IP_KEY_NONE )
	PORT_DIPSETTING(    0x80, "Off" )
	PORT_DIPSETTING(    0x00, "On" )

	PORT_START	/* DSW1 */
	PORT_DIPNAME( 0x03, 0x03, "Lives", IP_KEY_NONE )
	PORT_DIPSETTING(    0x03, "3" )
	PORT_DIPSETTING(    0x02, "4" )
	PORT_DIPSETTING(    0x01, "5" )
	PORT_DIPSETTING(    0x00, "7" )
	PORT_DIPNAME( 0x04, 0x00, "2 Players Game", IP_KEY_NONE )
	PORT_DIPSETTING(    0x04, "1 Credit" )
	PORT_DIPSETTING(    0x00, "2 Credits" )
	PORT_DIPNAME( 0x18, 0x08, "Bonus Life", IP_KEY_NONE )
	PORT_DIPSETTING(    0x08, "20000 80000 100000" )
	PORT_DIPSETTING(    0x00, "30000 90000 120000" )
	PORT_DIPSETTING(    0x18, "20000" )
	PORT_DIPSETTING(    0x10, "30000" )
	PORT_DIPNAME( 0x60, 0x60, "Difficulty", IP_KEY_NONE )
	PORT_DIPSETTING(    0x60, "Easy" )
	PORT_DIPSETTING(    0x40, "Medium" )
	PORT_DIPSETTING(    0x20, "Hard" )
	PORT_DIPSETTING(    0x00, "Hardest" )
	PORT_DIPNAME( 0x80, 0x80, "Freeze", IP_KEY_NONE )
	PORT_DIPSETTING(    0x80, "Off" )
	PORT_DIPSETTING(    0x00, "On" )
INPUT_PORTS_END



static struct GfxLayout charlayout =
{
	8,8,	/* 8*8 characters */
	1024,	/* 1024 characters */
	2,	/* 2 bits per pixel */
	{ 1024*8*8, 0 },	/* the two bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7 },	/* pretty straightforward layout */
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },
	8*8	/* every char takes 8 consecutive bytes */
};
static struct GfxLayout spritelayout =
{
	16,16,	/* 16*16 sprites */
	512,	/* 512 sprites */
	3,	/* 3 bits per pixel */
	{ 2*2048*8*8, 2048*8*8, 0 },	/* the two bitplanes are separated */
        { 8*16+7, 8*16+6, 8*16+5, 8*16+4, 8*16+3, 8*16+2, 8*16+1, 8*16+0,	/* pretty straightforward layout */
	   7, 6, 5, 4, 3, 2, 1, 0 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
            8*8, 9*8, 10*8, 11*8, 12*8, 13*8, 14*8, 15*8 },
	32*8	/* every sprite takes 32 consecutive bytes */
};


static struct GfxDecodeInfo gfxdecodeinfo[] =
{
	{ 1, 0x00000, &charlayout,      0, 64 },
	{ 1, 0x04000, &spritelayout, 64*4, 32 },
	{ -1 } /* end of array */
};



/* these are NOT the original color PROMs */
static unsigned char color_prom[] =
{
	/* 12: palette red/green component */
	0x00,0x99,0x80,0x32,0x32,0x60,0xBB,0x77,0x46,0x69,0x46,0x68,0xA0,0x9A,0x7C,0x00,
	0x00,0xCC,0x99,0x97,0x00,0x00,0x70,0xB0,0x00,0x09,0x0A,0x7D,0x70,0x80,0xA0,0x11,
	/* 13: palette blue component */
	0x00,0x09,0x00,0x07,0x09,0x0B,0x0B,0x07,0x00,0x04,0x06,0x08,0x0A,0x0A,0x07,0x0C,
	0x00,0x0C,0x09,0x0C,0x0A,0x07,0x0A,0x0D,0x0E,0x08,0x06,0x07,0x0E,0x00,0x00,0x01,
	/* B2: character lookup table */
	0x00,0x9B,0x80,0x30,0x30,0x6C,0xB0,0x70,0x40,0x6D,0x40,0x60,0xA0,0x9E,0x70,0x00,
	0x00,0xCF,0x90,0x90,0x00,0x00,0x70,0xB0,0x00,0x01,0x00,0x70,0x70,0x82,0xA0,0x10,
	0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x06,0x00,0x00,
	0x00,0x07,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x09,0x00,0x00,0x00,0x0A,0x00,0xD0,
	0xF0,0xF8,0xF9,0xFE,0xF0,0x7A,0xFB,0xFD,0xF0,0xF3,0x74,0xB5,0xF0,0xF7,0xF1,0xB6,
	0xB0,0xF3,0xF4,0xF2,0xF0,0x07,0x00,0x06,0x00,0x02,0x03,0x04,0x00,0x02,0x0A,0x0B,
	0x00,0x09,0x06,0x0E,0x00,0x01,0x06,0x07,0x00,0x07,0x01,0x06,0x00,0x02,0x0C,0x06,
	0x00,0x0C,0x0F,0x02,0x00,0x02,0x0B,0x06,0x00,0x02,0x0F,0xF6,0xF0,0xF2,0xFB,0xFD,
	0xF0,0x72,0xF1,0xF6,0xF0,0xF2,0xF9,0xFE,0xF0,0xF2,0xF4,0xFF,0xB0,0x72,0xF9,0xF6,
	0xD0,0x02,0x0F,0x06,0x00,0x0C,0x03,0x05,0x00,0x01,0x03,0x05,0x00,0x04,0x05,0x06,
	0x00,0x07,0x01,0x05,0x00,0x09,0x0E,0x0C,0x00,0x04,0x0F,0x03,0x00,0x04,0x0F,0x06,
	0x00,0x01,0x05,0x06,0x00,0x04,0x05,0x02,0x00,0x04,0x05,0x0C,0x00,0x0E,0x09,0x06,
	0x00,0x04,0x0F,0xD2,0xF0,0xF4,0xFF,0xF6,0xF0,0x7B,0xFD,0xF6,0xFC,0xFD,0x7E,0x70,
	0xF1,0xF2,0xF3,0xF4,0x75,0xF6,0xF7,0xF8,0xF9,0x0A,0x0B,0x0C,0x0D,0x0E,0x00,0x0F,
	0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x00,0x0F,0x01,
	0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x00,0x0F,0x01,0x02,
	/* B: sprite lookup table */
	0x0F,0x91,0x83,0x34,0x37,0x68,0xBB,0x70,0x4F,0x61,0x43,0x64,0xAC,0x96,0x7B,0x00,
	0x0F,0xC1,0x92,0x94,0x0C,0x08,0x7B,0xB0,0x0F,0x0C,0x04,0x7F,0x7F,0x8F,0xAF,0x10,
	0x0F,0x01,0x02,0x06,0x07,0x0E,0x0B,0x00,0x0F,0x01,0x02,0x08,0x07,0x0B,0x0F,0x00,
	0x0F,0x03,0x06,0x0A,0x0D,0x0E,0x0B,0x00,0x0F,0x01,0x02,0x08,0x07,0x0B,0x0E,0xDC,
	0xFF,0xF1,0xF2,0xF5,0xF8,0x7D,0xFE,0xF0,0xFF,0xF1,0x72,0xBF,0xF7,0xFE,0xFD,0xB0,
	0xBF,0xF1,0xF4,0xF7,0xF8,0x02,0x0C,0x00,0x0F,0x01,0x04,0x07,0x05,0x02,0x0C,0x00,
	0x0F,0x01,0x03,0x04,0x08,0x09,0x06,0x00,0x0F,0x0F,0x0F,0x04,0x08,0x05,0x0F,0x00,
	0x0F,0x05,0x06,0x07,0x0D,0x0E,0x00,0x0F,0x0F,0x01,0x03,0xF4,0xFC,0xFF,0xFB,0xF0,
	0xFF,0x7F,0xF8,0xF4,0xFD,0xFE,0xF0,0xFC,0xFF,0xFF,0xF8,0xF4,0xBD,0x7E,0xF0,0xFC,
	0xDF,0x01,0x06,0x07,0x08,0x0D,0x0C,0x0E,0x0F,0x01,0x03,0x05,0x0D,0x08,0x0E,0x00,
	0x0F,0x01,0x06,0x04,0x0D,0x02,0x00,0x0C,0x0F,0x01,0x08,0x07,0x0A,0x0B,0x00,0x0C,
	0x0F,0x01,0x03,0x0B,0x02,0x08,0x06,0x00,0x0F,0x01,0x04,0x0C,0x07,0x08,0x06,0x00,
	0x0F,0x01,0x0D,0xDE,0xF6,0xF7,0xF8,0xF0,0xFF,0x71,0xF2,0xF4,0xF7,0xF8,0x7C,0x70,
	0xFF,0xF1,0xF2,0xF3,0x7A,0xFB,0xF0,0xF6,0xFF,0x01,0x02,0x03,0x04,0x05,0x00,0x06,
	0x0F,0x01,0x02,0x03,0x0D,0x0E,0x00,0x06,0x0F,0x01,0x0F,0x0E,0x07,0x0A,0x0B,0x00,
	0x0F,0x01,0x02,0x04,0x06,0x07,0x08,0x00,0x0F,0x01,0x03,0x07,0x0A,0x06,0x09,0x00
};



static struct MachineDriver machine_driver =
{
	/* basic machine hardware */
	{
		{
			CPU_M6809,
			2000000,	/* 2 Mhz (?) */
			0,
			readmem,writemem,0,0,
			interrupt,1
		},
		{
			CPU_M6809 | CPU_AUDIO_CPU,
			2000000,	/* 2 Mhz (?) */
			2,
			sound_readmem,sound_writemem,0,0,
			capcom_sh_interrupt,12
		},
	},
	60,
	10,	/* 10 CPU slices per frame - enough for the sound CPU to read all commands */
	sonson_init_machine,

	/* video hardware */
	32*8, 32*8, { 1*8, 31*8-1, 1*8, 31*8-1 },
	gfxdecodeinfo,
	32,64*4+32*8,
	sonson_vh_convert_color_prom,

	VIDEO_TYPE_RASTER,
	0,
	generic_vh_start,
	generic_vh_stop,
	sonson_vh_screenrefresh,

	/* sound hardware */
	0,
	0,
	capcom_sh_start,
	AY8910_sh_stop,
	AY8910_sh_update
};



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

  Game driver(s)

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

ROM_START( sonson_rom )
	ROM_REGION(0x10000)	/* 64k for code + 3*16k for the banked ROMs images */
	ROM_LOAD( "ss.01e", 0x4000, 0x4000, 0x3c2e5ca0 )
	ROM_LOAD( "ss.02e", 0x8000, 0x4000, 0x1774e5ac )
	ROM_LOAD( "ss.03e", 0xc000, 0x4000, 0xfaeb2cc7 )

	ROM_REGION(0x10000)	/* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "ss5.v12", 0x00000, 0x2000, 0xbbfc79ca )	/* characters */
	ROM_LOAD( "ss6.v12", 0x02000, 0x2000, 0xd0f12d0b )	/* characters */
	ROM_LOAD( "ss7.v12", 0x04000, 0x4000, 0x17488a98 )	/* sprites */
	ROM_LOAD( "ss8.v12", 0x08000, 0x4000, 0x9964d560 )	/* sprites */
	ROM_LOAD( "ss9.v12", 0x0c000, 0x4000, 0x34d73709 )	/* sprites */

	ROM_REGION(0x10000)	/* 64k for the audio CPU */
	ROM_LOAD( "ss3.v12", 0xe000, 0x2000, 0x8d3be09f )
ROM_END



static int hiload(void)
{
	/* get RAM pointer (this game is multiCPU, we can't assume the global */
	/* RAM pointer is pointing to the right place) */
	unsigned char *RAM = Machine->memory_region[0];


	/* check if the hi score table has already been initialized */
	if (memcmp(&RAM[0x0300],"\x00\x01\x00\x00",4) == 0 &&
			memcmp(&RAM[0x0320],"\x00\x01\x00\x00",4) == 0 &&
			memcmp(&RAM[0x00d8],"\x00\x01\x00\x00",4) == 0 &&	/* high score */
			memcmp(&RAM[0x0328],"\x00\x02\x00\x00",4) == 0 &&
			memcmp(&RAM[0x0358],"\x00\x02\x00\x00",4) == 0)
	{
		void *f;


		if ((f = osd_fopen(Machine->gamedrv->name,0,OSD_FILETYPE_HIGHSCORE,0)) != 0)
		{
			osd_fread(f,&RAM[0x0300],8*5+12*5);
			RAM[0x00d8] = RAM[0x0300];
			RAM[0x00d9] = RAM[0x0301];
			RAM[0x00da] = RAM[0x0302];
			RAM[0x00db] = RAM[0x0303];
			osd_fclose(f);
		}

		return 1;
	}
	else return 0;	/* we can't load the hi scores yet */
}



static void hisave(void)
{
	/* get RAM pointer (this game is multiCPU, we can't assume the global */
	/* RAM pointer is pointing to the right place) */
	unsigned char *RAM = Machine->memory_region[0];
	void *f;


	if ((f = osd_fopen(Machine->gamedrv->name,0,OSD_FILETYPE_HIGHSCORE,1)) != 0)
	{
		osd_fwrite(f,&RAM[0x0300],8*5+12*5);
		osd_fclose(f);
	}
}



struct GameDriver sonson_driver =
{
	"Son Son",
	"sonson",
	"MIRKO BUFFONI\nNICOLA SALMORIA\nGARY WALTON\nSIMON WALLS",
	&machine_driver,

	sonson_rom,
	0, 0,
	0,

	0/*TBR*/,input_ports,0/*TBR*/,0/*TBR*/,0/*TBR*/,

	color_prom, 0, 0,
	ORIENTATION_DEFAULT,

	hiload, hisave
};
