/* BBUGTEST NOTES

With no documentation whatsoever, I have compiled what I know to share with you all.

I do not know if the graphics are complete, or if all tests are working.

Use the dipswitches to select a test pattern.

Pressing any player key, or start button will make a slight change to the colours.

If you move your mouse to the left, pressing the keys will alter the numbers across the bottom
of the screen. Moving the mouse to the right will revert to changing colours again.

If anyone has more information, please tell the misfitmame team. :) */


#include "driver.h"
#include "video/vector.h"
#include "cpu/ccpu/ccpu.h"
#include "cinemat.h"

#define MASTER_CLOCK			XTAL_19_923MHz

static UINT8 coin_detected;
static UINT8 coin_last_reset;

static UINT8 mux_select;



/*************************************
 *
 *  General machine init
 *
 *************************************/

static MACHINE_START( cinemat )
{
	state_save_register_global(coin_detected);
	state_save_register_global(coin_last_reset);
	state_save_register_global(mux_select);
}


MACHINE_RESET( cinemat )
{
	/* reset the coin states */
	coin_detected = 0;
	coin_last_reset = 0;

	/* reset mux select */
	mux_select = 0;
}



/*************************************
 *
 *  General input handlers
 *
 *************************************/

static READ8_HANDLER( inputs_r )
{
	return (input_port_read(machine, "INPUTS") >> offset) & 1;
}


static READ8_HANDLER( switches_r )
{
	static const UINT8 switch_shuffle[8] = { 2,5,4,3,0,1,6,7 };
	return (input_port_read(machine, "SWITCHES") >> switch_shuffle[offset]) & 1;
}



/*************************************
 *
 *  Coin handlers
 *
 *************************************/

static INPUT_CHANGED( coin_inserted )
{
	/* on the falling edge of a new coin, set the coin_detected flag */
	if (newval == 0)
		coin_detected = 1;
}

static READ8_HANDLER( coin_input_r )
{
	return !coin_detected;
}



/*************************************
 *
 *  General output handlers
 *
 *************************************/

static WRITE8_HANDLER( coin_reset_w )
{
	/* on the rising edge of a coin reset, clear the coin_detected flag */
	if (coin_last_reset != data && data != 0)
		coin_detected = 0;
	coin_last_reset = data;
}


static WRITE8_HANDLER( mux_select_w )
{
	mux_select = data;
	cinemat_sound_control_w(machine, 0x07, data);
}


/*************************************
 *
 *  Boxing Bugs inputs
 *
 *************************************/

static READ8_HANDLER( boxingb_dial_r )
{
	int value = input_port_read(machine, "DIAL");
	if (!mux_select) offset += 4;
	return (value >> offset) & 1;
}


/*************************************
 *
 *  Main CPU memory handlers
 *
 *************************************/

static ADDRESS_MAP_START( program_map_32k, ADDRESS_SPACE_PROGRAM, 8 )
	ADDRESS_MAP_GLOBAL_MASK(0x7fff)
	AM_RANGE(0x0000, 0x7fff) AM_ROM
ADDRESS_MAP_END


static ADDRESS_MAP_START( data_map, ADDRESS_SPACE_DATA, 16 )
	AM_RANGE(0x0000, 0x00ff) AM_RAM
ADDRESS_MAP_END


static ADDRESS_MAP_START( io_map, ADDRESS_SPACE_IO, 8 )
	AM_RANGE(0x00, 0x0f) AM_READ(inputs_r)
	AM_RANGE(0x10, 0x16) AM_READ(switches_r)
	AM_RANGE(0x17, 0x17) AM_READ(coin_input_r)

	AM_RANGE(0x05, 0x05) AM_WRITE(coin_reset_w)
	AM_RANGE(0x06, 0x06) AM_WRITE(cinemat_vector_control_w)
	AM_RANGE(0x00, 0x07) AM_WRITE(cinemat_sound_control_w)
ADDRESS_MAP_END



/*************************************
 *
 *  Port definitions
 *
 *************************************/

INPUT_PORTS_START( bbugtest )
	PORT_START("INPUTS")
	PORT_BIT( 0x0001, IP_ACTIVE_LOW,  IPT_BUTTON1 ) PORT_PLAYER(2)
	PORT_BIT( 0x0002, IP_ACTIVE_LOW,  IPT_BUTTON2 ) PORT_PLAYER(2)
	PORT_BIT( 0x0004, IP_ACTIVE_LOW,  IPT_BUTTON3 ) PORT_PLAYER(2)
	PORT_BIT( 0x0008, IP_ACTIVE_LOW,  IPT_BUTTON3 ) PORT_PLAYER(1)
	PORT_BIT( 0x0010, IP_ACTIVE_LOW,  IPT_BUTTON2 ) PORT_PLAYER(1)
	PORT_BIT( 0x0020, IP_ACTIVE_LOW,  IPT_BUTTON1 ) PORT_PLAYER(1)
	PORT_BIT( 0x0fc0, IP_ACTIVE_LOW,  IPT_UNUSED )
	PORT_BIT( 0xf000, IP_ACTIVE_HIGH, IPT_SPECIAL )	/* dial */

	PORT_START("SWITCHES")
	PORT_DIPNAME( 0x24,	0x00, "Test Mode" )
	PORT_DIPSETTING(	0x00, "Corners" )
	PORT_DIPSETTING(	0x04, "Grid" )
	PORT_DIPSETTING(	0x20, "Lines" )
	PORT_DIPSETTING(	0x24, "Rainbow" )
	PORT_SERVICE( 0x40,	IP_ACTIVE_LOW )
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_COIN1 ) PORT_CHANGED(coin_inserted, 0)

	PORT_START("DIAL")
	PORT_BIT( 0xff, 0x00, IPT_DIAL ) PORT_REVERSE PORT_SENSITIVITY(100) PORT_KEYDELTA(5)
