#include "cpu/z80/z80.h"
#include "../mame/drivers/dkong.c"


/* 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_MEMBER(dkong_state, sprite_ram) AM_SIZE_MEMBER(dkong_state, sprite_ram_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_MEMBER(dkong_state, video_ram)
	AM_RANGE(0x7800, 0x7808) AM_WRITENOP
	AM_RANGE(0x7c00, 0x7c00) AM_READ(dkonghs_eeprom_r) AM_LATCH8_WRITE("ls175.3d")
	AM_RANGE(0x7c80, 0x7c80) AM_READ_PORT("IN1")
	AM_RANGE(0x7d00, 0x7d00) AM_READ(dkong_in2_r)	/* IN2/DSW2 */
	AM_RANGE(0x7d00, 0x7d05) AM_DEVWRITE(LATCH8, "ls259.6h", latch8_bit0_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_READ_PORT("DSW0") AM_WRITE(dkong_audio_irq_w)
	AM_RANGE(0x7d81, 0x7d81) AM_WRITE(dkonghs_eeprom_cs_w)
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITE(dkong_spritebank_w)
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_DEVWRITE(DMA8257, "dma8257", p8257_drq_w)
	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_MEMBER(dkong_state, sprite_ram) AM_SIZE_MEMBER(dkong_state, sprite_ram_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_MEMBER(dkong_state, video_ram)
	AM_RANGE(0x7800, 0x7808) AM_WRITENOP
	AM_RANGE(0x7c00, 0x7c00) AM_READ(dkonghs_eeprom_r) AM_LATCH8_WRITE("ls174.3d")
	AM_RANGE(0x7c80, 0x7c80) AM_READ_PORT("IN1") AM_WRITE(dkongjr_gfxbank_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_READ(dkong_in2_r)
	AM_RANGE(0x7d00, 0x7d07) AM_DEVWRITE(LATCH8, "ls259.6h",latch8_bit0_w)
	AM_RANGE(0x7d80, 0x7d80) AM_READ_PORT("DSW0") AM_WRITE(dkong_audio_irq_w)
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITE(dkong_spritebank_w)
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_DEVWRITE(DMA8257, "dma8257", p8257_drq_w)
	AM_RANGE(0x7d86, 0x7d87) AM_WRITE(dkong_palettebank_w)
	AM_RANGE(0x7d80, 0x7d87) AM_DEVWRITE(LATCH8, "ls259.5h", latch8_bit0_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(dkong2b)
	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, "sound", 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

/* same as mame 'dkongf' except rom cx_5at_g.bin is different */
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, "sound", 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, "sound", 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, "sound", 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, "sound", 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, "sound", 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,    dkong2b,  dkongex,  0,        ROT90, "Jeff Kulczycki", "Donkey Kong Foundry", GAME_SUPPORTS_SAVE )
GAME( 2007, dkongp,   dkong,    dkong2b,  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,    dkong2b,  dkong,    0,        ROT90, "Vic Twenty George", "Donkey Kong (2600 graphics)", GAME_SUPPORTS_SAVE )
GAME( 2004, nadkong,  dkong,    dkong2b,  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. :)
 *
 **************************************************************/



/*************************************
 *
 *  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_MEMBER(dkong_state, sprite_ram) AM_SIZE_MEMBER(dkong_state, sprite_ram_size)
	AM_RANGE(0x6a80, 0x73ff) AM_RAM
	AM_RANGE(0x7400, 0x77ff) AM_RAM_WRITE(dkong_videoram_w) AM_BASE_MEMBER(dkong_state, video_ram)
	AM_RANGE(0x7800, 0x780f) AM_DEVREADWRITE(DMA8257, "dma8257", dma8257_r, dma8257_w)	/* P8257 control registers */
	AM_RANGE(0x7c00, 0x7c00) AM_READ_PORT("IN0") AM_LATCH8_WRITE("ls175.3d")
	AM_RANGE(0x7c80, 0x7c80) AM_READ_PORT("IN1")
	AM_RANGE(0x7d00, 0x7d00) AM_READ_PORT("IN2")
	AM_RANGE(0x7d00, 0x7d07) AM_DEVWRITE(LATCH8, "ls259.6h", latch8_bit0_w)
	AM_RANGE(0x7d80, 0x7d80) AM_READ_PORT("DSW0") AM_WRITE(dkong_audio_irq_w)
	AM_RANGE(0x7d82, 0x7d82) AM_WRITE(dkong_flipscreen_w)
	AM_RANGE(0x7d83, 0x7d83) AM_WRITE(dkong_spritebank_w)
	AM_RANGE(0x7d84, 0x7d84) AM_WRITE(interrupt_enable_w)
	AM_RANGE(0x7d85, 0x7d85) AM_DEVWRITE(DMA8257, "dma8257", p8257_drq_w)
	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(dkong2b)
	MDRV_CPU_MODIFY("main")
	MDRV_CPU_PROGRAM_MAP(d2k_map,0)
	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, "sound", 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, "sound", 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 */

