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

Senjyo / Star Force

This board was obviously born to run Senjyo. Four scrolling layers, gradient
background, sprite/background priorities, and even a small bitmap for the
radar. Star Force uses only a small subset of the features.

MAIN BOARD:
0000-7fff ROM
8000-8fff RAM
9000-93ff Video RAM
9400-97ff Color RAM
9800-987f Sprites
9c00-9dff Palette RAM
a000-a37f Background Video RAM #3
a800-aaff Background Video RAM #2
b000-b1ff Background Video RAM #1
b800-bbff Radar bitmap

read:
d000      IN0
d001      IN1
d002      IN2
d003      ?
d004      DSW1
d005      DSW2

write:
9e20-9e21 background #1 x position
9e25      background #1 y position
9e28-9e29 background #? x position ??
9e30-9e31 background #2 & #3 x position
9e35      background #2 & #3 y position
d000      ?? (before access paletteram data 0x00 )
d002      watchdog reset?
          IN0/IN1 latch ? ( write before read IN0/IN1 )
d004      sound command ( pio-a )

SOUND BOARD
memory read/write
0000-3fff ROM
4000-43ff RAM

write
8000 sound chip channel 1 1st 9f,bf,df,ff
9000 sound chip channel 2 1st 9f,bf,df,ff
a000 sound chip channel 3 1st 9f,bf,df,ff
d000 bit 0-3 single sound volume ( freq = ctc2 )
e000 ? ( initialize only )
f000 ? ( initialize only )

I/O read/write
00   z80pio-A data     ( from sound command )
01   z80pio-A controll ( mode 1 input )
02   z80pio-B data     ( no use )
03   z80pio-B controll ( mode 3 bit i/o )
08   z80ctc-ch1        ( timer mode cysclk/16, bas clock 15.625KHz )
09   z80ctc-ch2        ( cascade from ctc-1  , tempo interrupt 88.778Hz )
0a   z80ctc-ch3        ( timer mode , single sound freq. )
0b   z80ctc-ch4        ( no use )

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

#include "driver.h"
#include "vidhrdw/generic.h"
#include "machine/z80fmly.h"



/* in machine/segacrpt.c */
void suprloco_decode(void);


extern unsigned char *senjyo_fgscroll;
extern unsigned char *senjyo_bgstripes;
extern unsigned char *senjyo_scrollx1,*senjyo_scrolly1;
extern unsigned char *senjyo_scrollx2,*senjyo_scrolly2;
extern unsigned char *senjyo_scrollx3,*senjyo_scrolly3;
extern unsigned char *senjyo_fgvideoram,*senjyo_fgcolorram;
extern unsigned char *senjyo_bg1videoram,*senjyo_bg2videoram,*senjyo_bg3videoram;
extern unsigned char *senjyo_radarram;
void senjyo_fgvideoram_w(int offset,int data);
void senjyo_fgcolorram_w(int offset,int data);
void senjyo_bg1videoram_w(int offset,int data);
void senjyo_bg2videoram_w(int offset,int data);
void senjyo_bg3videoram_w(int offset,int data);
void senjyo_bgstripes_w(int offset,int data);

void starforc_init(void);
void starfore_init(void);
void senjyo_init(void);

int senjyo_vh_start(void);
void senjyo_vh_stop(void);
void senjyo_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh);

int senjyo_sh_start(const struct MachineSound *msound);
void senjyo_sh_stop(void);
void senjyo_sh_update(void);

void senjyo_sh_0_w( int offset , int data );
void senjyo_sh_1_w( int offset , int data );
void senjyo_sh_2_w( int offset , int data );

void starforc_pio_w( int offset , int data );
int  starforc_pio_r( int offset );

#if 1
void senjyo_volume_w( int offset , int data );
#endif



static int int_delay_kludge;

void senjyo_init_machine(void)
{
	/* we must avoid generating interrupts for the first few frames otherwise */
	/* Senjyo locks up. There must be an interrupt enable port somewhere, */
	/* or maybe interrupts are genenrated by the CTC. */
	/* Maybe a write to port d002 clears the IRQ line, but I'm not sure. */
	int_delay_kludge = 10;
}

int senjyo_interrupt(void)
{
	if (int_delay_kludge == 0) return interrupt();
	else int_delay_kludge--;
	return ignore_interrupt();
}



