#include "driver.h"
#include "cpu/i8039/i8039.h"
#include "sound/dac.h"
#include "sound/samples.h"
#include "includes/dkong.h"
#include "machine/8257dma.h"

static UINT8 page,mcustatus;
static UINT8 p[8];
static UINT8 t[2];
static double tt;
static UINT8 decay;
static INT8 counter;
static int hunchloopback;

#define ACTIVELOW_PORT_BIT(P,A,D)   ((P & (~(1 << A))) | ((D ^ 1) << A))

WRITE8_HANDLER( dkong_sh_sound3_w )     { p[2] = ACTIVELOW_PORT_BIT(p[2],5,data); }
WRITE8_HANDLER( dkong_sh_sound4_w )     { t[1] = ~data & 1; }
WRITE8_HANDLER( dkong_sh_sound5_w )     { t[0] = ~data & 1; }
WRITE8_HANDLER( dkong_sh_tuneselect_w ) { soundlatch_w(machine, offset,data ^ 0x0f); }

WRITE8_HANDLER( dkongjr_sh_test6_w )      { p[2] = ACTIVELOW_PORT_BIT(p[2],6,data); }
WRITE8_HANDLER( dkongjr_sh_test5_w )      { p[2] = ACTIVELOW_PORT_BIT(p[2],5,data); }
WRITE8_HANDLER( dkongjr_sh_test4_w )      { p[2] = ACTIVELOW_PORT_BIT(p[2],4,data); }
WRITE8_HANDLER( dkongjr_sh_tuneselect_w ) { soundlatch_w(machine, offset,data); }


static READ8_HANDLER( dkong_sh_p1_r )   { return p[1]; }
static READ8_HANDLER( dkong_sh_p2_r )   { return p[2]; }
static READ8_HANDLER( dkong_sh_t0_r )   { return t[0]; }
static READ8_HANDLER( dkong_sh_t1_r )   { return t[1]; }
static READ8_HANDLER( dkong_sh_tune_r )
{
	UINT8 *SND = memory_region(machine, "audio");
	if (page & 0x40)
	{
		switch (offset)
		{
			case 0x20:  return soundlatch_r(machine, 0);
		}
	}
	return (SND[2048+((page & 7)<<8)+offset]);
}

#define TSTEP 0.001

static WRITE8_HANDLER( dkong_sh_p1_w )
{
	dac_data_w(0, data);
//	DAC_data_w(0,(int)(data*exp(-tt)));
	if (decay) tt+=TSTEP; else tt=0;
}

static WRITE8_HANDLER( dkong_sh_p2_w )
{
	/*   If P2.Bit7 -> is apparently an external signal decay or other output control
     *   If P2.Bit6 -> activates the external compressed sample ROM
     *   If P2.Bit4 -> status code to main cpu
     *   P2.Bit2-0  -> select the 256 byte bank for external ROM
     */

	decay = !(data & 0x80);
	page = (data & 0x47);
	mcustatus = ((~data & 0x10) >> 4);
}

static READ8_HANDLER( dkong_in2_r )
{
	return input_port_2_r(machine, offset) | (mcustatus << 6);
}

/* EPOS games */

static MACHINE_START( dkong )
{
	state_save_register_global(page);
	state_save_register_global(mcustatus);
	state_save_register_global_array(p);
	state_save_register_global_array(t);
	state_save_register_global(tt);
	state_save_register_global(decay);
	state_save_register_global(counter);
	state_save_register_global(hunchloopback);

	mcustatus = 0;
	tt = 0;
	decay = 0;
	hunchloopback = 0;
}

static MACHINE_RESET( dkong )
{
	page = 0;
	p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = p[6] = p[7] = 255;
	t[0] = t[1] = 1;
	counter = 0;
}


static ADDRESS_MAP_START( readmem, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x5fff) AM_READ(SMH_ROM)	/* DK: 0000-3fff */
	AM_RANGE(0x6000, 0x6fff) AM_READ(SMH_RAM)	/* including sprites RAM */
	AM_RANGE(0x7400, 0x77ff) AM_READ(SMH_RAM)	/* video RAM */
	AM_RANGE(0x7c00, 0x7c00) AM_READ(input_port_0_r)	/* IN0 */
	AM_RANGE(0x7c80, 0x7c80) AM_READ(input_port_1_r)	/* IN1 */
	AM_RANGE(0x7d00, 0x7d00) AM_READ(dkong_in2_r)	/* IN2/DSW2 */
	AM_RANGE(0x7d80, 0x7d80) AM_READ(input_port_3_r)	/* DSW1 */
	AM_RANGE(0x8000, 0x9fff) AM_READ(SMH_ROM)	/* DK3 and bootleg DKjr only */
	AM_RANGE(0xb000, 0xbfff) AM_READ(SMH_ROM)	/* Pest Place only */
	AM_RANGE(0xd000, 0xdfff) AM_READ(SMH_ROM)	/* DK3 bootleg only */
ADDRESS_MAP_END

static ADDRESS_MAP_START( dkong_writemem, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x5fff) AM_WRITE(SMH_ROM)
	AM_RANGE(0x6000, 0x68ff) AM_WRITE(SMH_RAM)
	AM_RANGE(0x6900, 0x6a7f) AM_WRITE(SMH_RAM) AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
	AM_RANGE(0x6a80, 0x6fff) AM_WRITE(SMH_RAM)
	AM_RANGE(0x7000, 0x73ff) AM_WRITE(SMH_RAM)    /* ???? */
	AM_RANGE(0x7400, 0x77ff) AM_WRITE(dkong_videoram_w) AM_BASE(&videoram)
	AM_RANGE(0x7800, 0x7803) AM_WRITE(SMH_RAM)	/* ???? */
	AM_RANGE(0x7808, 0x7808) AM_WRITE(SMH_RAM)	/* ???? */
	AM_RANGE(0x7c00, 0x7c00) AM_WRITE(dkong_sh_tuneselect_w)
//  AM_RANGE(0x7c80, 0x7c80)
	AM_RANGE(0x7d00, 0x7d02) AM_WRITE(dkong_sh1_w)	/* walk/jump/boom sample trigger */
	AM_RANGE(0x7d03, 0x7d03) AM_WRITE(dkong_sh_sound3_w)
	AM_RANGE(0x7d04, 0x7d04) AM_WRITE(dkong_sh_sound4_w)
	AM_RANGE(0x7d05, 0x7d05) AM_WRITE(dkong_sh_sound5_w)
	AM_RANGE(0x7d80, 0x7d80) AM_WRITE(dkong_sh_w)
	AM_RANGE(0x7d81, 0x7d81) AM_WRITE(SMH_RAM)	/* ???? */
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITE(SMH_RAM)
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_WRITE(SMH_RAM)
	AM_RANGE(0x7d86, 0x7d87) AM_WRITE(dkong_palettebank_w)
ADDRESS_MAP_END


static ADDRESS_MAP_START( readmem_sound, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x0fff) AM_READ(SMH_ROM)
ADDRESS_MAP_END

static ADDRESS_MAP_START( writemem_sound, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x0fff) AM_WRITE(SMH_ROM)
ADDRESS_MAP_END

