#include "driver.h"
#include "cpu/i8039/i8039.h"
#include "sound/dac.h"
#include "sound/samples.h"
#include "includes/dkong.h"
#include <math.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)*256+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, 2*8, 30*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 */

/* Donkey Kong high scores are saved in a 93c66 serial nvram, it is connected to the existing 74LS240 which causes
   the output data to be inverted. This nvram is organised as 256 x 16 bits. The data when written will actually be
   saved in the dummy nvram area in the memory map. The 93c66 device is emulated here in full.
Pin 1 = CS = Chip Select = 0x7d81 (bit 0) [0x7c87=dkjrhs]
Pin 2 = SK = Clock = 0x7d06 (bit 0) [0x7c82]
Pin 3 = DI = Data In = 0x7d07 (bit 0) [0x7c83]
Pin 4 = DO = Data Out = 0x7c00 (bit 7) */

static UINT8 m93c66_state = 0;
/*
bit 0 - current data
bit 1 - clock state [SK]
bit 2 - chip select [CS]
bit 3 - write enabled
bit 4 - data waiting to be clocked in [DI]
bit 7 = data waiting to be read out [DO]
*/

static UINT8 m93c66_mode = 0;			/* operation mode */
/*
1 = EWDS  = Make device read only
2 = EWEN  = Make device write-enabled
3 = READ  = Read a word
4 = WRAL  = Write a word to all addresses
5 = ERAL  = Set everything to all ones (erase device)
6 = WRITE = Write a word
7 = ERASE = Set a word to all ones */

static UINT8 m93c66_bits = 0;		/* counts the bits as they get passed */
static UINT32 m93c66_word = 0;
/*
Bits 0 to 15 = Data
Bits 16 to 23 = Offset
Bits 24 to 25 = Operation Mode */

/* This is what happens when a low-to-high clock pulse is received, and the chip is enabled */
static void m93c66_process_data( void )
{
	UINT8 address;
	UINT8 data = (m93c66_state & 0x10) >> 4;  /* DI */

	if ((m93c66_bits > 26) && (data))
	{
		m93c66_bits--;				/* start bit required before any data is accepted */
		m93c66_word = 0;
		m93c66_mode = 0;
	}
	else
	if (m93c66_bits > 16)				/* Process address */
	{
		m93c66_bits--;
		m93c66_word |= data << m93c66_bits;

		/* once 10 bits are in, need to determine device mode and take appropriate action */
		if (m93c66_bits == 16)
		{
			switch (m93c66_word & 0x3000000)
			{
				case 0x3000000:
					m93c66_mode = 7;	/* ERASE */
					m93c66_word |= 0xffff;
					break;
				case 0x2000000:
					m93c66_mode = 3;	/* READ */
					m93c66_state &= 0x7f;	/* dummy low bit at start of reading */
					address = (m93c66_word & 0xff0000) >> 16;
					m93c66_word |= ((generic_nvram[address+0x100] << 8) | generic_nvram[address]);
					break;
				case 0x1000000:
					m93c66_mode = 6;	/* WRITE */
					break;
				case 0:
					switch (m93c66_word & 0xc00000)
					{
						case 0xc00000:
							m93c66_mode = 2;	/* EWEN */
							m93c66_state |= 8;
							break;
						case 0x800000:
							m93c66_mode = 5;	/* ERAL */
							m93c66_word |= 0xffff;
							break;
						case 0x400000:
							m93c66_mode = 4;	/* WRAL */
							break;
						case 0:
							m93c66_mode = 1;	/* EWDS */
							m93c66_state &= 0xf7;
					}
			}
		}
	}
	else
	if ((m93c66_bits) && (m93c66_mode == 3))		/* READ */
	{
		m93c66_bits--;
		data = (m93c66_word & (1 << m93c66_bits)) >> m93c66_bits; /* get a bit and rotate to bit 0 */
		m93c66_state = (m93c66_state & 0x7f) | (data << 7);	/* send a bit to DO pin */
	}
	else
	if ((m93c66_bits) && ((m93c66_mode & 5) == 4))		/* WRITE, WRAL */
	{
		m93c66_bits--;
		m93c66_word |= (data << m93c66_bits);	/* store incoming data for writing */
	}
}

static WRITE8_HANDLER( m93c66_clock_w )
{
	data &= 1;	/* SK */
	m93c66_state = (m93c66_state & 0xfe) | data;
	if ((m93c66_state & 7) == 5) m93c66_process_data();	/* process data on a low-to-high transition */
	m93c66_state = (m93c66_state & 0xfd) | (data << 1);	/* store clock state */
}

