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

Minesweeper driver, originally for Dotrikun hardware

Driver by Robert - April 2007

CPU   : Z-80 (4MHz)
SOUND : (none)

****************************************************************************

Notes from the Readme files

MINE SWEEPER for SEGA's Dottorikun PCB

Distributed by Arcade roms from Japan (j-rom)

This game is the Dottorikun port of the Windows' Mine Sweeper.
It has "Time Limit", "more Stage", etc...

Usage:

You must burn DT_MINEJ.BIN to 27128 and replace it to the ROM of
Dottorikun's board. I assume you can find this board easily...

Rules:

* The timer limit is 999
* If you hit a mine, many counts added to the timer
* There are no mines at the start point and the corner

Option:

This game supports BACKUP and SOUND. If you want to play
the sound correctly, you must modify the board. If not,
some noise appears on the screen.

* THIS MODIFY IS QUITE DIFFICULT, USE AT YOUR OWN RISK! *
*    AND DON'T ASK ABOUT THIS MODIFYING INFORMATION!    *

Backup Support:

* You have to install the RESET-IC (PST518, etc.)
* Check the power on/off
* Install the Lithium battery or super capacity condensor
* Connect the inverted RESET signal to RAM's 18pin (use 74HC04, etc.)
  (You must backup both RAM and HC04)

Sound Support:

You must get the sound board of Wing War (Guns'n Wings).
You can hear the sonic sound if you use this sound board!

* Cut IC17's 10pin from the board
* Use shielded cable, Connect CN2's 6pin on the sound board to the above pin
* And the shield connects GND and CN2's 5pin
* Connect IC17 10pin's board side to the GND
* This sound board has 2 set of STEREO, you have to mix them

Secrets:

* Push start when the power on : RAM CLEAR
* right+up lever and push 1 and 2 button : SOUND TEST

* left+up and push 1, 2, COIN, START when the checking Backup RAM is
  finished, you can see the Ending scene.

* Push 1 and 2 when the Ending scene is changed, you can see the
  secret picture (^^)

Robert's notes:
- Backup nvram added
- Tried adding sound by using the YM3438 driver, but couldn't get a clean compile.

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

#include "driver.h"
#include "cpu/z80/z80.h"
//#include "sound/2612intf.h"



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

    Palette Setting.

*******************************************************************/
WRITE8_HANDLER( mineswp_color_w )
{
	palette_set_color(Machine, 0, pal1bit(data >> 3), pal1bit(data >> 4), pal1bit(data >> 5));		// BG color
	palette_set_color(Machine, 1, pal1bit(data >> 0), pal1bit(data >> 1), pal1bit(data >> 2));		// DOT color
}


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

    Draw Pixel.

*******************************************************************/
WRITE8_HANDLER( mineswp_videoram_w )
{
	int i;
	int x, y;
	int color;


	videoram[offset] = data;

	x = 2 * (((offset % 16) * 8));
	y = 2 * ((offset / 16));

	if (x >= Machine->screen[0].visarea.min_x &&
			x <= Machine->screen[0].visarea.max_x &&
			y >= Machine->screen[0].visarea.min_y &&
			y <= Machine->screen[0].visarea.max_y)
	{
		for (i = 0; i < 8; i++)
		{
			color = Machine->pens[((data >> i) & 0x01)];

			/* I think the video hardware doubles pixels, screen would be too small otherwise */
			plot_pixel(tmpbitmap, x + 2*(7 - i),   y,   color);
			plot_pixel(tmpbitmap, x + 2*(7 - i)+1, y,   color);
			plot_pixel(tmpbitmap, x + 2*(7 - i),   y+1, color);
			plot_pixel(tmpbitmap, x + 2*(7 - i)+1, y+1, color);
		}
	}
}


VIDEO_UPDATE( mineswp )
{
	if (get_vh_global_attribute_changed())
	{
		int offs;

		/* redraw bitmap */

		for (offs = 0; offs < videoram_size; offs++)
			mineswp_videoram_w(offs,videoram[offs]);
	}
	copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->screen[0].visarea,TRANSPARENCY_NONE,0);
	return 0;
}


static ADDRESS_MAP_START( mineswp_map, ADDRESS_SPACE_PROGRAM, 8 )
	AM_RANGE(0x0000, 0x3fff) AM_ROM
	AM_RANGE(0x8000, 0x85ff) AM_READWRITE(MRA8_RAM,mineswp_videoram_w) AM_BASE(&videoram) AM_SIZE(&videoram_size)
	AM_RANGE(0x8600, 0x86df) AM_RAM
	AM_RANGE(0x86e0, 0x86ff) AM_RAM AM_BASE(&generic_nvram) AM_SIZE(&generic_nvram_size)
	AM_RANGE(0x8700, 0x87ff) AM_RAM
	AM_RANGE(0xfffc, 0xffff) AM_RAM
ADDRESS_MAP_END

static ADDRESS_MAP_START( mineswp_io, ADDRESS_SPACE_IO, 8 )
	ADDRESS_MAP_FLAGS( AMEF_ABITS(8) )
	AM_RANGE(0x00, 0x00) AM_READ(input_port_0_r)
//	AM_RANGE(0x00, 0x00) AM_WRITE(mineswp_color_w)		/* not used? */
//	AM_RANGE(0x20, 0x20) AM_WRITE(YM3438_data_port_0_A_w) // AM_WRITENOP		// sound?
	AM_RANGE(0x20, 0x20) AM_WRITENOP
ADDRESS_MAP_END


INPUT_PORTS_START( mineswp )
	PORT_START
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )
	PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT )
	PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 )
	PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 )
	PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_START1 )
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_COIN1 )
INPUT_PORTS_END


static MACHINE_DRIVER_START( mineswp )

	/* basic machine hardware */
	MDRV_CPU_ADD(Z80, 4000000)		 /* 4 MHz */
	MDRV_CPU_PROGRAM_MAP(mineswp_map,0)
	MDRV_CPU_IO_MAP(mineswp_io,0)
	MDRV_CPU_VBLANK_INT(irq0_line_hold,1)

	MDRV_SCREEN_REFRESH_RATE(60)
	MDRV_SCREEN_VBLANK_TIME(DEFAULT_60HZ_VBLANK_DURATION)

	/* video hardware */
	MDRV_VIDEO_ATTRIBUTES(VIDEO_TYPE_RASTER)
	MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
	MDRV_SCREEN_SIZE(256, 256)
	MDRV_SCREEN_VISIBLE_AREA(0, 256-1, 0, 192-1)
	MDRV_PALETTE_LENGTH(2)

	MDRV_VIDEO_START(generic_bitmapped)
	MDRV_VIDEO_UPDATE(mineswp)
	MDRV_NVRAM_HANDLER(generic_1fill)

	/* sound hardware */
//	MDRV_SPEAKER_STANDARD_STEREO("left", "right")

//	MDRV_SOUND_ADD(YM3438, 8000000)
//	MDRV_SOUND_ROUTE(0, "left", 0.60)
//	MDRV_SOUND_ROUTE(1, "right", 0.60)
MACHINE_DRIVER_END


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

  Game driver(s)

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

ROM_START( mineswp )
	ROM_REGION( 0x10000, REGION_CPU1, 0 ) /* 64k for code */
	ROM_LOAD( "14479b.mpr", 0x0000, 0x4000, CRC(1cece483) SHA1(c425a09b640721c9a5b499cdb79848dc18ee66b9) )
ROM_END

GAME( 1998, mineswp,  0, mineswp, mineswp, 0, ROT0, "J-Rom", "Mine Sweeper (Dottori Kun hardware) [h]", GAME_NO_SOUND )