static struct MemoryReadAddress readmem[] =
{
	{ 0x0000, 0x7fff, MRA_ROM },
	{ 0x8000, 0x97ff, MRA_RAM },
	{ 0x9800, 0x987f, MRA_RAM },
	{ 0x9c00, 0x9d8f, MRA_RAM },
	{ 0x9e00, 0x9e3f, MRA_RAM },
	{ 0xa000, 0xa37f, MRA_RAM },
	{ 0xa800, 0xaaff, MRA_RAM },
	{ 0xb000, 0xb1ff, MRA_RAM },
	{ 0xb800, 0xbbff, MRA_RAM },
	{ 0xd000, 0xd000, input_port_0_r },	/* player 1 input */
	{ 0xd001, 0xd001, input_port_1_r },	/* player 2 input */
	{ 0xd002, 0xd002, input_port_2_r },	/* coin */
	{ 0xd004, 0xd004, input_port_3_r },	/* DSW1 */
	{ 0xd005, 0xd005, input_port_4_r },	/* DSW2 */
	{ -1 }  /* end of table */
};

static struct MemoryWriteAddress writemem[] =
{
	{ 0x0000, 0x7fff, MWA_ROM },
	{ 0x8000, 0x8fff, MWA_RAM },
	{ 0x9000, 0x93ff, senjyo_fgvideoram_w, &senjyo_fgvideoram },
	{ 0x9400, 0x97ff, senjyo_fgcolorram_w, &senjyo_fgcolorram },
	{ 0x9800, 0x987f, MWA_RAM, &spriteram, &spriteram_size },
	{ 0x9c00, 0x9d8f, paletteram_IIBBGGRR_w, &paletteram },
	{ 0x9e00, 0x9e1f, MWA_RAM, &senjyo_fgscroll },
	{ 0x9e20, 0x9e21, MWA_RAM, &senjyo_scrolly3 },
/*	{ 0x9e22, 0x9e23, height of the layer (Senjyo only, fixed at 0x380) */
	{ 0x9e25, 0x9e25, MWA_RAM, &senjyo_scrollx3 },
	{ 0x9e27, 0x9e27, senjyo_bgstripes_w, &senjyo_bgstripes },	/* controls width of background stripes */
	{ 0x9e28, 0x9e29, MWA_RAM, &senjyo_scrolly2 },
/*	{ 0x9e2a, 0x9e2b, height of the layer (Senjyo only, fixed at 0x200) */
	{ 0x9e2d, 0x9e2d, MWA_RAM, &senjyo_scrollx2 },
	{ 0x9e30, 0x9e31, MWA_RAM, &senjyo_scrolly1 },
/*	{ 0x9e32, 0x9e33, height of the layer (Senjyo only, fixed at 0x100) */
	{ 0x9e35, 0x9e35, MWA_RAM, &senjyo_scrollx1 },
/*	{ 0x9e38, 0x9e38, probably radar y position (Senjyo only, fixed at 0x61) */
/*	{ 0x9e3d, 0x9e3d, probably radar x position (Senjyo only, 0x00/0xc0 depending on screen flip) */
{ 0x9e00, 0x9e3f, MWA_RAM },
	{ 0xa000, 0xa37f, senjyo_bg3videoram_w, &senjyo_bg3videoram },
	{ 0xa800, 0xaaff, senjyo_bg2videoram_w, &senjyo_bg2videoram },
	{ 0xb000, 0xb1ff, senjyo_bg1videoram_w, &senjyo_bg1videoram },
	{ 0xb800, 0xbbff, MWA_RAM, &senjyo_radarram },
	{ 0xd004, 0xd004, z80pioA_0_p_w },
	{ -1 }  /* end of table */
};


static struct MemoryReadAddress sound_readmem[] =
{
	{ 0x0000, 0x3fff, MRA_ROM },
	{ 0x4000, 0x43ff, MRA_RAM },
	{ -1 }  /* end of table */
};

static struct MemoryWriteAddress sound_writemem[] =
{
	{ 0x0000, 0x3fff, MWA_ROM },
	{ 0x4000, 0x43ff, MWA_RAM },
	{ 0x8000, 0x8000, SN76496_0_w },
	{ 0x9000, 0x9000, SN76496_1_w },
	{ 0xa000, 0xa000, SN76496_2_w },
	{ 0xd000, 0xd000, senjyo_volume_w },
#if 0
	{ 0xe000, 0xe000, unknown },
	{ 0xf000, 0xf000, unknown },
#endif
	{ -1 }  /* end of table */
};

static struct IOReadPort sound_readport[] =
{
	{ 0x00, 0x03, z80pio_0_r },
	{ 0x08, 0x0b, z80ctc_0_r },
	{ -1 }	/* end of table */
};

static struct IOWritePort sound_writeport[] =
{
	{ 0x00, 0x03, z80pio_0_w },
	{ 0x08, 0x0b, z80ctc_0_w },
	{ -1 }	/* end of table */
};