static ADDRESS_MAP_START( readport_sound, ADDRESS_SPACE_IO, 8 )
	AM_RANGE(0x00, 0xff) AM_READ(dkong_sh_tune_r)
	AM_RANGE(I8039_p1, I8039_p1) AM_READ(dkong_sh_p1_r)
	AM_RANGE(I8039_p2, I8039_p2) AM_READ(dkong_sh_p2_r)
	AM_RANGE(I8039_t0, I8039_t0) AM_READ(dkong_sh_t0_r)
	AM_RANGE(I8039_t1, I8039_t1) AM_READ(dkong_sh_t1_r)
ADDRESS_MAP_END

static ADDRESS_MAP_START( writeport_sound, ADDRESS_SPACE_IO, 8 )
	AM_RANGE(I8039_p1, I8039_p1) AM_WRITE(dkong_sh_p1_w)
	AM_RANGE(I8039_p2, I8039_p2) AM_WRITE(dkong_sh_p2_w)
ADDRESS_MAP_END

static ADDRESS_MAP_START( dkongjr_writemem, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x5fff) AM_WRITE(SMH_ROM)
	AM_RANGE(0x6000, 0x68ff) AM_WRITE(SMH_RAM)
	AM_RANGE(0x6900, 0x6a7f) AM_WRITE(SMH_RAM) AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
	AM_RANGE(0x6a80, 0x6fff) AM_WRITE(SMH_RAM)
	AM_RANGE(0x7400, 0x77ff) AM_WRITE(dkong_videoram_w) AM_BASE(&videoram)
	AM_RANGE(0x7800, 0x7803) AM_WRITE(SMH_RAM)	/* ???? */
	AM_RANGE(0x7808, 0x7808) AM_WRITE(SMH_RAM)	/* ???? */
	AM_RANGE(0x7c00, 0x7c00) AM_WRITE(dkongjr_sh_tuneselect_w)
	AM_RANGE(0x7c80, 0x7c80) AM_WRITE(dkongjr_gfxbank_w)
	AM_RANGE(0x7c81, 0x7c81) AM_WRITE(dkongjr_sh_test6_w)
	AM_RANGE(0x7d00, 0x7d00) AM_WRITE(dkongjr_sh_climb_w) /* HC - climb sound */
	AM_RANGE(0x7d01, 0x7d01) AM_WRITE(dkongjr_sh_jump_w) /* HC - jump */
	AM_RANGE(0x7d02, 0x7d02) AM_WRITE(dkongjr_sh_land_w) /* HC - climb sound */
	AM_RANGE(0x7d03, 0x7d03) AM_WRITE(dkongjr_sh_roar_w)
	AM_RANGE(0x7d04, 0x7d04) AM_WRITE(dkong_sh_sound4_w)
	AM_RANGE(0x7d05, 0x7d05) AM_WRITE(dkong_sh_sound5_w)
	AM_RANGE(0x7d06, 0x7d06) AM_WRITE(dkongjr_sh_snapjaw_w)
	AM_RANGE(0x7d07, 0x7d07) AM_WRITE(dkongjr_sh_walk_w)	/* controls pitch of the walk/climb? */
	AM_RANGE(0x7d80, 0x7d80) AM_WRITE(dkongjr_sh_death_w)
	AM_RANGE(0x7d81, 0x7d81) AM_WRITE(dkongjr_sh_drop_w)   /* active when Junior is falling */
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_WRITE(SMH_RAM)
	AM_RANGE(0x7d86, 0x7d87) AM_WRITE(dkong_palettebank_w)
	AM_RANGE(0x8000, 0x9fff) AM_WRITE(SMH_ROM)	/* bootleg DKjr only */
	AM_RANGE(0xd000, 0xdfff) AM_WRITE(SMH_ROM)	/* DK3 bootleg only */
ADDRESS_MAP_END



INPUT_PORTS_START( dkong )
	PORT_START ("IN0")      /* IN0 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_4WAY
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT ) PORT_4WAY
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP ) PORT_4WAY
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN ) PORT_4WAY
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("IN1")      /* IN1 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_4WAY PORT_COCKTAIL
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT ) PORT_4WAY PORT_COCKTAIL
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP ) PORT_4WAY PORT_COCKTAIL
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN ) PORT_4WAY PORT_COCKTAIL
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_COCKTAIL
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("IN2")      /* IN2 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_SERVICE ) PORT_NAME(DEF_STR( Service_Mode )) PORT_CODE(KEYCODE_F2)
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_START1 )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_START2 )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_SPECIAL )	/* status from sound cpu */
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_COIN1 )

	PORT_START("DSW0")      /* DSW0 */
	PORT_DIPNAME( 0x03, 0x00, DEF_STR( Lives ) )
	PORT_DIPSETTING(    0x00, "3" )
	PORT_DIPSETTING(    0x01, "4" )
	PORT_DIPSETTING(    0x02, "5" )
	PORT_DIPSETTING(    0x03, "6" )
	PORT_DIPNAME( 0x0c, 0x00, DEF_STR( Bonus_Life ) )
	PORT_DIPSETTING(    0x00, "7000" )
	PORT_DIPSETTING(    0x04, "10000" )
	PORT_DIPSETTING(    0x08, "15000" )
	PORT_DIPSETTING(    0x0c, "20000" )
	PORT_DIPNAME( 0x70, 0x00, DEF_STR( Coinage ) )
	PORT_DIPSETTING(    0x70, DEF_STR( 5C_1C ) )
	PORT_DIPSETTING(    0x50, DEF_STR( 4C_1C ) )
	PORT_DIPSETTING(    0x30, DEF_STR( 3C_1C ) )
	PORT_DIPSETTING(    0x10, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x20, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x40, DEF_STR( 1C_3C ) )
	PORT_DIPSETTING(    0x60, DEF_STR( 1C_4C ) )
	PORT_DIPNAME( 0x80, 0x80, DEF_STR( Cabinet ) )
	PORT_DIPSETTING(    0x80, DEF_STR( Upright ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Cocktail ) )
INPUT_PORTS_END


static const gfx_layout charlayout =
{
	8,8,	/* 8*8 characters */
	RGN_FRAC(1,2),
	2,	/* 2 bits per pixel */
	{ RGN_FRAC(1,2), RGN_FRAC(0,2) },	/* 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 const gfx_layout spritelayout =
{
	16,16,	/* 16*16 sprites */
	RGN_FRAC(1,4),	/* 128 sprites */
	2,	/* 2 bits per pixel */
	{ RGN_FRAC(1,2), RGN_FRAC(0,2) },	/* the two bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7,	/* the two halves of the sprite are separated */
			RGN_FRAC(1,4)+0, RGN_FRAC(1,4)+1, RGN_FRAC(1,4)+2, RGN_FRAC(1,4)+3, RGN_FRAC(1,4)+4, RGN_FRAC(1,4)+5, RGN_FRAC(1,4)+6, RGN_FRAC(1,4)+7 },
	{ 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 },
	16*8	/* every sprite takes 16 consecutive bytes */
};

static GFXDECODE_START( dkong )
	GFXDECODE_ENTRY( "gfx1", 0x0000, charlayout,   0, 64 )
	GFXDECODE_ENTRY( "gfx2", 0x0000, spritelayout, 0, 64 )
GFXDECODE_END

