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

	Star Wars (MisfitMAME version)

	December, 2008 by Robbbert

	This is a real dump, which came with colour proms.

	Differences between starw and galxwars:
	1. The audio port is 4 instead of 3
	2. Extra (unknown) write to ports 3 and 7 at boot time


	There is a minor bug in this game:
	If the dips are set for 5 ships, AND you scored enough to get an extra ship,
	then when your game ends, the attract mode will award itself an extra ship. This
	will overlay the word CREDIT, causing corrupt graphics.

	Changes for MisfitMAME:

	1. Created this one-off driver
	2. Added colour as per the flyers
	3. Fixed coin (1 coin 2 credits instead of 2 coins 1 credit)

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

#include "driver.h"
#include "invaders.h"
#include "sound/samples.h"
#include "sound/sn76477.h"



/*************************************
 *
 *  Sound
 *
 *************************************/


static WRITE8_HANDLER( galxwars_03_w )
{
	static UINT8 port_04_last = 0;
	UINT8 rising_bits = data & ~port_04_last;
	port_04_last = data;

	sn76477_enable_w(0, !(data & 0x01));				/* thrust sound */

	if (rising_bits & 0x04) sample_start_n(0, 0, 1, 0);		/* EXPLOSION */
	if (rising_bits & 0x10) sample_start_n(0, 1, 8, 0);		/* BONUS MISSILE BASE */
	screen_red = data & 4;

	sound_global_enable(data & 0x20);

	if (data & 0xca) printf("3=%X ",data);
}

static WRITE8_HANDLER( galxwars_05_w )
{
	/*
	00 - normal play
	10 - killed an invader
	20 - flip screen */

	static UINT8 port_05_last = 0;
	UINT8 rising_bits = data & ~port_05_last;
	port_05_last = data;

	if (rising_bits & 0x10) sample_start_n(0, 2, 7, 0);		/* invader hit */

	player = data & 0x20;
	flipscreen = player & input_port_read(machine, "CAB");

	if (data & 0xcf) printf("5=%X ",data);
}



/*************************************
 *
 *  Address Maps
 *
 *************************************/

static ADDRESS_MAP_START( galxwars_io_map, ADDRESS_SPACE_IO, 8 )
	AM_RANGE(0x00, 0x00) AM_READ_PORT("IN0")			/* used by galxwars, galxwar2 only */
	AM_RANGE(0x01, 0x01) AM_READ_PORT("IN1")
	AM_RANGE(0x02, 0x02) AM_READ(invaders_02_r)

	AM_RANGE(0x03, 0x03) AM_WRITE(galxwars_03_w)
	AM_RANGE(0x05, 0x05) AM_WRITE(galxwars_05_w)
	AM_RANGE(0x06, 0x06) AM_WRITE(watchdog_reset_w)
ADDRESS_MAP_END

static ADDRESS_MAP_START( starw1_io_map, ADDRESS_SPACE_IO, 8 )
	AM_RANGE(0x01, 0x01) AM_READ_PORT("IN1")
	AM_RANGE(0x02, 0x02) AM_READ(invaders_02_r)

	AM_RANGE(0x03, 0x03) AM_WRITENOP	/* writes 9B at boot */
	AM_RANGE(0x04, 0x04) AM_WRITE(galxwars_03_w)
	AM_RANGE(0x05, 0x05) AM_WRITE(galxwars_05_w)
	AM_RANGE(0x06, 0x06) AM_WRITE(watchdog_reset_w)
	AM_RANGE(0x07, 0x07) AM_WRITENOP	/* writes 89 at boot */
ADDRESS_MAP_END



/*************************************
 *
 *  Inputs
 *
 *************************************/