INPUT_PORTS_START( senjyo_input_ports )
	PORT_START	/* IN0 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT | IPF_8WAY )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT | IPF_8WAY )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP | IPF_8WAY )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN | IPF_8WAY )
	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 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 | IPF_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 */
/* coin input for both must be active between 2 and 9 frames to be consistently recognized */
	PORT_BIT_IMPULSE( 0x01, IP_ACTIVE_HIGH, IPT_COIN1, 2 )
	PORT_BIT_IMPULSE( 0x02, IP_ACTIVE_HIGH, IPT_COIN2, 2 )
	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_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_START	/* DSW0 */

	PORT_DIPNAME( 0x03, 0x00, DEF_STR( Coin_A ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x01, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x02, DEF_STR( 1C_3C ) )
	PORT_DIPSETTING(    0x03, DEF_STR( 1C_6C ) )
	PORT_DIPNAME( 0x0c, 0x00, DEF_STR( Coin_B ) )
	PORT_DIPSETTING(    0x04, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x08, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x0c, DEF_STR( 1C_3C ) )
	PORT_DIPNAME( 0x30, 0x00, DEF_STR( Lives ) )
	PORT_DIPSETTING(    0x30, "2" )
	PORT_DIPSETTING(    0x00, "3" )
	PORT_DIPSETTING(    0x10, "4" )
	PORT_DIPSETTING(    0x20, "5" )
	PORT_DIPNAME( 0x40, 0x40, DEF_STR( Cabinet ) )
	PORT_DIPSETTING(    0x40, DEF_STR( Upright ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Cocktail ) )
	PORT_DIPNAME( 0x80, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x80, DEF_STR( On ) )

	PORT_START	/* DSW1 */
	PORT_DIPNAME( 0x01, 0x01, DEF_STR( Demo_Sounds ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x01, DEF_STR( On ) )
	PORT_DIPNAME( 0x02, 0x00, DEF_STR( Bonus_Life ) )
	PORT_DIPSETTING(    0x00, "100000" )
	PORT_DIPSETTING(    0x02, "None" )
	PORT_DIPNAME( 0x04, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x04, DEF_STR( On ) )
	PORT_DIPNAME( 0x08, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x08, DEF_STR( On ) )
	PORT_DIPNAME( 0x10, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x10, DEF_STR( On ) )
	PORT_DIPNAME( 0x20, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x20, DEF_STR( On ) )
	PORT_DIPNAME( 0x40, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x40, DEF_STR( On ) )
	PORT_DIPNAME( 0x80, 0x00, "Planes" )
	PORT_DIPSETTING(    0x00, "Normal" )
	PORT_DIPSETTING(    0x80, "Always come at you" )
INPUT_PORTS_END

INPUT_PORTS_START( starforc_input_ports )
	PORT_START	/* IN0 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT | IPF_8WAY )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT | IPF_8WAY )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP | IPF_8WAY )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN | IPF_8WAY )
	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 */
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN | IPF_8WAY | IPF_COCKTAIL )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 | IPF_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 */
/* coin input for both must be active between 2 and 9 frames to be consistently recognized */
	PORT_BIT_IMPULSE( 0x01, IP_ACTIVE_HIGH, IPT_COIN1, 2 )
	PORT_BIT_IMPULSE( 0x02, IP_ACTIVE_HIGH, IPT_COIN2, 2 )
	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_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_START	/* DSW0 */

	PORT_DIPNAME( 0x03, 0x00, DEF_STR( Coin_A ) )
	PORT_DIPSETTING(    0x01, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x02, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x03, DEF_STR( 1C_3C ) )
	PORT_DIPNAME( 0x0c, 0x00, DEF_STR( Coin_B ) )
	PORT_DIPSETTING(    0x04, DEF_STR( 2C_1C ) )
	PORT_DIPSETTING(    0x00, DEF_STR( 1C_1C ) )
	PORT_DIPSETTING(    0x08, DEF_STR( 1C_2C ) )
	PORT_DIPSETTING(    0x0c, DEF_STR( 1C_3C ) )
	PORT_DIPNAME( 0x30, 0x00, DEF_STR( Lives ) )
	PORT_DIPSETTING(    0x30, "2" )
	PORT_DIPSETTING(    0x00, "3" )
	PORT_DIPSETTING(    0x10, "4" )
	PORT_DIPSETTING(    0x20, "5" )
	PORT_DIPNAME( 0x40, 0x40, DEF_STR( Cabinet ) )
	PORT_DIPSETTING(    0x40, DEF_STR( Upright ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Cocktail ) )
	PORT_DIPNAME( 0x80, 0x80, DEF_STR( Demo_Sounds ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x80, DEF_STR( On ) )

	PORT_START	/* DSW1 */
	PORT_DIPNAME( 0x07, 0x00, DEF_STR( Bonus_Life ) )
	PORT_DIPSETTING(    0x00, "50000 200000 500000" )
	PORT_DIPSETTING(    0x01, "100000 300000 800000" )
	PORT_DIPSETTING(    0x02, "50000 200000" )
	PORT_DIPSETTING(    0x03, "100000 300000" )
	PORT_DIPSETTING(    0x04, "50000" )
	PORT_DIPSETTING(    0x05, "100000" )
	PORT_DIPSETTING(    0x06, "200000" )
	PORT_DIPSETTING(    0x07, "None" )
	PORT_DIPNAME( 0x38, 0x00, DEF_STR( Difficulty ) )
	PORT_DIPSETTING(    0x00, "Easiest" )
	PORT_DIPSETTING(    0x08, "Easy" )
	PORT_DIPSETTING(    0x10, "Normal" )
	PORT_DIPSETTING(    0x18, "Difficult" )
	PORT_DIPSETTING(    0x20, "Hard" )
	PORT_DIPSETTING(    0x28, "Hardest" )
	/* 0x30 and x038 are unused */
	PORT_DIPNAME( 0x40, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x40, DEF_STR( On ) )
	PORT_DIPNAME( 0x80, 0x00, DEF_STR( Unknown ) )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x80, DEF_STR( On ) )
INPUT_PORTS_END



static struct GfxLayout charlayout =
{
	8,8,	/* 8*8 characters */
	512,	/* 512 characters */
	3,	/* 3 bits per pixel */
	{ 0, 512*8*8, 2*512*8*8 },	/* the bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7 },
	{ 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 struct GfxLayout tilelayout_256 =
{
	16,16,	/* 16*16 characters */
	256,	/* 256 characters */
	3,	/* 3 bits per pixel */
	{ 0, 256*16*16, 2*256*16*16 },	/* the bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7,
			8*8+0, 8*8+1, 8*8+2, 8*8+3, 8*8+4, 8*8+5, 8*8+6, 8*8+7 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
			16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8 },
	32*8	/* every character takes 32 consecutive bytes */
};
static struct GfxLayout tilelayout_128 =
{
	16,16,	/* 16*16 characters */
	128,	/* 128 characters */
	3,	/* 3 bits per pixel */
	{ 0, 128*16*16, 2*128*16*16 },	/* the bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7,
			8*8+0, 8*8+1, 8*8+2, 8*8+3, 8*8+4, 8*8+5, 8*8+6, 8*8+7 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
			16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8 },
	32*8	/* every character takes 32 consecutive bytes */
};
static struct GfxLayout spritelayout1 =
{
	16,16,	/* 16*16 sprites */
	512,	/* 512 sprites */
	3,	/* 3 bits per pixel */
	{ 0, 512*16*16, 2*512*16*16 },	/* the bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7,
			8*8+0, 8*8+1, 8*8+2, 8*8+3, 8*8+4, 8*8+5, 8*8+6, 8*8+7 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
			16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8 },
	32*8	/* every sprite takes 32 consecutive bytes */
};
static struct GfxLayout spritelayout2 =
{
	32,32,	/* 32*32 sprites */
	128,	/* 128 sprites */
	3,	/* 3 bits per pixel */
	{ 0, 128*32*32, 2*128*32*32 },	/* the bitplanes are separated */
	{ 0, 1, 2, 3, 4, 5, 6, 7,
			8*8+0, 8*8+1, 8*8+2, 8*8+3, 8*8+4, 8*8+5, 8*8+6, 8*8+7,
			32*8+0, 32*8+1, 32*8+2, 32*8+3, 32*8+4, 32*8+5, 32*8+6, 32*8+7,
			40*8+0, 40*8+1, 40*8+2, 40*8+3, 40*8+4, 40*8+5, 40*8+6, 40*8+7 },
	{ 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8,
			16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8,
			64*8, 65*8, 66*8, 67*8, 68*8, 69*8, 70*8, 71*8,
			80*8, 81*8, 82*8, 83*8, 84*8, 85*8, 86*8, 87*8 },
	128*8	/* every sprite takes 128 consecutive bytes */
};

static struct GfxDecodeInfo senjyo_gfxdecodeinfo[] =
{
	{ 1, 0x00000, &charlayout,       0, 8 },	/*   0- 63 characters */
	{ 1, 0x03000, &tilelayout_128,  64, 8 },	/*  64-127 background #1 */
	{ 1, 0x06000, &tilelayout_128, 128, 8 },	/* 128-191 background #2 */
	{ 1, 0x09000, &tilelayout_128, 192, 8 },	/* 192-255 background #3 */
	{ 1, 0x0c000, &spritelayout1,  320, 8 },	/* 320-383 normal sprites */
	{ 1, 0x0c000, &spritelayout2,  320, 8 },	/* 320-383 large sprites */
												/* 384-399 is background */
	{ -1 } /* end of array */
};

static struct GfxDecodeInfo starforc_gfxdecodeinfo[] =
{
	{ 1, 0x00000, &charlayout,       0, 8 },	/*   0- 63 characters */
	{ 1, 0x03000, &tilelayout_256,  64, 8 },	/*  64-127 background #1 */
	{ 1, 0x09000, &tilelayout_256, 128, 8 },	/* 128-191 background #2 */
	{ 1, 0x0f000, &tilelayout_128, 192, 8 },	/* 192-255 background #3 */
	{ 1, 0x12000, &spritelayout1,  320, 8 },	/* 320-383 normal sprites */
	{ 1, 0x12000, &spritelayout2,  320, 8 },	/* 320-383 large sprites */
												/* 384-399 is background */
	{ -1 } /* end of array */
};



static Z80_DaisyChain daisy_chain[] =
{
	{ z80pio_reset , z80pio_interrupt, z80pio_reti , 0 }, /* device 0 = PIO_0 , low  priority */
	{ z80ctc_reset , z80ctc_interrupt, z80ctc_reti , 0 }, /* device 1 = CTC_0 , high priority */
	{ 0,0,0,-1} 	   /* end mark */
};



static struct SN76496interface sn76496_interface =
{
	3,	/* 3 chips */
	2000000,	/* 2 Mhz? */
	{ 50, 50, 50 }
};

static struct CustomSound_interface custom_interface =
{
	senjyo_sh_start,
	senjyo_sh_stop,
	senjyo_sh_update
};



#define MACHINE_DRV(NAME)                                                 \
static struct MachineDriver NAME##_machine_driver =                       \
{                                                                         \
	{                                                                     \
		{                                                                 \
			CPU_Z80,                                                      \
			4000000,	/* 4 Mhz? */                                      \
			0,                                                            \
			readmem,writemem,0,0,                                         \
			senjyo_interrupt,1                                            \
		},                                                                \
		{                                                                 \
			CPU_Z80 | CPU_AUDIO_CPU,                                      \
			2000000,	/* 2 Mhz? */                                      \
			2,                                                            \
			sound_readmem,sound_writemem,sound_readport,sound_writeport,  \
			0,0, /* interrupts are made by z80 daisy chain system */      \
			0,0,daisy_chain                                               \
		}                                                                 \
	},                                                                    \
	60, DEFAULT_60HZ_VBLANK_DURATION,	/* frames per second, vblank duration */              \
	1,	/* 1 CPU slice per frame - interleaving is forced when a sound command is written */  \
	senjyo_init_machine,                                                  \
                                                                          \
	/* video hardware */                                                  \
	32*8, 32*8, { 0*8, 32*8-1, 2*8, 30*8-1 },                             \
	NAME##_gfxdecodeinfo,                                                 \
	402, 402,                                                             \
	0,                                                                    \
                                                                          \
	VIDEO_TYPE_RASTER | VIDEO_MODIFIES_PALETTE,                           \
	0,                                                                    \
	senjyo_vh_start,                                                      \
	senjyo_vh_stop,                                                       \
	senjyo_vh_screenrefresh,                                              \
                                                                          \
	/* sound hardware */                                                  \
	0,0,0,0,                                                              \
	{                                                                     \
		{                                                                 \
			SOUND_SN76496,                                                \
			&sn76496_interface                                            \
		},                                                                \
		{                                                                 \
			SOUND_CUSTOM,                                                 \
			&custom_interface                                             \
		}                                                                 \
	}                                                                     \
}


MACHINE_DRV(senjyo);
MACHINE_DRV(starforc);


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

  Game driver(s)

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

ROM_START( senjyo_rom )
	ROM_REGION(0x10000)     /* 64k for code */
	ROM_LOAD( "08m_05t.bin", 0x0000, 0x2000, 0xb1f3544d )
	ROM_LOAD( "08k_04t.bin", 0x2000, 0x2000, 0xe34468a8 )
	ROM_LOAD( "08j_03t.bin", 0x4000, 0x2000, 0xc33aedee )
	ROM_LOAD( "08f_02t.bin", 0x6000, 0x2000, 0x0ef4db9e )

	ROM_REGION(0x18000)     /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "08h_08b.bin", 0x00000, 0x1000, 0x0c875994 )	/* fg */
	ROM_LOAD( "08f_07b.bin", 0x01000, 0x1000, 0x497bea8e )
	ROM_LOAD( "08d_06b.bin", 0x02000, 0x1000, 0x4ef69b00 )
	ROM_LOAD( "05n_16m.bin", 0x03000, 0x1000, 0x0d3e00fb )	/* bg1 */
	ROM_LOAD( "05k_15m.bin", 0x04000, 0x2000, 0x93442213 )
	ROM_LOAD( "07n_18m.bin", 0x06000, 0x1000, 0xd50fced3 )	/* bg2 */
	ROM_LOAD( "07k_17m.bin", 0x07000, 0x2000, 0x10c3a5f0 )
	ROM_LOAD( "09n_20m.bin", 0x09000, 0x1000, 0x54cb8126 )	/* bg3 */
	ROM_LOAD( "09k_19m.bin", 0x0a000, 0x2000, 0x373e047c )
	ROM_LOAD( "08p_13b.bin", 0x0c000, 0x2000, 0x40127efd )	/* sprites */
	ROM_LOAD( "08s_14b.bin", 0x0e000, 0x2000, 0x42648ffa )
	ROM_LOAD( "08m_11b.bin", 0x10000, 0x2000, 0xccc4680b )
	ROM_LOAD( "08n_12b.bin", 0x12000, 0x2000, 0x742fafed )
	ROM_LOAD( "08j_09b.bin", 0x14000, 0x2000, 0x1ee63b5c )
	ROM_LOAD( "08k_10b.bin", 0x16000, 0x2000, 0xa9f41ec9 )

	ROM_REGION(0x10000)     /* 64k for sound board */
	ROM_LOAD( "02h_01t.bin", 0x0000, 0x2000, 0xc1c24455 )

	ROM_REGION(0x0020)     /* PROMs */
    ROM_LOAD( "07b.bin",    0x0000, 0x0020, 0x68db8300 )	/* unknown - timing? */