INPUT_PORTS_END



/*************************************
 *
 *  CPU configurations
 *
 *************************************/

static const ccpu_config config_jmi =
{
	NULL,
	cinemat_vector_callback
};



/*************************************
 *
 *  Core machine drivers
 *
 *************************************/

static MACHINE_DRIVER_START( bbugtest )

	/* basic machine hardware */
	MDRV_CPU_ADD("main", CCPU, 5000000)
	MDRV_CPU_CONFIG(config_jmi)
	MDRV_CPU_PROGRAM_MAP(program_map_32k,0)
	MDRV_CPU_DATA_MAP(data_map,0)
	MDRV_CPU_IO_MAP(io_map,0)

	MDRV_MACHINE_START(cinemat)
	MDRV_MACHINE_RESET(cinemat)

	/* video hardware */
	MDRV_VIDEO_ATTRIBUTES(VIDEO_ALWAYS_UPDATE)

	MDRV_SCREEN_ADD("main", VECTOR)
	MDRV_SCREEN_REFRESH_RATE(MASTER_CLOCK/4/16/16/16/16/2)
	MDRV_SCREEN_SIZE(1024, 768)
	MDRV_SCREEN_VISIBLE_AREA(0, 1023, 0, 767)

	MDRV_VIDEO_START(cinemat_color)
	MDRV_VIDEO_UPDATE(cinemat)
MACHINE_DRIVER_END


/*************************************
 *
 *  Driver initialization
 *
 *************************************/


static DRIVER_INIT( bbugtest )
{
	memory_install_read8_handler(machine, 0, ADDRESS_SPACE_IO, 0x0c, 0x0f, 0, 0, boxingb_dial_r);
	memory_install_write8_handler(machine, 0, ADDRESS_SPACE_IO, 0x07, 0x07, 0, 0, mux_select_w);
}


/*************************************
 *
 *  Game drivers
 *
 *************************************/

ROM_START( bbugtest )
	ROM_REGION( 0x10000, "main", 0 )	/* 16k for code */
	ROM_LOAD16_BYTE( "bbugtest.t7", 0x0000, 0x1000, CRC(92a460f1) SHA1(eaa8fea38d87f1fd6e2e52b92085fed43498e5ce) )
	ROM_LOAD16_BYTE( "bbugtest.p7", 0x0001, 0x1000, CRC(06d0bde2) SHA1(b0a224b7a74fa739b293ad7383236ffaa012f3c3) )
	ROM_LOAD16_BYTE( "bbugtest.u7", 0x2000, 0x1000, CRC(92a460f1) SHA1(eaa8fea38d87f1fd6e2e52b92085fed43498e5ce) )
	ROM_LOAD16_BYTE( "bbugtest.r7", 0x2001, 0x1000, CRC(06d0bde2) SHA1(b0a224b7a74fa739b293ad7383236ffaa012f3c3) )

	ROM_REGION( 0x1a0, "proms", ROMREGION_DISPOSE )
	ROM_LOAD("prom.f14", 0x000, 0x100, CRC(9edbf536) SHA1(036ad8a231284e05f44b1106d38fc0c7e041b6e8) )
	ROM_LOAD("prom.e14", 0x100, 0x020, CRC(29dbfb87) SHA1(d8c40ab010b2ea30f29b2c443819e2b69f376c04) )
	ROM_LOAD("prom.d14", 0x120, 0x020, CRC(9a05afbf) SHA1(5d806a42424942ba5ef0b70a1d629315b37f931b) )
	ROM_LOAD("prom.c14", 0x140, 0x020, CRC(07492cda) SHA1(32df9148797c23f70db47b840139c40e046dd710) )
	ROM_LOAD("prom.j14", 0x160, 0x020, CRC(a481ca71) SHA1(ce145d61686f600cc16b77febfd5c783bf8c13b0) )
	ROM_LOAD("prom.e8",  0x180, 0x020, CRC(791ec9e1) SHA1(6f7fcce4aa3be9020595235568381588adaab88e) )
ROM_END

GAME(1981, bbugtest, 0, bbugtest, bbugtest,  bbugtest,  ORIENTATION_FLIP_Y,   "Test Rom", "Test Rom - Boxing Bugs", GAME_SUPPORTS_SAVE )