static const char *const dkong_sample_names[] =
{
	"*dkong",
	"run01.wav",
	"run02.wav",
	"run03.wav",
	"jump.wav",
	"dkstomp.wav",
	0	/* end of array */
};

static const char *const dkongjr_sample_names[] =
{
	"*dkongjr",
	"jump.wav",
	"land.wav",
	"roar.wav",
	"climb0.wav",
	"climb1.wav",
	"climb2.wav",
	"death.wav",
	"drop.wav",
	"walk0.wav",
	"walk1.wav",
	"walk2.wav",
	"snapjaw.wav",
	0	/* end of array */
};

static const samples_interface dkong_samples_interface =
{
	8,	/* 8 channels */
	dkong_sample_names
};

static const samples_interface dkongjr_samples_interface =
{
	8,	/* 8 channels */
	dkongjr_sample_names
};


static MACHINE_DRIVER_START( dkong )

	/* basic machine hardware */
	MDRV_CPU_ADD ("main",Z80, 3072000)	/* 3.072 MHz (?) */
	MDRV_CPU_PROGRAM_MAP(readmem,dkong_writemem)
	MDRV_CPU_VBLANK_INT("main",nmi_line_pulse)

	MDRV_CPU_ADD("audio", I8035,6e6)	/* 6MHz crystal */
	MDRV_CPU_PROGRAM_MAP(readmem_sound,writemem_sound)
	MDRV_CPU_IO_MAP(readport_sound,writeport_sound)

	MDRV_MACHINE_START(dkong)
	MDRV_MACHINE_RESET(dkong)

	/* video hardware */
	MDRV_SCREEN_ADD("main", RASTER)
	MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
	MDRV_SCREEN_SIZE(32*8, 32*8)
	MDRV_SCREEN_VISIBLE_AREA(0*8, 32*8-1, 1*8, 31*8-1)
	MDRV_SCREEN_REFRESH_RATE(60)
	MDRV_GFXDECODE(dkong)
	MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(250))
	MDRV_PALETTE_LENGTH(256)

	MDRV_PALETTE_INIT(dkong)
	MDRV_VIDEO_START(dkong)
	MDRV_VIDEO_UPDATE(dkong)

	/* sound hardware */
	MDRV_SOUND_START(dkong)

	MDRV_SPEAKER_STANDARD_MONO("mono")
	MDRV_SOUND_ADD("dac", DAC, 0)
	MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.5)

	MDRV_SOUND_ADD ("samples", SAMPLES, 0)
	MDRV_SOUND_CONFIG(dkong_samples_interface)
	MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
MACHINE_DRIVER_END

static MACHINE_DRIVER_START( dkongjr )
	MDRV_IMPORT_FROM(dkong)

	/* basic machine hardware */
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(readmem,dkongjr_writemem)

	/* sound hardware */
	MDRV_SOUND_REPLACE("samples", SAMPLES, 0)
	MDRV_SOUND_CONFIG(dkongjr_samples_interface)
	MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
MACHINE_DRIVER_END

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

  Game driver(s)

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

ROM_START( dkong )
	ROM_REGION( 0x10000, "main", 0 )	/* 64k for code */
	ROM_LOAD( "c_5et_g.bin",  0x0000, 0x1000, CRC(ba70b88b) SHA1(d76ebecfea1af098d843ee7e578e480cd658ac1a) )
	ROM_LOAD( "c_5ct_g.bin",  0x1000, 0x1000, CRC(5ec461ec) SHA1(acb11a8fbdbb3ab46068385fe465f681e3c824bd) )
	ROM_LOAD( "c_5bt_g.bin",  0x2000, 0x1000, CRC(1c97d324) SHA1(c7966261f3a1d3296927e0b6ee1c58039fc53c1f) )
	ROM_LOAD( "c_5at_g.bin",  0x3000, 0x1000, CRC(b9005ac0) SHA1(3fe3599f6fa7c496f782053ddf7bacb453d197c4) )
	/* space for diagnostic ROM */

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "v_5h_b.bin",   0x0000, 0x0800, CRC(12c8c95d) SHA1(a57ff5a231c45252a63b354137c920a1379b70a3) )
	ROM_LOAD( "v_3pt.bin",    0x0800, 0x0800, CRC(15e9c5e9) SHA1(976eb1e18c74018193a35aa86cff482ebfc5cc4e) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "l_4m_b.bin",   0x0000, 0x0800, CRC(59f8054d) SHA1(793dba9bf5a5fe76328acdfb90815c243d2a65f1) )
	ROM_LOAD( "l_4n_b.bin",   0x0800, 0x0800, CRC(672e4714) SHA1(92e5d379f4838ac1fa44d448ce7d142dae42102f) )
	ROM_LOAD( "l_4r_b.bin",   0x1000, 0x0800, CRC(feaa59ee) SHA1(ecf95db5a20098804fc8bd59232c66e2e0ed3db4) )
	ROM_LOAD( "l_4s_b.bin",   0x1800, 0x0800, CRC(20f2ef7e) SHA1(3bc482a38bf579033f50082748ee95205b0f673d) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "v-5e.bpr",     0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) ) /* character color codes on a per-column basis */

/*********************************************************
I use more appropreate filenames for color PROMs.
    ROM_REGION( 0x0300, "proms", 0 )
    ROM_LOAD( "dkong.2k",     0x0000, 0x0100, CRC(1e82d375) )
    ROM_LOAD( "dkong.2j",     0x0100, 0x0100, CRC(2ab01dc8) )
    ROM_LOAD( "dkong.5f",     0x0200, 0x0100, CRC(44988665) )
*********************************************************/
ROM_END


GAME( 1981, dkong,    0,        dkong,    dkong,    0,        ROT90, "Nintendo of America", "Donkey Kong (US set 1)", GAME_SUPPORTS_SAVE )

/* Misfitmame start */

#include "machine/eeprom.h"

/* dkonghs and dkjrhs store high scores in a EEPROM device, type ST93C66, 512 bytes x 8 bits.
	The data being read is inverted by a spare gate on the existing 74LS240 and becomes bit 7 of 7C00. */

const eeprom_interface eeprom_interface_dkonghs =
{
	9,				// address bits 9
	8,				// data bits    8
	"*110",			// read         1 10 aaaaaa
	"*101",			// write        1 01 aaaaaa dddddddddddddddd
	"*111",			// erase        1 11 aaaaaa
	"*10000xxxx",	// lock         1 00 00xxxx
	"*10011xxxx",	// unlock       1 00 11xxxx
	1,
//  "*10001xxxx"    // write all    1 00 01xxxx dddddddddddddddd
//  "*10010xxxx"    // erase all    1 00 10xxxx
};

NVRAM_HANDLER( dkonghs )
{
	if (read_or_write)
		eeprom_save(file);
	else
	{
		eeprom_init(&eeprom_interface_dkonghs);
		if (file)	eeprom_load(file);
	}
}

static READ8_HANDLER( dkonghs_eeprom_r )
{
	return (input_port_read(machine,"IN0") & 0x7f) | ((~eeprom_read_bit()&1)<<7);
}

static WRITE8_HANDLER( dkonghs_eeprom_data_w )
{
	eeprom_write_bit(data & 1);
}