ROM_END

ROM_START( starforc_rom )
	ROM_REGION(0x10000)     /* 64k for code */
	ROM_LOAD( "starforc.3",   0x0000, 0x4000, 0x8ba27691 )
	ROM_LOAD( "starforc.2",   0x4000, 0x4000, 0x0fc4d2d6 )

	ROM_REGION_DISPOSE(0x1e000)     /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "starforc.7",   0x00000, 0x1000, 0xf4803339 )	/* fg */
	ROM_LOAD( "starforc.8",   0x01000, 0x1000, 0x96979684 )
	ROM_LOAD( "starforc.9",   0x02000, 0x1000, 0xeead1d5c )
	ROM_LOAD( "starforc.15",  0x03000, 0x2000, 0xc3bda12f )	/* bg1 */
	ROM_LOAD( "starforc.14",  0x05000, 0x2000, 0x9e9384fe )
	ROM_LOAD( "starforc.13",  0x07000, 0x2000, 0x84603285 )
	ROM_LOAD( "starforc.12",  0x09000, 0x2000, 0xfdd9e38b )	/* bg2 */
	ROM_LOAD( "starforc.11",  0x0b000, 0x2000, 0x668aea14 )
	ROM_LOAD( "starforc.10",  0x0d000, 0x2000, 0xc62a19c1 )
	ROM_LOAD( "starforc.18",  0x0f000, 0x1000, 0x6455c3ad )	/* bg3 */
	ROM_LOAD( "starforc.17",  0x10000, 0x1000, 0x68c60d0f )
	ROM_LOAD( "starforc.16",  0x11000, 0x1000, 0xce20b469 )
	ROM_LOAD( "starforc.6",   0x12000, 0x4000, 0x5468a21d )	/* sprites */
	ROM_LOAD( "starforc.5",   0x16000, 0x4000, 0xf71717f8 )
	ROM_LOAD( "starforc.4",   0x1a000, 0x4000, 0xdd9d68a4 )

	ROM_REGION(0x10000)     /* 64k for sound board */
	ROM_LOAD( "starforc.1",   0x0000, 0x2000, 0x2735bb22 )