static INPUT_PORTS_START( galxwars )
	PORT_START("IN0")
	PORT_BIT( 0x40, IP_ACTIVE_LOW,  IPT_UNUSED )
	PORT_BIT( 0xbf, IP_ACTIVE_HIGH, IPT_UNUSED )

	PORT_START("IN1")
	PORT_BIT( 0x01, IP_ACTIVE_LOW,  IPT_COIN1 )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_START2 )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_START1 )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT ) PORT_2WAY
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_2WAY
	PORT_BIT( 0x80, IP_ACTIVE_LOW,  IPT_UNKNOWN )

	PORT_START("IN2")
	PORT_DIPNAME( 0x03, 0x00, DEF_STR( Lives ) )		PORT_DIPLOCATION("SW1:1,2")
	PORT_DIPSETTING(    0x00, "2" )
	PORT_DIPSETTING(    0x01, "3" )
	PORT_DIPSETTING(    0x02, "4" )
	PORT_DIPSETTING(    0x03, "5" )
	PORT_DIPUNKNOWN_DIPLOC( 0x04, 0x00, "SW1:3" )
	PORT_DIPNAME( 0x08, 0x00, DEF_STR( Bonus_Life ) )	PORT_DIPLOCATION("SW1:4")
	PORT_DIPSETTING(    0x00, "3000" )
	PORT_DIPSETTING(    0x08, "5000" )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_PLAYER(2)			PORT_DIPLOCATION("SW1:5")
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT ) PORT_2WAY PORT_PLAYER(2)	PORT_DIPLOCATION("SW1:6")
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_2WAY PORT_PLAYER(2)	PORT_DIPLOCATION("SW1:7")
	PORT_DIPNAME( 0x80, 0x00, DEF_STR( Coinage ) )					PORT_DIPLOCATION("SW1:8")
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x80, DEF_STR( 1C_2C ) )

	INVADERS_CAB(0x20)
INPUT_PORTS_END


/*************************************
 *
 *  Machine Drivers
 *
 *************************************/

static MACHINE_DRIVER_START( galxwars )

	/* basic machine hardware */
	MDRV_CPU_ADD("main",8080,INVADERS_CPU_CLOCK)
	MDRV_CPU_PROGRAM_MAP(invaders_map,0)
	MDRV_CPU_IO_MAP(galxwars_io_map,0)
	MDRV_WATCHDOG_VBLANK_INIT(255)
	MDRV_MACHINE_START(invaders_common)
	MDRV_MACHINE_RESET(invaders_common)

	/* video hardware */
	MDRV_VIDEO_UPDATE(invadpt2)

	MDRV_SCREEN_ADD("main", RASTER)
	MDRV_SCREEN_FORMAT(BITMAP_FORMAT_RGB32)
	MDRV_SCREEN_RAW_PARAMS(INVADERS_PIXEL_CLOCK, INVADERS_HTOTAL, INVADERS_HBEND, INVADERS_HPIXCOUNT, INVADERS_VTOTAL, INVADERS_VBEND, INVADERS_VBSTART)

	/* sound hardware */
	MDRV_IMPORT_FROM(invaders_samples_audio)
MACHINE_DRIVER_END

static MACHINE_DRIVER_START( starw1 )
	MDRV_IMPORT_FROM(galxwars)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_IO_MAP(starw1_io_map,0)
MACHINE_DRIVER_END



/*************************************
 *
 *  Roms
 *
 *************************************/

/* compatible color proms - bits 0,1,2 are identical between the sets

	From lrescuem - not suitable for that game - perfect for most invader-type sets
	ROM_LOAD( "cv01-7643.2c", 0x0000, 0x0400, CRC(aac24f34) SHA1(ad110e776547fb48baac568bb50d61854537ca34) )
	ROM_LOAD( "cv02-7643.1c", 0x0400, 0x0400, CRC(2bdf83a0) SHA1(01ffbd43964c41987e7d44816271308f9a70802b) )

	From schsrcv - not suitable for that game - same as above but bit 3 is set on all bytes
	ROM_LOAD_OPTIONAL( "cv01", 0x0000, 0x0400, CRC(037e16ac) SHA1(d585030aaff428330c91ae94d7cd5c96ebdd67dd) )
	ROM_LOAD_OPTIONAL( "cv02", 0x0400, 0x0400, CRC(8263da38) SHA1(2e7c769d129e6f8a1a31eba1e02777bb94ac32b2) )

	From starw1 - remove the last 0x400 bytes, then AND each byte with 7 - the result is the same as lrescuem above
	ROM_LOAD( "cv01",         0x0000, 0x0800, CRC(8d892ef3) SHA1(c471dd6197a3c779d89c33fcb425cf3bbdf4fc15) )
	ROM_LOAD( "cv02",         0x0400, 0x0800, CRC(b44ddde8) SHA1(8793f370526c072e645d8d0b9794b1b64a7701ef) )
	*/	