static WRITE8_HANDLER( dkonghs_eeprom_clock_w )
{
	eeprom_set_clock_line( (data & 1) ? ASSERT_LINE : CLEAR_LINE );
}

static WRITE8_HANDLER( dkjrhs_eeprom_clock_w )
{
	eeprom_set_clock_line( (~data & 1) ? ASSERT_LINE : CLEAR_LINE );
}

static WRITE8_HANDLER( dkonghs_eeprom_cs_w )
{
	eeprom_set_cs_line( (data & 1) ? CLEAR_LINE : ASSERT_LINE );
}


static ADDRESS_MAP_START( dkonghs_map, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x5fff) AM_ROM
	AM_RANGE(0x6000, 0x68ff) AM_RAM
	AM_RANGE(0x6900, 0x6a7f) AM_RAM AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
	AM_RANGE(0x6a80, 0x6fff) AM_RAM
	AM_RANGE(0x7000, 0x73ff) AM_WRITENOP
	AM_RANGE(0x7400, 0x77ff) AM_RAM_WRITE(dkong_videoram_w) AM_BASE(&videoram)
	AM_RANGE(0x7800, 0x7808) AM_WRITENOP
	AM_RANGE(0x7c00, 0x7c00) AM_READWRITE(dkonghs_eeprom_r,dkong_sh_tuneselect_w)
	AM_RANGE(0x7c80, 0x7c80) AM_READ(input_port_1_r)	/* IN1 */
	AM_RANGE(0x7d00, 0x7d00) AM_READ(dkong_in2_r)	/* IN2/DSW2 */
	AM_RANGE(0x7d00, 0x7d02) AM_WRITE(dkong_sh1_w)	/* walk/jump/boom sample trigger */
	AM_RANGE(0x7d03, 0x7d03) AM_WRITE(dkong_sh_sound3_w)
	AM_RANGE(0x7d04, 0x7d04) AM_WRITE(dkong_sh_sound4_w)
	AM_RANGE(0x7d05, 0x7d05) AM_WRITE(dkong_sh_sound5_w)
	AM_RANGE(0x7d06, 0x7d06) AM_WRITE(dkonghs_eeprom_clock_w)
	AM_RANGE(0x7d07, 0x7d07) AM_WRITE(dkonghs_eeprom_data_w)
	AM_RANGE(0x7d80, 0x7d80) AM_READWRITE(input_port_3_r,dkong_sh_w)	/* DSW1 */
	AM_RANGE(0x7d81, 0x7d81) AM_WRITE(dkonghs_eeprom_cs_w)
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITENOP
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_WRITENOP
	AM_RANGE(0x7d86, 0x7d87) AM_WRITE(dkong_palettebank_w)
ADDRESS_MAP_END

static ADDRESS_MAP_START( dkjrhs_map, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x5fff) AM_ROM
	AM_RANGE(0x6000, 0x68ff) AM_RAM
	AM_RANGE(0x6900, 0x6a7f) AM_RAM AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
	AM_RANGE(0x6a80, 0x6fff) AM_RAM
	AM_RANGE(0x7000, 0x73ff) AM_WRITENOP
	AM_RANGE(0x7400, 0x77ff) AM_RAM_WRITE(dkong_videoram_w) AM_BASE(&videoram)
	AM_RANGE(0x7800, 0x7808) AM_WRITENOP
	AM_RANGE(0x7c00, 0x7c00) AM_READWRITE(dkonghs_eeprom_r,dkongjr_sh_tuneselect_w)
	AM_RANGE(0x7c80, 0x7c80) AM_READWRITE(input_port_1_r,dkongjr_gfxbank_w)	/* IN1 */
	AM_RANGE(0x7c81, 0x7c81) AM_WRITE(dkongjr_sh_test6_w)
	AM_RANGE(0x7c82, 0x7c82) AM_WRITE(dkjrhs_eeprom_clock_w)
	AM_RANGE(0x7c83, 0x7c83) AM_WRITE(dkonghs_eeprom_data_w)
	AM_RANGE(0x7c87, 0x7c87) AM_WRITE(dkonghs_eeprom_cs_w)
	AM_RANGE(0x7d00, 0x7d00) AM_READWRITE(dkong_in2_r,dkongjr_sh_climb_w) /* HC - climb sound */
	AM_RANGE(0x7d01, 0x7d01) AM_WRITE(dkongjr_sh_jump_w) /* HC - jump */
	AM_RANGE(0x7d02, 0x7d02) AM_WRITE(dkongjr_sh_land_w) /* HC - climb sound */
	AM_RANGE(0x7d03, 0x7d03) AM_WRITE(dkongjr_sh_roar_w)
	AM_RANGE(0x7d04, 0x7d04) AM_WRITE(dkong_sh_sound4_w)
	AM_RANGE(0x7d05, 0x7d05) AM_WRITE(dkong_sh_sound5_w)
	AM_RANGE(0x7d06, 0x7d06) AM_WRITE(dkongjr_sh_snapjaw_w)
	AM_RANGE(0x7d07, 0x7d07) AM_WRITE(dkongjr_sh_walk_w)	/* controls pitch of the walk/climb? */
	AM_RANGE(0x7d80, 0x7d80) AM_READWRITE(input_port_3_r,dkongjr_sh_death_w)
	AM_RANGE(0x7d81, 0x7d81) AM_WRITE(dkongjr_sh_drop_w)   /* active when Junior is falling */
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITENOP
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_WRITENOP
	AM_RANGE(0x7d86, 0x7d87) AM_WRITE(dkong_palettebank_w)
ADDRESS_MAP_END

INPUT_PORTS_START( dkongex )
	PORT_INCLUDE( dkong )
	PORT_MODIFY("DSW0")
	PORT_DIPNAME( 0x70, 0x00, DEF_STR( Coinage ) )
	PORT_DIPSETTING(    0x70, "Freeplay" )
	PORT_DIPSETTING(    0x50, DEF_STR( 4C_1C ) )
	PORT_DIPSETTING(    0x30, DEF_STR( 3C_1C ) )
	PORT_DIPSETTING(    0x10, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x20, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x40, DEF_STR( 1C_3C ) )
	PORT_DIPSETTING(    0x60, DEF_STR( 1C_4C ) )
INPUT_PORTS_END

static MACHINE_DRIVER_START( dkonghs )
	MDRV_IMPORT_FROM(dkong)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(dkonghs_map,0)
	MDRV_NVRAM_HANDLER(dkonghs)
MACHINE_DRIVER_END

static MACHINE_DRIVER_START( dkjrhs )
	MDRV_IMPORT_FROM(dkongjr)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(dkjrhs_map,0)
	MDRV_NVRAM_HANDLER(dkonghs)
MACHINE_DRIVER_END