ROM_END

ROM_START( starfore_rom )
	ROM_REGION(0x10000)     /* 64k for code */
	ROM_LOAD( "starfore.005", 0x0000, 0x2000, 0x825f7ebe )
	ROM_LOAD( "starfore.004", 0x2000, 0x2000, 0xfbcecb65 )
	ROM_LOAD( "starfore.003", 0x4000, 0x2000, 0x9f8013b9 )
	ROM_LOAD( "starfore.002", 0x6000, 0x2000, 0xf8111eba )

	ROM_REGION_DISPOSE(0x1e000)     /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "starforc.7",   0x00000, 0x1000, 0xf4803339 )	/* fg */
	ROM_LOAD( "starforc.8",   0x01000, 0x1000, 0x96979684 )
	ROM_LOAD( "starforc.9",   0x02000, 0x1000, 0xeead1d5c )
	ROM_LOAD( "starforc.15",  0x03000, 0x2000, 0xc3bda12f )	/* bg1 */
	ROM_LOAD( "starforc.14",  0x05000, 0x2000, 0x9e9384fe )
	ROM_LOAD( "starforc.13",  0x07000, 0x2000, 0x84603285 )
	ROM_LOAD( "starforc.12",  0x09000, 0x2000, 0xfdd9e38b )	/* bg2 */
	ROM_LOAD( "starforc.11",  0x0b000, 0x2000, 0x668aea14 )
	ROM_LOAD( "starforc.10",  0x0d000, 0x2000, 0xc62a19c1 )
	ROM_LOAD( "starforc.18",  0x0f000, 0x1000, 0x6455c3ad )	/* bg3 */
	ROM_LOAD( "starforc.17",  0x10000, 0x1000, 0x68c60d0f )
	ROM_LOAD( "starforc.16",  0x11000, 0x1000, 0xce20b469 )
	ROM_LOAD( "starforc.6",   0x12000, 0x4000, 0x5468a21d )	/* sprites */
	ROM_LOAD( "starforc.5",   0x16000, 0x4000, 0xf71717f8 )
	ROM_LOAD( "starforc.4",   0x1a000, 0x4000, 0xdd9d68a4 )

	ROM_REGION(0x10000)     /* 64k for sound board */
	ROM_LOAD( "starfore.000", 0x0000, 0x2000, 0xa277c268 )

	ROM_REGION(0x0020)     /* PROMs */
    ROM_LOAD( "07b.bin",    0x0000, 0x0020, 0x68db8300 )	/* unknown - timing? */
