#include "../mame/drivers/contra.c"

#include "audio/misfmod.h"

static WRITE8_HANDLER( contram_sound_command_w )
{
	switch(data)
	{
	case 0:
		StopFile(0);
		break;
	
	case 77: /* Intro */
		PlayFile(0, 110, "songs/metlcnta/intro.ogg", 0, 0, 0);
		return;
	
	case 64: /* First Stage */
		PlayFile(0, 110, "songs/metlcnta/stage1.ogg", 1, 72619, 2320000);
		return;

	case 68: /* Corridors */
		PlayFile(0, 110, "songs/metlcnta/stage1a.ogg", 1, 0, 0);
		return;
	
	case 66: /* Second Stage */
		PlayFile(0, 110, "songs/metlcnta/stage2.ogg", 1, 447367, 1938000);
		return;

	case 71: /* BOSS */
		PlayFile(0, 110, "songs/metlcnta/boss.ogg", 1, 447367, 1938000);
		return;

	case 70: /* Third Stage */
		PlayFile(0, 110, "songs/metlcnta/stage3.ogg", 1, 161777, 1673000);
		return;
	
	case 67: /* Third Stage */
		PlayFile(0, 110, "songs/metlcnta/stage3a.ogg", 1, 0, 0);
		return;
	
	case 72: /* Alien's nast */
		PlayFile(0, 110, "songs/metlcnta/stage3b.ogg", 1, 480544, 1473000);
		return;
	
	case 76: /* Conclusion */
		PlayFile(0, 110, "songs/metlcnta/cnclsion.oggg", 0, 0, 0);
		return;

	case 73: /* Ending */
		PlayFile(0, 110, "songs/metlcnta/ending.ogg", 1, 0, 0);
		return;
	
	case 69: /* High Score */
		PlayFile(0, 110, "songs/metlcnta/complete.ogg", 0, 0, 0);
		return;
	
	case 74: /* Game Over */
		PlayFile(0, 110, "songs/metlcnta/gameover.ogg", 0, 0, 0);
		return;
	
	case 75: /* Mission Complete */
		PlayFile(0, 110, "songs/metlcnta/complete.ogg", 0, 0, 0);
		return;
	}

	soundlatch_w(machine, offset, data);
}

static ADDRESS_MAP_START( mwritemem, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x0007) AM_WRITE(contra_K007121_ctrl_0_w)
	AM_RANGE(0x0018, 0x0018) AM_WRITE(contra_coin_counter_w)
	AM_RANGE(0x001a, 0x001a) AM_WRITE(contra_sh_irqtrigger_w)
	AM_RANGE(0x001c, 0x001c) AM_WRITE(contram_sound_command_w)
	AM_RANGE(0x001e, 0x001e) AM_WRITE(SMH_NOP)	/* ? */
	AM_RANGE(0x0060, 0x0067) AM_WRITE(contra_K007121_ctrl_1_w)
	AM_RANGE(0x0c00, 0x0cff) AM_WRITE(SMH_RAM) AM_BASE(&paletteram)
	AM_RANGE(0x1000, 0x1fff) AM_WRITE(SMH_RAM)
	AM_RANGE(0x2000, 0x23ff) AM_WRITE(contra_fg_cram_w) AM_BASE(&contra_fg_cram)
	AM_RANGE(0x2400, 0x27ff) AM_WRITE(contra_fg_vram_w) AM_BASE(&contra_fg_vram)
	AM_RANGE(0x2800, 0x2bff) AM_WRITE(contra_text_cram_w) AM_BASE(&contra_text_cram)
	AM_RANGE(0x2c00, 0x2fff) AM_WRITE(contra_text_vram_w) AM_BASE(&contra_text_vram)
	AM_RANGE(0x3000, 0x37ff) AM_WRITE(SMH_RAM) AM_BASE(&spriteram)/* 2nd bank is at 0x5000 */
	AM_RANGE(0x3800, 0x3fff) AM_WRITE(SMH_RAM) // second sprite buffer
	AM_RANGE(0x4000, 0x43ff) AM_WRITE(contra_bg_cram_w) AM_BASE(&contra_bg_cram)
	AM_RANGE(0x4400, 0x47ff) AM_WRITE(contra_bg_vram_w) AM_BASE(&contra_bg_vram)
	AM_RANGE(0x4800, 0x5fff) AM_WRITE(SMH_RAM)
	AM_RANGE(0x6000, 0x6fff) AM_WRITE(SMH_ROM)
 	AM_RANGE(0x7000, 0x7000) AM_WRITE(contra_bankswitch_w)
	AM_RANGE(0x7001, 0xffff) AM_WRITE(SMH_ROM)
ADDRESS_MAP_END

static MACHINE_DRIVER_START( contram )
	MDRV_IMPORT_FROM(contra)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(readmem,mwritemem)
MACHINE_DRIVER_END


#define rom_contram rom_contra

GAME( 1987, contram,   0, contram, contra, 0, ROT90, "Konami", "Metal Contra", GAME_SUPPORTS_SAVE )