static WRITE8_HANDLER( m93c66_cs_w )
{
	UINT8 address;
	data &= 1;	/* CS */
	m93c66_state = (m93c66_state & 0xfe) | data;

	if ((m93c66_state & 5) == 1)				/* re-enable chip */
	{
		m93c66_bits = 27;
		m93c66_mode = 0;
	}
	else
	if (((m93c66_state & 13) == 12) && (m93c66_mode > 5))	/* disable chip (WRITE, ERASE) causes byte to be written to nvram */
	{
		m93c66_state &= 0x7f;				/* indicate programming is happening */
		address = (m93c66_word & 0xff0000) >> 16;
		generic_nvram[address] = m93c66_word & 0xff;
		generic_nvram[address+0x100] = (m93c66_word & 0xff00) >> 8;
		m93c66_state |= 0x80;				/* indicate programming is complete */
	}
	else
	if (((m93c66_state & 13) == 12) && ((m93c66_mode & 6) == 4)) /* (WRAL,ERAL) write to every address */
	{
		int i;
		m93c66_state &= 0x7f;
		for (i = 0;i < 0x100; i++)
		{
			generic_nvram[i] = m93c66_word & 0xff;
			generic_nvram[i+0x100] = (m93c66_word & 0xff00) >> 8;
		}
		m93c66_state |= 0x80;
	}

	m93c66_state = (m93c66_state & 0xfb) | (data << 2);	/* save CS state */
}

static WRITE8_HANDLER( m93c66_data_w )
{
	data &= 1;	/* DI */
	m93c66_state = (m93c66_state & 0xef) | (data << 4);		/* data gets buffered until clock pulse */
}

static READ8_HANDLER( m93c66_data_r )
{
	/* data is inverted by 74LS240 and becomes bit 7 of IN 0 */
	return (input_port_read(machine,"IN0") & 0x7f) | (~m93c66_state & 0x80);	/* DO */
}

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(m93c66_data_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(m93c66_clock_w)
	AM_RANGE(0x7d07, 0x7d07) AM_WRITE(m93c66_data_w)
	AM_RANGE(0x7d80, 0x7d80) AM_READWRITE(input_port_3_r,dkong_sh_w)	/* DSW1 */
	AM_RANGE(0x7d81, 0x7d81) AM_WRITE(m93c66_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)
	AM_RANGE(0xfe00, 0xffff) AM_RAM AM_BASE(&generic_nvram) AM_SIZE(&generic_nvram_size)
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(m93c66_data_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(m93c66_clock_w)
	AM_RANGE(0x7c83, 0x7c83) AM_WRITE(m93c66_data_w)
	AM_RANGE(0x7c87, 0x7c87) AM_WRITE(m93c66_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)
	AM_RANGE(0xfe00, 0xffff) AM_RAM AM_BASE(&generic_nvram) AM_SIZE(&generic_nvram_size)
ADDRESS_MAP_END

static MACHINE_DRIVER_START( dkonghs )
	MDRV_IMPORT_FROM(dkong)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(dkonghs_map,0)
	MDRV_NVRAM_HANDLER(generic_1fill)
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(generic_1fill)
MACHINE_DRIVER_END

static DRIVER_INIT( dkonghs )
{
	UINT8 *ROM = memory_region(machine, "main");
	ROM[0x3f5a] = 0;	/* patch out call to output another start bit */
	ROM[0x3f5b] = 0;
	ROM[0x3f5c] = 0;
}

static DRIVER_INIT( dkjrhs )
{
	UINT8 *ROM = memory_region(machine, "main");
	ROM[0x3f62] = 0;	/* patch out call to output another start bit */
	ROM[0x3f63] = 0;
	ROM[0x3f64] = 0;
}

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

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,    dkjrhs,   ROT90, "JRok", "Donkey Kong Junior [h]", 0 )
GAME( 1981, dkongex,  dkong,    dkong,    dkong,    0,        ROT90, "Hack", "Donkey Kong (w/ Foundry)", GAME_SUPPORTS_SAVE )
GAME( 2001, dkonghs,  dkong,    dkonghs,  dkong,    dkonghs,  ROT90, "JRok", "Donkey Kong [h]", 0 )
GAME( 1999, kong2600, dkong,    dkong,	  dkong,    0,        ROT90, "Vic Twenty George hack", "Donkey Kong (2600 graphics)", GAME_SUPPORTS_SAVE )
GAME( 2004, nadkong,  dkong,    dkong,    dkong,    0,        ROT90, "Hack", "Naked Donkey Kong", GAME_SUPPORTS_SAVE )

/* Misfitmame end */