ROM_END

ROM_START( megaforc_rom )
	ROM_REGION(0x10000)     /* 64k for code */
	ROM_LOAD( "mf3.bin",      0x0000, 0x4000, 0xd3ea82ec )
	ROM_LOAD( "mf2.bin",      0x4000, 0x4000, 0xaa320718 )

	ROM_REGION_DISPOSE(0x1e000)     /* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "mf7.bin",      0x00000, 0x1000, 0x43ef8d20 )	/* fg */
	ROM_LOAD( "mf8.bin",      0x01000, 0x1000, 0xc36fb746 )
	ROM_LOAD( "mf9.bin",      0x02000, 0x1000, 0x62e7c9ec )
	ROM_LOAD( "starforc.15",  0x03000, 0x2000, 0xc3bda12f )	/* bg1 */
	ROM_LOAD( "starforc.14",  0x05000, 0x2000, 0x9e9384fe )
	ROM_LOAD( "starforc.13",  0x07000, 0x2000, 0x84603285 )
	ROM_LOAD( "starforc.12",  0x09000, 0x2000, 0xfdd9e38b )	/* bg2 */
	ROM_LOAD( "starforc.11",  0x0b000, 0x2000, 0x668aea14 )
	ROM_LOAD( "starforc.10",  0x0d000, 0x2000, 0xc62a19c1 )
	ROM_LOAD( "starforc.18",  0x0f000, 0x1000, 0x6455c3ad )	/* bg3 */
	ROM_LOAD( "starforc.17",  0x10000, 0x1000, 0x68c60d0f )
	ROM_LOAD( "starforc.16",  0x11000, 0x1000, 0xce20b469 )
	ROM_LOAD( "starforc.6",   0x12000, 0x4000, 0x5468a21d )	/* sprites */
	ROM_LOAD( "starforc.5",   0x16000, 0x4000, 0xf71717f8 )
	ROM_LOAD( "starforc.4",   0x1a000, 0x4000, 0xdd9d68a4 )

	ROM_REGION(0x10000)     /* 64k for sound board */
	ROM_LOAD( "starforc.1",   0x0000, 0x2000, 0x2735bb22 )

	ROM_REGION(0x0020)     /* PROMs */
    ROM_LOAD( "07b.bin",    0x0000, 0x0020, 0x68db8300 )	/* unknown - timing? */