ROM_START( dkjrhs )
	ROM_REGION( 0x10000, "main", 0 )	/* 64k for code */
	ROM_LOAD( "dkjrhs.5b",    0x0000, 0x1000, CRC(584880cb) SHA1(e5c0f62c90aee385efa820a4975e362e0ce25b90) )
	ROM_CONTINUE(             0x3000, 0x1000 )
	ROM_LOAD( "dkjrhs.5c",    0x2000, 0x0800, CRC(dc321058) SHA1(7bf0f9fb6a5916e7c14545483a1ecceef51996f3) )
	ROM_CONTINUE(             0x4800, 0x0800 )
	ROM_CONTINUE(             0x1000, 0x0800 )
	ROM_CONTINUE(             0x5800, 0x0800 )
	ROM_LOAD( "dkj.5e",       0x4000, 0x0800, CRC(d042b6a8) SHA1(57ac237d273496b44220b4437118115ef11dbd9f) )
	ROM_CONTINUE(             0x2800, 0x0800 )
	ROM_CONTINUE(             0x5000, 0x0800 )
	ROM_CONTINUE(             0x1800, 0x0800 )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "c_3h.bin",       0x0000, 0x1000, CRC(715da5f8) SHA1(f708c3fd374da65cbd9fe2e191152f5d865414a0) )

	ROM_REGION( 0x2000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "dkj.3n",       0x0000, 0x1000, CRC(8d51aca9) SHA1(64887564b079d98e98aafa53835e398f34fe4e3f) )
	ROM_LOAD( "dkj.3p",       0x1000, 0x1000, CRC(4ef64ba5) SHA1(41a7a4005087951f57f62c9751d62a8c495e6bb3) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "v_7c.bin",     0x0000, 0x0800, CRC(dc7f4164) SHA1(07a6242e95b5c3b8dfdcd4b4950f463dba16dd77) )
	ROM_LOAD( "v_7d.bin",     0x0800, 0x0800, CRC(0ce7dcf6) SHA1(0654b77526c49f0dfa077ac4f1f69cf5cb2e2f64) )
	ROM_LOAD( "v_7e.bin",     0x1000, 0x0800, CRC(24d1ff17) SHA1(696854bf3dc5447d33b4815db357e6ce3834d867) )
	ROM_LOAD( "v_7f.bin",     0x1800, 0x0800, CRC(0f8c083f) SHA1(0b688ae9da296b2447fffa5e135fd6a56ec3e790) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2e.bpr",     0x0000, 0x0100, CRC(463dc7ad) SHA1(b2c9f22facc8885be2d953b056eb8dcddd4f34cb) )	/* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2f.bpr",     0x0100, 0x0100, CRC(47ba0042) SHA1(dbec3f4b8013628c5b8f83162e5f8b1f82f6ee5f) )	/* palette high 4 bits (inverted) */
	ROM_LOAD( "v-2n.bpr",     0x0200, 0x0100, CRC(dbf185bf) SHA1(2697a991a4afdf079dd0b7e732f71c7618f43b70) )	/* character color codes on a per-column basis */
ROM_END

ROM_START( dkongex )
	ROM_REGION( 0x10000, "main", 0 )	/* 64k for code */
	ROM_LOAD( "cx_5et_g.bin", 0x0000, 0x1000, CRC(00b7efaf) SHA1(97ed5930eb5d0cb98a9008b1d329ba7f3b8b8dbf) )
	ROM_LOAD( "cx_5ct_g.bin", 0x1000, 0x1000, CRC(88af9b69) SHA1(c5621dd8198c333b3fa32fdece60ee5a3d8b2276) )
	ROM_LOAD( "cx_5bt_g.bin", 0x2000, 0x1000, CRC(de74ad91) SHA1(c80227361bdbc565e9f5764e6364b52d40ca778a) )
	ROM_LOAD( "cx_5at_g.bin", 0x3000, 0x1000, CRC(9fa3e5b7) SHA1(5c34170f762a147d5285d17d6689fd64dab99ff2) )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "v_5h_b.bin",   0x0000, 0x0800, CRC(12c8c95d) SHA1(a57ff5a231c45252a63b354137c920a1379b70a3) )
	ROM_LOAD( "v_3pt.bin",    0x0800, 0x0800, CRC(15e9c5e9) SHA1(976eb1e18c74018193a35aa86cff482ebfc5cc4e) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "l_4m_b.bin",   0x0000, 0x0800, CRC(59f8054d) SHA1(793dba9bf5a5fe76328acdfb90815c243d2a65f1) )
	ROM_LOAD( "l_4n_b.bin",   0x0800, 0x0800, CRC(672e4714) SHA1(92e5d379f4838ac1fa44d448ce7d142dae42102f) )
	ROM_LOAD( "l_4r_b.bin",   0x1000, 0x0800, CRC(feaa59ee) SHA1(ecf95db5a20098804fc8bd59232c66e2e0ed3db4) )
	ROM_LOAD( "l_4s_b.bin",   0x1800, 0x0800, CRC(20f2ef7e) SHA1(3bc482a38bf579033f50082748ee95205b0f673d) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "v-5e.bpr",  	  0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) ) /* character color codes on a per-column basis */
ROM_END

ROM_START( dkonghs )
	ROM_REGION( 0x10000, "main", 0 )	/* 64k for code */
	ROM_LOAD( "ckonghs.5e",   0x0000, 0x1000, CRC(8839a8ef) SHA1(f1517e889c163bd4c1a57f065b7a8750a7d6934c) )
	ROM_LOAD( "ckonghs.5c",   0x1000, 0x1000, CRC(37708ece) SHA1(b1195f773f336f181e9b4399dc73029525927964) )
	ROM_LOAD( "c_5bt_g.bin",  0x2000, 0x1000, CRC(1c97d324) SHA1(c7966261f3a1d3296927e0b6ee1c58039fc53c1f) )
	ROM_LOAD( "ckonghs.5a",   0x3000, 0x1000, CRC(fb53673f) SHA1(5c846ea3ca67558513061b1a15ec77872ac87993) )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "v_5h_b.bin",   0x0000, 0x0800, CRC(12c8c95d) SHA1(a57ff5a231c45252a63b354137c920a1379b70a3) )
	ROM_LOAD( "v_3pt.bin",    0x0800, 0x0800, CRC(15e9c5e9) SHA1(976eb1e18c74018193a35aa86cff482ebfc5cc4e) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "l_4m_b.bin",   0x0000, 0x0800, CRC(59f8054d) SHA1(793dba9bf5a5fe76328acdfb90815c243d2a65f1) )
	ROM_LOAD( "l_4n_b.bin",   0x0800, 0x0800, CRC(672e4714) SHA1(92e5d379f4838ac1fa44d448ce7d142dae42102f) )
	ROM_LOAD( "l_4r_b.bin",   0x1000, 0x0800, CRC(feaa59ee) SHA1(ecf95db5a20098804fc8bd59232c66e2e0ed3db4) )
	ROM_LOAD( "l_4s_b.bin",   0x1800, 0x0800, CRC(20f2ef7e) SHA1(3bc482a38bf579033f50082748ee95205b0f673d) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "v-5e.bpr",     0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) ) /* character color codes on a per-column basis */
ROM_END

/* This set has the fixes proposed by Don Hodges at the page:
	http://donhodges.com/how_high_can_you_get.htm */