ROM_START( galxwars )
	ROM_REGION( 0x10000, "main", 0 )
	ROM_LOAD( "univgw3.0",    0x0000, 0x0400, CRC(937796f4) SHA1(88e9494cc532498e51e3a68fa1122c40f22b27dd) )
	ROM_LOAD( "univgw4.1",    0x0400, 0x0400, CRC(4b86e7a6) SHA1(167f9f7491a2de39d08e3e6f7057cc75b36c9340) )
	ROM_LOAD( "univgw5.2",    0x0800, 0x0400, CRC(47a187cd) SHA1(640c896ba25f34d323624005bd676257ad17b687) )
	ROM_LOAD( "univgw6.3",    0x0c00, 0x0400, CRC(7b7d22ff) SHA1(74364cf2b04dcfbbc8e0131fa12c0e574f693d34) )
	ROM_LOAD( "univgw1.4",    0x4000, 0x0400, CRC(0871156e) SHA1(3726d0bfe153a0afc62ea56737662074986064b0) )
	ROM_LOAD( "univgw2.5",    0x4400, 0x0400, CRC(6036d7bf) SHA1(36c2ad2ffdb47bbecc40fd67ced6ab51a5cd2f3e) )

	ROM_REGION( 0x0800, "proms", 0 )
	ROM_LOAD( "cv01-7643.2c", 0x0000, 0x0400, CRC(aac24f34) SHA1(ad110e776547fb48baac568bb50d61854537ca34) )
	ROM_LOAD( "cv02-7643.1c", 0x0400, 0x0400, CRC(2bdf83a0) SHA1(01ffbd43964c41987e7d44816271308f9a70802b) )
ROM_END

ROM_START( galxwar2 )
	ROM_REGION( 0x10000, "main", 0 )
	ROM_LOAD( "3192.h6",      0x0000, 0x1000, CRC(bde6860b) SHA1(e04b8add32d8f7ea588fae6d6a387f1d40495f1b) )
	ROM_LOAD( "3193.h7",      0x4000, 0x1000, CRC(a17cd507) SHA1(554ab0e8bdc0e7af4a30b0ddc8aa053c8e70255c) )

	ROM_REGION( 0x0800, "proms", 0 )
	ROM_LOAD( "cv01-7643.2c", 0x0000, 0x0400, CRC(aac24f34) SHA1(ad110e776547fb48baac568bb50d61854537ca34) )
	ROM_LOAD( "cv02-7643.1c", 0x0400, 0x0400, CRC(2bdf83a0) SHA1(01ffbd43964c41987e7d44816271308f9a70802b) )
ROM_END

ROM_START( galxwart )
	ROM_REGION( 0x10000, "main", 0 )
	ROM_LOAD( "galxwars.0",   0x0000, 0x0400, CRC(608bfe7f) SHA1(a41a40a2f0a1bb61a70b9ff8a7da925ab1db7f74) )
	ROM_LOAD( "galxwars.1",   0x0400, 0x0400, CRC(a810b258) SHA1(030a72fffcf240f643bc3006028cb4883cf58bbc) )
	ROM_LOAD( "galxwars.2",   0x0800, 0x0400, CRC(74f31781) SHA1(1de70e8ebbb26eea20ffedb7bd0ca051a67f45e7) )
	ROM_LOAD( "galxwars.3",   0x0c00, 0x0400, CRC(c88f886c) SHA1(4d705fbb97e3868c3f6c90c5e5753ad17cfbf5d6) )
	ROM_LOAD( "galxwars.4",   0x4000, 0x0400, CRC(ae4fe8fb) SHA1(494f44167dc84e4515b769c12f6e24419461dce4) )
	ROM_LOAD( "galxwars.5",   0x4400, 0x0400, CRC(37708a35) SHA1(df6fd521ddfa146ef93e390e47741bdbfda1e7ba) )

	ROM_REGION( 0x0800, "proms", 0 )
	ROM_LOAD( "cv01-7643.2c", 0x0000, 0x0400, CRC(aac24f34) SHA1(ad110e776547fb48baac568bb50d61854537ca34) )
	ROM_LOAD( "cv02-7643.1c", 0x0400, 0x0400, CRC(2bdf83a0) SHA1(01ffbd43964c41987e7d44816271308f9a70802b) )
ROM_END