ROM_END



static int starforc_hiload(void)
{
        unsigned char *RAM = Machine->memory_region[Machine->drv->cpu[0].memory_region];


        if (memcmp(&RAM[0x8348],"\x00\x08\x05\x00",4) == 0 &&
        memcmp(&RAM[0x9181],"\x18",1) == 0 &&
        memcmp(&RAM[0x91a1],"\x18",1) == 0 &&
        memcmp(&RAM[0x91c1],"\x21",1) == 0 &&
        memcmp(&RAM[0x91e1],"\x18",1) == 0 &&
        memcmp(&RAM[0x9201],"\x1d",1) == 0)

        {
                void *f;

                if ((f = osd_fopen(Machine->gamedrv->name,0,OSD_FILETYPE_HIGHSCORE,0)) != 0)
                {
                        int p,temp;

                        osd_fread(f,&RAM[0x8038],112);
                        RAM[0x8348] = RAM[0x803d];
                        RAM[0x8349] = RAM[0x803c];
                        RAM[0x834a] = RAM[0x803b];
                        RAM[0x834b] = RAM[0x803a];

                        for (p=0;p<4;p++)
                           {
                            temp=0x18+(RAM[0x8348+p]%16);
                            if (temp>=0x20) temp++;
                            RAM[0x9181+(p*0x40)]=temp;
                            temp=0x18+(RAM[0x8348+p]/16);
                            if (temp>=0x20) temp++;
                            RAM[0x91A1+(p*0x40)]=temp;
                            }

                        for (p=0;p<8;p++ )
                        {
                                if (RAM[0x9261-(p*0x20)]==0x18) RAM[0x9261-(p*0x20)]=0x23;
                                else break;
                        }

                        osd_fclose(f);
                }

                return 1;
        }
        else return 0;  /* we can't load the hi scores yet */

}