ROM_START( dkongp )
	ROM_REGION( 0x10000, "main", 0 )
	ROM_LOAD( "c_5et_g.bin",  0x0000, 0x1000, CRC(ba70b88b) SHA1(d76ebecfea1af098d843ee7e578e480cd658ac1a) )
	ROM_LOAD( "c_5ct_g.bin",  0x1000, 0x1000, CRC(5ec461ec) SHA1(acb11a8fbdbb3ab46068385fe465f681e3c824bd) )
	ROM_LOAD( "c_5bt_g.bin",  0x2000, 0x1000, CRC(1c97d324) SHA1(c7966261f3a1d3296927e0b6ee1c58039fc53c1f) )
	ROM_LOAD( "c_5at_g.bin",  0x3000, 0x1000, CRC(b9005ac0) SHA1(3fe3599f6fa7c496f782053ddf7bacb453d197c4) )
	ROM_FILL(0x0f7d,1,0xfe)
	ROM_FILL(0x0f7e,1,0x04)
	ROM_FILL(0x0f7f,1,0x38)
	ROM_FILL(0x0f80,1,0x02)
	ROM_FILL(0x0f81,1,0x3e)
	ROM_FILL(0x0f82,1,0x04)
	ROM_FILL(0x0f83,1,0x47)
	ROM_FILL(0x0f84,1,0xa7)
	ROM_FILL(0x0f85,1,0x17)
	ROM_FILL(0x0f86,1,0xa7)
	ROM_FILL(0x0f87,1,0x17)
	ROM_FILL(0x0f88,1,0xa7)
	ROM_FILL(0x0f89,1,0x17)
	ROM_FILL(0x0f8a,1,0x80)
	ROM_FILL(0x0f8b,1,0x80)
	ROM_FILL(0x0f8c,1,0xc6)
	ROM_FILL(0x0f8d,1,0x28)

	ROM_REGION( 0x1000, "audio", 0 )
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "v_5h_b.bin",   0x0000, 0x0800, CRC(12c8c95d) SHA1(a57ff5a231c45252a63b354137c920a1379b70a3) )
	ROM_LOAD( "v_3pt.bin",    0x0800, 0x0800, CRC(15e9c5e9) SHA1(976eb1e18c74018193a35aa86cff482ebfc5cc4e) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "l_4m_b.bin",   0x0000, 0x0800, CRC(59f8054d) SHA1(793dba9bf5a5fe76328acdfb90815c243d2a65f1) )
	ROM_LOAD( "l_4n_b.bin",   0x0800, 0x0800, CRC(672e4714) SHA1(92e5d379f4838ac1fa44d448ce7d142dae42102f) )
	ROM_LOAD( "l_4r_b.bin",   0x1000, 0x0800, CRC(feaa59ee) SHA1(ecf95db5a20098804fc8bd59232c66e2e0ed3db4) )
	ROM_LOAD( "l_4s_b.bin",   0x1800, 0x0800, CRC(20f2ef7e) SHA1(3bc482a38bf579033f50082748ee95205b0f673d) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) )
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) )
	ROM_LOAD( "v-5e.bpr",     0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) )
ROM_END

ROM_START( kong2600 )
	ROM_REGION( 0x10000, "main", 0 )	/* 64k for code */
	ROM_LOAD( "c_5et_g.bin",  0x0000, 0x1000, CRC(ba70b88b) SHA1(d76ebecfea1af098d843ee7e578e480cd658ac1a) )
	ROM_LOAD( "c_5ct_g.bin",  0x1000, 0x1000, CRC(5ec461ec) SHA1(acb11a8fbdbb3ab46068385fe465f681e3c824bd) )
	ROM_LOAD( "c_5bt_g.bin",  0x2000, 0x1000, CRC(1c97d324) SHA1(c7966261f3a1d3296927e0b6ee1c58039fc53c1f) )
	ROM_LOAD( "c_5at_g.bin",  0x3000, 0x1000, CRC(b9005ac0) SHA1(3fe3599f6fa7c496f782053ddf7bacb453d197c4) )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "k2600.3n",     0x0000, 0x0800, CRC(0e6a2a6d) SHA1(7ebbcb78b269184cf588b5ad0b90f449d6d4e299) )
	ROM_LOAD( "k2600.3p",     0x0800, 0x0800, CRC(ca57e0f4) SHA1(12c7821fc9f7fee276f7aa27dd0421f565b0f469) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "k2600.7c",     0x0000, 0x0800, CRC(cf450a43) SHA1(efa772f92b890181c0823b8113e069b7c4400ede) )
	ROM_LOAD( "k2600.7d",     0x0800, 0x0800, CRC(d5046907) SHA1(1eb08ed67f6de04c4f3cb8f449503bbedc6207bc) )
	ROM_LOAD( "k2600.7e",     0x1000, 0x0800, CRC(1539fe2a) SHA1(ea599987a2d65ffe2d09ca0951529bde2d31b1be) )
	ROM_LOAD( "k2600.7f",     0x1800, 0x0800, CRC(77cc00ab) SHA1(7560bf4fd65c03f28f015abf64f83fc4ad7ae512) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "k2600.2k",     0x0000, 0x0100, CRC(1e82d375) SHA1(8f2da5cedd1f62be08555dd0eb929eb41da5079d) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "k2600.2j",     0x0100, 0x0100, CRC(2ab01dc8) SHA1(c25958d0706cabf997efe16fad71f454fc1ced0b) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "k2600.5f",     0x0200, 0x0100, CRC(44988665) SHA1(68c474fc81aff46eae6c9a7ac6ab80288303e291) ) /* character color codes on a per-column basis */
ROM_END

ROM_START( nadkong )
	ROM_REGION( 0x10000, "main", 0 )	/* 64k for code */
	ROM_LOAD( "c_5et_g.bin",  0x0000, 0x1000, CRC(ba70b88b) SHA1(d76ebecfea1af098d843ee7e578e480cd658ac1a) )
	ROM_LOAD( "c_5ct_g.bin",  0x1000, 0x1000, CRC(5ec461ec) SHA1(acb11a8fbdbb3ab46068385fe465f681e3c824bd) )
	ROM_LOAD( "c_5bt_g.bin",  0x2000, 0x1000, CRC(1c97d324) SHA1(c7966261f3a1d3296927e0b6ee1c58039fc53c1f) )
	ROM_LOAD( "c_5at_g.bin",  0x3000, 0x1000, CRC(b9005ac0) SHA1(3fe3599f6fa7c496f782053ddf7bacb453d197c4) )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",	  0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "nadkong.5h",   0x0000, 0x0800, CRC(88772f5b) SHA1(0fd6cbb593e87b863eb317b063151ef3b81565c2) )
	ROM_LOAD( "nadkong.3p",   0x0800, 0x0800, CRC(4c8aa728) SHA1(a277988d48f0d2f5d7cf9ef05eef222a36248cd3) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "nadkong.4m",   0x0000, 0x0800, CRC(2afbf9e6) SHA1(51e4f2f67903158b77574d55fcd17f302bc04a52) )
	ROM_LOAD( "nadkong.4n",   0x0800, 0x0800, CRC(4f077877) SHA1(4f2c2bc693b0e8d36114024b6eaeb37a3b5d853d) )
	ROM_LOAD( "nadkong.4r",   0x1000, 0x0800, CRC(d868da93) SHA1(c1c5d21f8c6a057fd5f54cab1eb4dbef06120405) )
	ROM_LOAD( "nadkong.4s",   0x1800, 0x0800, CRC(7ebfe9f3) SHA1(75bd16e9590ada93b061dee3ec7916e1e1f113c3) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "v-5e.bpr",  	  0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) ) /* character color codes on a per-column basis */
ROM_END