ROM_START( starw )
	ROM_REGION( 0x10000, "main", 0 )
	ROM_LOAD( "roma",         0x0000, 0x0400, CRC(60e8993c) SHA1(0bdf163ff0f2e6a8771987d4e7ac604c45af21b8) )
	ROM_LOAD( "romb",         0x0400, 0x0400, CRC(b8060773) SHA1(92aa358c338ef8f5773bccada8988d068764e7ea) )
	ROM_LOAD( "romc",         0x0800, 0x0400, CRC(307ce6b8) SHA1(f4b6f54db3d2377ec27d62d33fa1c4946559a092) )
	ROM_LOAD( "romd",         0x1400, 0x0400, CRC(2b0d0a88) SHA1(d079d12b6d4136519ded32415d668a02147b7601) )
	ROM_LOAD( "rome",         0x1800, 0x0400, CRC(5b1c3ad0) SHA1(edb42eec59c3dd7e274e2ea08fed0f3e8fc72e9e) )
	ROM_LOAD( "romf",         0x1c00, 0x0400, CRC(c8e42d3d) SHA1(841b27af251b9c3a964972e864fb7c88acc742e0) )

	ROM_REGION( 0x0800, "proms", 0 )
	ROM_LOAD( "cv01-7643.2c", 0x0000, 0x0400, CRC(aac24f34) SHA1(ad110e776547fb48baac568bb50d61854537ca34) )
	ROM_LOAD( "cv02-7643.1c", 0x0400, 0x0400, CRC(2bdf83a0) SHA1(01ffbd43964c41987e7d44816271308f9a70802b) )
ROM_END

ROM_START( starw1 )
	ROM_REGION( 0x10000, "main", 0 )
	ROM_LOAD( "gc.75",        0x0000, 0x0400, CRC(ad10c128) SHA1(c30ff9ff5cf8dedf7654c8e2799a4bb79a30104a) )
	ROM_LOAD( "gc.77",        0x0400, 0x0400, CRC(ab77c474) SHA1(eb07dcad1f265834b93a8108298d4441d6a74b2e) )
	ROM_LOAD( "gc.76",        0x0800, 0x0400, CRC(3638aed4) SHA1(1426c9270f248fd2ab134dc35526599c02051ccd) )
	ROM_LOAD( "gc.80",        0x1400, 0x0400, CRC(4c67957b) SHA1(dda7bbd54e7395dea80d224e487318fb4429f027) )
	ROM_LOAD( "gc.81",        0x1800, 0x0400, CRC(246621ef) SHA1(bddc5253f735fa81266d725a24b1c14faabe0c6a) )
	ROM_LOAD( "gc.82",        0x1c00, 0x0400, CRC(19bd32ee) SHA1(a1718a6a6300c3d7df469793cb0d590c4a966aff) )

	ROM_REGION( 0x0800, "proms", 0 )		/* color maps player 1/player 2 */
	ROM_LOAD( "cv01",         0x0000, 0x0400, CRC(8d892ef3) SHA1(c471dd6197a3c779d89c33fcb425cf3bbdf4fc15) )
	ROM_IGNORE( 0x0400 )
	ROM_LOAD( "cv02",         0x0400, 0x0400, CRC(b44ddde8) SHA1(8793f370526c072e645d8d0b9794b1b64a7701ef) )
	ROM_IGNORE( 0x0400 )
ROM_END

/*************************************
 *
 *  Game Drivers
 *
 *************************************/

/* board #  rom       parent    machine   inp */

GAME(1979, galxwars, 0,        galxwars, galxwars, 0, ROT270, "Universal", "Galaxy Wars (Universal set 1)", 0 | GAME_SUPPORTS_SAVE )
GAME(1979, galxwar2, galxwars, galxwars, galxwars, 0, ROT270, "Universal", "Galaxy Wars (Universal set 2)", 0 | GAME_SUPPORTS_SAVE )
GAME(1979, galxwart, galxwars, galxwars, galxwars, 0, ROT270, "Taito?", "Galaxy Wars (Taito?)" , 0 | GAME_SUPPORTS_SAVE ) /* Copyright Not Displayed */
GAME(1979, starw,    galxwars, galxwars, galxwars, 0, ROT270, "bootleg", "Star Wars (set 1)", 0 | GAME_SUPPORTS_SAVE )
GAME(1979, starw1,   galxwars, starw1,   galxwars, 0, ROT270, "Yamashita", "Star Wars (set 2)", 0 | GAME_SUPPORTS_SAVE )