static void starforc_hisave(void)
{
        void *f;
        int i;
        unsigned char *RAM = Machine->memory_region[Machine->drv->cpu[0].memory_region];


        /* Bug to solve the problem about resetting in the hi-score screen */
        for (i = 0x8039; i < 0x80a0; i+=0x0b)
                        if (RAM[i] == 0x02) RAM[i] = 0x01;

        if ((RAM[0x8038] != 0) &&
            ((f = osd_fopen(Machine->gamedrv->name,0,OSD_FILETYPE_HIGHSCORE,1)) != 0))


        {
                osd_fwrite(f,&RAM[0x8038],112);

                osd_fclose(f);
        }
}



struct GameDriver senjyo_driver =
{
	__FILE__,
	0,
	"senjyo",
	"Senjyo",
	"1983",
	"Tehkan",
	"Mirko Buffoni\nNicola Salmoria\n",
	0,
	&senjyo_machine_driver,
	senjyo_init,

	senjyo_rom,
	0, 0,
	0,
	0,      /* sound_prom */

	senjyo_input_ports,

	0, 0, 0,
	ORIENTATION_ROTATE_90,

	0, 0
};

struct GameDriver starforc_driver =
{
	__FILE__,
	0,
	"starforc",
	"Star Force",
	"1984",
	"Tehkan",
	"Mirko Buffoni\nNicola Salmoria\nTatsuyuki Satoh\nJuan Carlos Lorente\nMarco Cassili",
	0,
	&starforc_machine_driver,
	starforc_init,

	starforc_rom,
	0, 0,
	0,
	0,      /* sound_prom */

	starforc_input_ports,

	0, 0, 0,
	ORIENTATION_ROTATE_90,

	starforc_hiload, starforc_hisave
};

struct GameDriver starfore_driver =
{
	__FILE__,
	&starforc_driver,
	"starfore",
	"Star Force (encrypted)",
	"1984",
	"Tehkan",
	"Mirko Buffoni\nNicola Salmoria\nTatsuyuki Satoh\nJuan Carlos Lorente\nMarco Cassili",
	0,
	&starforc_machine_driver,
	starfore_init,

	starfore_rom,
	0, suprloco_decode,
	0,
	0,      /* sound_prom */

	starforc_input_ports,

	0, 0, 0,
	ORIENTATION_ROTATE_90,

	starforc_hiload, starforc_hisave
};

struct GameDriver megaforc_driver =
{
	__FILE__,
	&starforc_driver,
	"megaforc",
	"Mega Force",
	"1985",
	"Tehkan (Video Ware license)",
	"Mirko Buffoni\nNicola Salmoria\nTatsuyuki Satoh\nJuan Carlos Lorente\nMarco Cassili",
	0,
	&starforc_machine_driver,
	starforc_init,

	megaforc_rom,
	0, 0,
	0,
	0,      /* sound_prom */

	starforc_input_ports,

	0, 0, 0,
	ORIENTATION_ROTATE_90,

	starforc_hiload, starforc_hisave
};