GAME( 2001, dkjrhs,   0,        dkjrhs,   dkong,    0,        ROT90, "JRok", "Donkey Kong Junior [h]", GAME_SUPPORTS_SAVE )
GAME( 2005, dkongex,  dkong,    dkong,    dkongex,  0,        ROT90, "Jeff Kulczycki", "Donkey Kong Foundry", GAME_SUPPORTS_SAVE )
GAME( 2007, dkongp,   dkong,    dkong,    dkong,    0,        ROT90, "Don Hodges", "Donkey Kong (patched)", GAME_SUPPORTS_SAVE )
GAME( 2001, dkonghs,  dkong,    dkonghs,  dkong,    0,        ROT90, "JRok", "Donkey Kong [h]", GAME_SUPPORTS_SAVE )
GAME( 1999, kong2600, dkong,    dkong,	  dkong,    0,        ROT90, "Vic Twenty George", "Donkey Kong (2600 graphics)", GAME_SUPPORTS_SAVE )
GAME( 2004, nadkong,  dkong,    dkong,    dkong,    0,        ROT90, "Hack", "Naked Donkey Kong", GAME_SUPPORTS_SAVE )


/**************************************************************
 *
 *  Donkey Kong II driver, by Robbbert, October/November 2008
 *
 *  Thanks to assistance, testing, dumps and kind words from
 *  the entire MisfitMAME community. :)
 *
 **************************************************************/


/*************************************
 *
 *  DMA handling
 *
 *************************************/
#define CLOCK_1H			(MASTER_CLOCK / 5 / 4)
#define HARDWARE_TYPE_TAG		"HARDWARE_TYPE"
#define HARDWARE_TKG04			0

static UINT8 dma_latch = 0;

static READ8_DEVICE_HANDLER( p8257_ctl_r )
{
	return dma_latch;
}

static WRITE8_DEVICE_HANDLER( p8257_ctl_w )
{
	dma_latch = data;
}

static WRITE8_DEVICE_HANDLER( p8257_drq_w )
{
	dma8257_drq_w(device, 0, data & 0x01);
	dma8257_drq_w(device, 1, data & 0x01);
}

static READ8_DEVICE_HANDLER( dk_dma_read_byte )
{
	UINT8 result;

	cpuintrf_push_context(0);
	result = program_read_byte(offset);
	cpuintrf_pop_context();

	return result;
}

static WRITE8_DEVICE_HANDLER( dk_dma_write_byte )
{
	cpuintrf_push_context(0);
	program_write_byte(offset, data);
	cpuintrf_pop_context();
}

static const dma8257_interface dk_dma =
{
	0,
	CLOCK_1H,

	dk_dma_read_byte,
	dk_dma_write_byte,

	{ 0, p8257_ctl_r, 0, 0 },
	{ p8257_ctl_w, 0, 0, 0 },
	{ 0, 0, 0, 0 }
};

/*************************************
 *
 *  93C46 EEPROM (8-bit version)
 *
 *************************************/
/* The initial settings are determined from the dipswitches.
	After successful initialisation (first run), the switches
	are overridden by the eeprom.
	Cocktail mode is always controlled by the dip. */

const eeprom_interface eeprom_interface_d2k =
{
	7,				// address bits 7
	8,				// data bits    8
	"*110",			// read         1 10 aaaaaa
	"*101",			// write        1 01 aaaaaa dddddddddddddddd
	"*111",			// erase        1 11 aaaaaa
	"*10000xxxx",	// lock         1 00 00xxxx
	"*10011xxxx",	// unlock       1 00 11xxxx
	1,
//  "*10001xxxx"    // write all    1 00 01xxxx dddddddddddddddd
//  "*10010xxxx"    // erase all    1 00 10xxxx
};

NVRAM_HANDLER( d2k )
{
	if (read_or_write)
		eeprom_save(file);
	else
	{
		eeprom_init(&eeprom_interface_d2k);
		if (file)	eeprom_load(file);
	}
}

static READ8_HANDLER( d2k_eeprom_r )
{
	return eeprom_read_bit();
}

static WRITE8_HANDLER( d2k_eeprom_w )
{
	eeprom_write_bit(data & 1);
	eeprom_set_clock_line( (data & 2) ? ASSERT_LINE : CLEAR_LINE );
	eeprom_set_cs_line( (data & 4) ? CLEAR_LINE : ASSERT_LINE );
}


/*************************************
 *
 *  Bankswitching ( by e801 and e80c)
 *
 *************************************/

static WRITE8_HANDLER( d2k_rombank_w )
{
	memory_set_bank(1, data & 1 );
	memory_set_bank(2, data & 1 );
}


/*************************************
 *
 *  Address Map
 *
 *************************************/

static ADDRESS_MAP_START( d2k_map, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x5fff) AM_ROMBANK(1)
	AM_RANGE(0x0007, 0x0007) AM_WRITENOP
	AM_RANGE(0x6000, 0x68ff) AM_RAM
	AM_RANGE(0x6900, 0x6a7f) AM_RAM AM_BASE(&spriteram) AM_SIZE(&spriteram_size)
	AM_RANGE(0x6a80, 0x73ff) AM_RAM
	AM_RANGE(0x7400, 0x77ff) AM_RAM_WRITE(dkong_videoram_w) AM_BASE(&videoram)
	AM_RANGE(0x7800, 0x780f) AM_DEVREADWRITE(DMA8257, "dma8257", dma8257_r, dma8257_w)	/* P8257 control registers */
	AM_RANGE(0x7c00, 0x7c00) AM_READ_PORT("IN0") AM_WRITE(dkong_sh_tuneselect_w)	/* IN0 */
	AM_RANGE(0x7c80, 0x7c80) AM_READ_PORT("IN1")
	AM_RANGE(0x7d00, 0x7d00) AM_READ_PORT("IN2")
	AM_RANGE(0x7d00, 0x7d02) AM_WRITE(dkong_sh1_w)					/* walk/jump/boom sample trigger */
	AM_RANGE(0x7d03, 0x7d03) AM_WRITE(dkong_sh_sound3_w)
	AM_RANGE(0x7d04, 0x7d04) AM_WRITE(dkong_sh_sound4_w)
	AM_RANGE(0x7d05, 0x7d05) AM_WRITE(dkong_sh_sound5_w)
	AM_RANGE(0x7d06, 0x7d07) AM_WRITENOP						/* unemulated sounds */
	AM_RANGE(0x7d80, 0x7d80) AM_READ_PORT("DSW0") AM_WRITE(dkong_sh_w)		/* DSW0 */
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITENOP				// AM_WRITE(dkong_spritebank_w) - not emulated yet
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_DEVWRITE(DMA8257, "dma8257", p8257_drq_w)		/* P8257 ==> /DRQ0 /DRQ1 */
	AM_RANGE(0x7d86, 0x7d87) AM_WRITE(dkong_palettebank_w)
	AM_RANGE(0xc000, 0xc000) AM_READNOP
	AM_RANGE(0xc800, 0xc800) AM_READWRITE(d2k_eeprom_r, d2k_eeprom_w)
	AM_RANGE(0xd000, 0xdfff) AM_ROM
	AM_RANGE(0xe000, 0xe000) AM_WRITE(d2k_rombank_w)
	AM_RANGE(0xe800, 0xffff) AM_ROMBANK(2)
ADDRESS_MAP_END

/*************************************
 *
 *  Machine Driver
 *
 *************************************/
static MACHINE_DRIVER_START( d2k )
	MDRV_IMPORT_FROM(dkong)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(d2k_map,0)
	MDRV_DEVICE_ADD("dma8257", DMA8257)
	MDRV_DEVICE_CONFIG(dk_dma)
	MDRV_NVRAM_HANDLER(d2k)
	MDRV_SCREEN_MODIFY("main")
	MDRV_SCREEN_VISIBLE_AREA(0*8, 32*8-1, 2*8, 30*8-1)	// needed to disguise a bug in the test screen
MACHINE_DRIVER_END

/*************************************
 *
 *  Driver Initialisation
 *
 *************************************/
static DRIVER_INIT( d2k )
{
	UINT8 *ROM = memory_region(machine, "main");
	int i, m, n;

	/* Fix data lines */
	for( i = 0x20000; i < 0x30000; i++ )
		ROM[i] = BITSWAP8( ROM[i], 1, 4, 5, 7, 6, 0, 3, 2 );

	/* Fix address lines */
	for (i = 0; i < 0x100; i++)
	{
		m = (BITSWAP8( i, 7, 2, 0, 1, 5, 6, 4, 3 ) << 8) | 0x20000;
		n = (i << 8) | 0x10000;
		memcpy( &ROM[n], &ROM[m], 0x100);
	}

	/* the lines below are to fix a bug that appears in the test screen if the width is set any wider,
		but adding this fix breaks the rom checksum. So, cheated by making the screen narrower. */
//	ROM[0x14e69] = 0;	// fix error in clear-screen routine - v1.2
//	ROM[0x14e77] = 0;	// fix error in clear-screen routine - v1.1

	memcpy( &ROM[0xd000], &ROM[0x1d000], 0x1000);		// mirror for E830 trickery

	memory_configure_bank(1, 0, 2, &ROM[0x10000], 0x8000);	// rom at 0-0x5fff is banked (dk or d2k)
	memory_configure_bank(2, 0, 2, &ROM[0x16800], 0x8000);	// 2nd bank

	d2k_rombank_w( machine, 0, 0);				// select dk at bootup
}


/*************************************
 *
 *  Roms
 *
 *************************************/
ROM_START( d2k )
	ROM_REGION( 0x30000, "main", 0 )
	ROM_LOAD( "d2k.bin",      0x20000, 0x10000, CRC(6e95ca0d) SHA1(c058add0f146d577e3df0ba60828fe1734e78d01) )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "v_5h_b.bin",   0x0000, 0x0800, CRC(12c8c95d) SHA1(a57ff5a231c45252a63b354137c920a1379b70a3) )
	ROM_LOAD( "v_3pt.bin",    0x0800, 0x0800, CRC(15e9c5e9) SHA1(976eb1e18c74018193a35aa86cff482ebfc5cc4e) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "l_4m_b.bin",   0x0000, 0x0800, CRC(59f8054d) SHA1(793dba9bf5a5fe76328acdfb90815c243d2a65f1) )
	ROM_LOAD( "l_4n_b.bin",   0x0800, 0x0800, CRC(672e4714) SHA1(92e5d379f4838ac1fa44d448ce7d142dae42102f) )
	ROM_LOAD( "l_4r_b.bin",   0x1000, 0x0800, CRC(feaa59ee) SHA1(ecf95db5a20098804fc8bd59232c66e2e0ed3db4) )
	ROM_LOAD( "l_4s_b.bin",   0x1800, 0x0800, CRC(20f2ef7e) SHA1(3bc482a38bf579033f50082748ee95205b0f673d) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "v-5e.bpr",     0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) ) /* character color codes on a per-column basis */
ROM_END

ROM_START( d2k11 )
	ROM_REGION( 0x30000, "main", 0 )
	ROM_LOAD( "d2k11.bin",    0x20000, 0x10000, CRC(2048fc42) SHA1(e427a09ed8e792ee8ce01cd0b07c6a0d5a7c5536) )

	ROM_REGION( 0x1000, "audio", 0 )	/* sound */
	ROM_LOAD( "s_3i_b.bin",   0x0000, 0x0800, CRC(45a4ed06) SHA1(144d24464c1f9f01894eb12f846952290e6e32ef) )
	ROM_LOAD( "s_3j_b.bin",   0x0800, 0x0800, CRC(4743fe92) SHA1(6c82b57637c0212a580591397e6a5a1718f19fd2) )

	ROM_REGION( 0x1000, "gfx1", ROMREGION_DISPOSE )
	ROM_LOAD( "v_5h_b.bin",   0x0000, 0x0800, CRC(12c8c95d) SHA1(a57ff5a231c45252a63b354137c920a1379b70a3) )
	ROM_LOAD( "v_3pt.bin",    0x0800, 0x0800, CRC(15e9c5e9) SHA1(976eb1e18c74018193a35aa86cff482ebfc5cc4e) )

	ROM_REGION( 0x2000, "gfx2", ROMREGION_DISPOSE )
	ROM_LOAD( "l_4m_b.bin",   0x0000, 0x0800, CRC(59f8054d) SHA1(793dba9bf5a5fe76328acdfb90815c243d2a65f1) )
	ROM_LOAD( "l_4n_b.bin",   0x0800, 0x0800, CRC(672e4714) SHA1(92e5d379f4838ac1fa44d448ce7d142dae42102f) )
	ROM_LOAD( "l_4r_b.bin",   0x1000, 0x0800, CRC(feaa59ee) SHA1(ecf95db5a20098804fc8bd59232c66e2e0ed3db4) )
	ROM_LOAD( "l_4s_b.bin",   0x1800, 0x0800, CRC(20f2ef7e) SHA1(3bc482a38bf579033f50082748ee95205b0f673d) )

	ROM_REGION( 0x0300, "proms", 0 )
	ROM_LOAD( "c-2k.bpr",     0x0000, 0x0100, CRC(e273ede5) SHA1(b50ec9e1837c00c20fb2a4369ec7dd0358321127) ) /* palette low 4 bits (inverted) */
	ROM_LOAD( "c-2j.bpr",     0x0100, 0x0100, CRC(d6412358) SHA1(f9c872da2fe8e800574ae3bf483fb3ccacc92eb3) ) /* palette high 4 bits (inverted) */
	ROM_LOAD( "v-5e.bpr",     0x0200, 0x0100, CRC(b869b8f5) SHA1(c2bdccbf2654b64ea55cd589fd21323a9178a660) ) /* character color codes on a per-column basis */
ROM_END

/*******************************************************
 *
 *  Game Driver - savestate support is unlikely to work
 *
 *******************************************************/
GAME( 2006, d2k,   dkong, d2k, dkong,  d2k,   ROT90, "Jeff Kulczycki", "Donkey Kong II: Jumpman Returns (v1.2) [h]", GAME_SUPPORTS_SAVE )
GAME( 2006, d2k11, dkong, d2k, dkong,  d2k,   ROT90, "Jeff Kulczycki", "Donkey Kong II: Jumpman Returns (v1.1) [h]", GAME_SUPPORTS_SAVE )

/* Misfitmame end */

