/***************************************************************************

Mappy memory map (preliminary)

CPU #1:
0000-07ff video RAM
0800-0fff color RAM
1000-177f RAM
1780-17ff sprite data 1 (sprite number & color)
1800-1f7f RAM
1f80-1fff sprite data 2 (x, y position)
2000-277f RAM
2780-27ff sprite data 3 (high bit of y, flip flags, double-size flags)
3800-3fff scroll register map
4040-43ff RAM shared with CPU #2
4800-480f custom I/O chip #1
4810-481f custom I/O chip #2
5002-5003 IRQ enable
500a-500b CPU #2 enable
8000      watchdog timer
a000-ffff ROM

CPU #2:
0000-0040 sound registers
0040-03ff RAM shared with CPU #1
2000-2001 IRQ enable
2006-2007 sound enable
e000-ffff ROM

Interrupts:
CPU #1 IRQ generated by VBLANK
CPU #2 IRQ generated by VBLANK

***************************************************************************/

#include "driver.h"
#include "vidhrdw/generic.h"

/* machine driver data & functions */
extern unsigned char *mappy_sharedram;
extern unsigned char *mappy_customio_1,*mappy_customio_2;
extern int mappy_init_machine(const char *gamename);
extern int mappy_sharedram_r(int offset);
extern int mappy_sharedram_r2(int offset);
extern int mappy_cpu1ram_r(int offset);
extern void mappy_sharedram_w(int offset,int data);
extern void mappy_customio_w_1(int offset,int data);
extern void mappy_customio_w_2(int offset,int data);
extern int mappy_customio_r_1(int offset);
extern int mappy_customio_r_2(int offset);
extern int mappy_interrupt_1(void);
extern int mappy_interrupt_2(void);
extern void mappy_interrupt_enable_1_w(int offset,int data);
extern void mappy_interrupt_enable_2_w(int offset,int data);
extern void mappy_cpu_enable_w(int offset,int data);

/* video driver data & functions */
extern int mappy_vh_start(void);
extern void mappy_vh_stop(void);
extern void mappy_vh_screenrefresh(struct osd_bitmap *bitmap);
extern void mappy_videoram_w(int offset,int data);
extern void mappy_colorram_w(int offset,int data);
extern void mappy_scroll_w(int offset,int data);
extern void mappy_vh_convert_color_prom(unsigned char *palette, unsigned char *colortable,const unsigned char *color_prom);

/* sound driver data & functions */
extern unsigned char *mappy_soundregs;
extern void mappy_sh_update(void);
extern void mappy_sound_w(int offset,int data);
extern void mappy_sound_enable_w(int offset,int data);

/* CPU 1 read addresses */
static struct MemoryReadAddress readmem_cpu1[] =
{
	{ 0xa000, 0xffff, MRA_ROM },                                 /* ROM code */
	{ 0x4040, 0x43ff, MRA_RAM, &mappy_sharedram },               /* shared RAM with the sound CPU */
	{ 0x4800, 0x480f, mappy_customio_r_1, &mappy_customio_1 },   /* custom I/O chip #1 interface */
	{ 0x4810, 0x481f, mappy_customio_r_2, &mappy_customio_2 },   /* custom I/O chip #2 interface */
	{ 0x0000, 0x9fff, mappy_cpu1ram_r },                         /* RAM everywhere else */

	{ -1 }	/* end of table */
};

/* CPU 1 write addresses */
static struct MemoryWriteAddress writemem_cpu1[] =
{
	{ 0x1000, 0x177f, MWA_RAM },                                 /* general RAM, area 1 */
	{ 0x1800, 0x1f7f, MWA_RAM },                                 /* general RAM, area 2 */
	{ 0x2000, 0x277f, MWA_RAM },                                 /* general RAM, area 3 */
	{ 0x4040, 0x43ff, MWA_RAM },                                 /* shared RAM with the sound CPU */
	{ 0x0000, 0x07ff, mappy_videoram_w, &videoram },             /* video RAM */
	{ 0x0800, 0x0fff, mappy_colorram_w, &colorram },             /* color RAM */
	{ 0x1780, 0x17ff, MWA_RAM, &spriteram },                     /* sprite RAM, area 1 */
	{ 0x1f80, 0x1fff, MWA_RAM, &spriteram_2 },                   /* sprite RAM, area 2 */
	{ 0x2780, 0x27ff, MWA_RAM, &spriteram_3 },                   /* sprite RAM, area 3 */
	{ 0x3800, 0x3fff, mappy_scroll_w },                          /* scroll registers */
	{ 0x4800, 0x480f, mappy_customio_w_1 },                      /* custom I/O chip #1 interface */
	{ 0x4810, 0x481f, mappy_customio_w_2 },                      /* custom I/O chip #2 interface */
	{ 0x5002, 0x5003, mappy_interrupt_enable_1_w },              /* interrupt enable */
	{ 0x500a, 0x500b, mappy_cpu_enable_w },                      /* sound CPU enable */
	{ 0x8000, 0x8000, MWA_NOP },                                 /* watchdog timer */
	{ 0xa000, 0xffff, MWA_ROM },                                 /* ROM code */

	{ -1 }	/* end of table */
};

/* CPU 2 read addresses */
static struct MemoryReadAddress readmem_cpu2[] =
{
	{ 0xe000, 0xffff, MRA_ROM },                                 /* ROM code */
	{ 0x0040, 0x03ff, mappy_sharedram_r2 },                      /* shared RAM with the main CPU */

	{ -1 }	/* end of table */
};

/* CPU 2 write addresses */
static struct MemoryWriteAddress writemem_cpu2[] =
{
	{ 0x0040, 0x03ff, mappy_sharedram_w },                       /* shared RAM with the main CPU */
	{ 0x0000, 0x003f, mappy_sound_w, &mappy_soundregs },         /* sound control registers */
	{ 0x2000, 0x2001, mappy_interrupt_enable_2_w },              /* interrupt enable */
	{ 0x2006, 0x2007, mappy_sound_enable_w },                    /* sound enable */
	{ 0xe000, 0xffff, MWA_ROM },                                 /* ROM code */

	{ -1 }	/* end of table */
};


/* input from the outside world */
static struct InputPort input_ports[] =
{
	{	/* DSW1 */
		0x00,
		{ 0, 0, 0, 0, 0, 0, OSD_KEY_F2, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0 }
	},
	{	/* DSW2 */
		0x00,
		{ 0, 0, 0, 0, 0, 0, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0 }
	},
	{	/* IN0 */
		0x00,
		{ 0, OSD_KEY_RIGHT, 0, OSD_KEY_LEFT, OSD_KEY_CONTROL, 0, 0, 0 },
		{ 0, OSD_JOY_RIGHT, 0, OSD_JOY_LEFT, OSD_JOY_FIRE, 0, 0, 0 },
	},
	{	/* IN1 */
		0x00,
		{ OSD_KEY_3, 0, 0, 0, OSD_KEY_1, OSD_KEY_2, 0, 0 },
		{ 0, 0, 0, 0, 0, 0, 0, 0 },
	},
	{ -1 }	/* end of table */
};

static struct TrakPort trak_ports[] =
{
        { -1 }
};

/* key descriptions and links */
static struct KEYSet keys[] =
{
    { 2, 3, "MOVE LEFT"  },
    { 2, 1, "MOVE RIGHT" },
    { 2, 4, "OPEN DOOR"  },
    { -1 }
};


/* dipswitch menu data */
static struct DSW mappy_dsw[] =
{
	{ 0, 0x03, "DIFFICULTY", { "A", "B", "C", "D" }, 0 },
	{ 0, 0x20, "DEMO SOUND", { "ON", "OFF" }, 1 },
	{ 1, 0x07, "CREDITS",    { "1 COIN 1 CRED", "1 COIN 2 CRED", "1 COIN 3 CRED", "1 COIN 6 CRED", "2 COINS 1 CRED", "2 COINS 3 CRED", "3 COINS 1 CRED", "3 COINS 2 CRED" }, 0 },
	{ 1, 0x38, "BONUS",      { "20K 70K", "20K 60K", "20K 80K", "30K 100K", "20K NONE", "20K 70K 70K", "20K 80K 80K", "NONE" }, 0 },
	{ 1, 0xc0, "LIVES",      { "3", "5", "1", "2" }, 0, },
	{ -1 }
};


/* layout of the 8x8x2 character data */
static struct GfxLayout charlayout =
{
	8,8,	         /* 8*8 characters */
	256,	         /* 256 characters */
	2,             /* 2 bits per pixel */
	{ 0, 4 },      /* the two bitplanes for 4 pixels are packed into one byte */
	{ 7*8, 6*8, 5*8, 4*8, 3*8, 2*8, 1*8, 0*8 },   /* characters are rotated 90 degrees */
	{ 8*8+0, 8*8+1, 8*8+2, 8*8+3, 0, 1, 2, 3 },   /* bits are packed in groups of four */
	16*8	       /* every char takes 16 bytes */
};

/* layout of the 16x16x4 sprite data */
static struct GfxLayout spritelayout =
{
	16,16,	     /* 16*16 sprites */
	128,	        /* 128 sprites */
	4,	           /* 4 bits per pixel */
	{ 0, 4, 8192*8, 8192*8+4 },	/* the two bitplanes for 4 pixels are packed into one byte */
	{ 39 * 8, 38 * 8, 37 * 8, 36 * 8, 35 * 8, 34 * 8, 33 * 8, 32 * 8,
			7 * 8, 6 * 8, 5 * 8, 4 * 8, 3 * 8, 2 * 8, 1 * 8, 0 * 8 },
	{ 0, 1, 2, 3, 8*8, 8*8+1, 8*8+2, 8*8+3, 16*8+0, 16*8+1, 16*8+2, 16*8+3,
			24*8+0, 24*8+1, 24*8+2, 24*8+3 },
	64*8	/* every sprite takes 64 bytes */
};

/* pointers to the appropriate memory locations and their associated decode structs */
static struct GfxDecodeInfo gfxdecodeinfo[] =
{
	{ 1, 0x0000, &charlayout,       0, 128 },
	{ 1, 0x1000, &spritelayout, 128*4,  32 },
	{ -1 } /* end of array */
};


/* the palette used -- these are all guesses until we can get the color PROMs */
static unsigned char mappy_palette[] =
{
	0x00, 0x00, 0x00,	/* 00 black */
	0xff, 0x00, 0x00,	/* 01 red */
	0xde, 0x97, 0x47,	/* 02 brown */
	0xff, 0xb8, 0xde,	/* 03 pink */
	0x00, 0x00, 0x00,	/* 04 */
	0x00, 0xff, 0xde,	/* 05 cyan */
	0x47, 0x97, 0xde,	/* 06 lt blue */
	0xff, 0xb8, 0x47,	/* 07 orange */
	0xff, 0x00, 0xff,	/* 08 */
	0xff, 0xff, 0x00,	/* 09 yellow */
	0xb8, 0xb8, 0xb8,	/* 0A gray */
	0x21, 0x47, 0xde,	/* 0B dk blue */
	0x21, 0xff, 0x21,	/* 0C green */
	0x47, 0xb8, 0x97,	/* 0D bluish */
	0xde, 0xde, 0x97,	/* 0E tan */
	0xff, 0xff, 0xff	/* 0F white */
};


/* the color table used -- these are all guesses until we can get the color PROMs */
static unsigned char mappy_color_table[] =
{
	/* ---------- char colors --------------- */
	14,2,12,0,		/* 00 - BORDERS */
	14,2,11,0,		/* 01 - MICROPOLICE */
	14,2,9,0,		/* 02 - GORO */
	14,2,1,0,		/* 03 - NAMCO text ... BIG BIT */
	0,1,1,0,
	0,2,2,0,
	0,3,3,0,
	0,0,15,0,		/* 07 - NAMCO PRESENTS ... COPYRIGHT ... ALL RIGHTS RESERVED ... MAPPY */
	0,6,6,0,
	0,15,15,0,		/* 09 - used for the grid ... AS */
	14,2,14,0,		/* 0A - OPEN DOORS */
	0,7,7,0,			/* 0B - MEOWKY */
	0,3,3,0,			/* 0C - NAUGHTY FOLK */
	0,5,5,0,			/* 0D - HIGHLIGHT COLOR FOR HI SCORE ENTERING */
	14,5,2,0,		/* 0E - OPEN MICROWAVE DOORS */
	6,15,9,0,		/* 0F - MAPPY LIFE SYMBOL */
	2,1,1,0,			/* 10-17 - COLOR CYCLING FOR MICROWAVE DOORS */
	2,3,3,0,
	2,7,7,0,
	2,15,15,0,
	2,9,9,0,
	2,15,15,0,
	2,14,14,0,
	2,15,15,0,
	12,15,1,0,		/* 18 - BALLON FOR LEVEL SYMBOL*/
	14,5,6,0,		/* 19 - BIG BALLOON FOR LEVEL */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 20 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 28 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 30 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 38 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	0,12,12,0,		/* 40 - STARRING */
	1,14,3,0,		/* 41 - HOUSES */
	0,9,9,0,			/* 42 - CREDIT */
	0,1,1,0,			/* 43 - SCORE ... 1UP */
	14,2,6,0,		/* 44 - LEVEL 4 HOUSES */
	14,2,1,0,		/* 45 - LEVEL 1 HOUSES */
	14,2,12,0,		/* 46 - LEVEL 2 HOUSES */
	15,0,0,15,		/* 47 - clear background */
	1,2,3,0,			/* 48 */
	0,9,9,0,			/* 49 - SCORE value */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 50 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 58 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 60 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 68 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 70 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,			/* 78 */
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	1,2,3,0,
	
	/* --------- sprite colors --------------- */
	0,6,10,2,9,11,3,7,12,1,10,2,5,3,15,0,		/* general palette */
	0,12,10,2,9,11,3,7,1,6,10,11,12,3,15,0,	/* color cycle 1 */
	0,1,10,2,9,11,3,7,6,12,10,11,12,3,15,0,	/* color cycle 2 */
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,11,10,1,1,11,0,7,10,1,0,11,12,11,14,0,	/* M in Mappy */
	0,10,0,10,11,0,10,0,10,0,11,0,0,11,0,0,	/* M/A erase */
	0,11,10,7,7,11,0,7,10,1,0,10,1,11,14,0,	/* A in Mappy */
	0,10,0,10,11,0,10,0,10,0,11,11,10,11,0,0,	/* P/P erase */
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,				/* blink -- all off? */
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,
	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,0,
};


/* waveforms for the audio hardware -- these don't sound right, they're just guesses */
static unsigned char samples[8*32] =
{
	0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,
	0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0x88,

	0xff,0x22,0x44,0x55,0x66,0x55,0x44,0x22,0xff,0xcc,0xaa,0x99,0x88,0x99,0xaa,0xcc,
	0xff,0x33,0x55,0x66,0x55,0x33,0xff,0xbb,0x99,0x88,0x99,0xbb,0xff,0x66,0xff,0x88,

	0xff,0x22,0x44,0x55,0x66,0x55,0x44,0x22,0xff,0xcc,0xaa,0x99,0x88,0x99,0xaa,0xcc,
	0xff,0x33,0x55,0x66,0x55,0x33,0xff,0xbb,0x99,0x88,0x99,0xbb,0xff,0x66,0xff,0x88,

	0x33,0x55,0x66,0x55,0x44,0x22,0x00,0x00,0x00,0x22,0x44,0x55,0x66,0x55,0x33,0x00,
	0xcc,0xaa,0x99,0xaa,0xbb,0xdd,0xff,0xff,0xff,0xdd,0xbb,0xaa,0x99,0xaa,0xcc,0xff,

	0xff,0x22,0x44,0x55,0x66,0x55,0x44,0x22,0xff,0xcc,0xaa,0x99,0x88,0x99,0xaa,0xcc,
	0xff,0x33,0x55,0x66,0x55,0x33,0xff,0xbb,0x99,0x88,0x99,0xbb,0xff,0x66,0xff,0x88,

	0x33,0x55,0x66,0x55,0x44,0x22,0x00,0x00,0x00,0x22,0x44,0x55,0x66,0x55,0x33,0x00,
	0xcc,0xaa,0x99,0xaa,0xbb,0xdd,0xff,0xff,0xff,0xdd,0xbb,0xaa,0x99,0xaa,0xcc,0xff,

	0xff,0x00,0x22,0x44,0x66,0x55,0x44,0x44,0x33,0x22,0x00,0xff,0xdd,0xee,0xff,0x00,
	0x00,0x11,0x22,0x33,0x11,0x00,0xee,0xdd,0xcc,0xcc,0xbb,0xaa,0xcc,0xee,0x00,0x11,

	0x33,0x55,0x66,0x55,0x44,0x22,0x00,0x00,0x00,0x22,0x44,0x55,0x66,0x55,0x33,0x00,
	0xcc,0xaa,0x99,0xaa,0xbb,0xdd,0xff,0xff,0xff,0xdd,0xbb,0xaa,0x99,0xaa,0xcc,0xff,
};


/* the machine driver: 2 6809s running at 1MHz */
static struct MachineDriver machine_driver =
{
	/* basic machine hardware */
	{
		{
			CPU_M6809,
			1100000,			/* 1.1 Mhz */
			0,
			readmem_cpu1,writemem_cpu1,0,0,
			mappy_interrupt_1,1
		},
		{
			CPU_M6809,
			1100000,			/* 1.1 Mhz */
			2,	/* memory region #2 */
			readmem_cpu2,writemem_cpu2,0,0,
			mappy_interrupt_2,1
		}
	},
	60,
	mappy_init_machine,

	/* video hardware */
	28*8, 36*8, { 0*8, 28*8-1, 0*8, 36*8-1 },
	gfxdecodeinfo,
	128+32,128*4+32*16,
	0,

	0,
	mappy_vh_start,
	mappy_vh_stop,
	mappy_vh_screenrefresh,

	/* sound hardware */
	samples,
	0,
	0,
	0,
	mappy_sh_update
};


/* ROM loader description */
ROM_START( mappy_rom )
	ROM_REGION(0x10000)	/* 64k for code for the first CPU  */
	ROM_LOAD( "mappy1d.64", 0xa000, 0x2000 )
	ROM_LOAD( "mappy1c.64", 0xc000, 0x2000 )
	ROM_LOAD( "mappy1b.64", 0xe000, 0x2000 )

	ROM_REGION(0x5000)	/* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "mappy3b.32", 0x0000, 0x1000 )
	ROM_LOAD( "mappy3m.64", 0x1000, 0x2000 )
	ROM_LOAD( "mappy3n.64", 0x3000, 0x2000 )

	ROM_REGION(0x10000)	/* 64k for the second CPU */
	ROM_LOAD( "mappy1k.64", 0xe000, 0x2000 )
ROM_END


/* load the high score table */
static int hiload(const char *name)
{
   int writing = 0;
   FILE *f;

   /* get RAM pointer (this game is multiCPU, we can't assume the global */
   /* RAM pointer is pointing to the right place) */
   unsigned char *RAM = Machine->memory_region[0];

   /* check if the hi score table has already been initialized */
   if (memcmp(&RAM[0x1465],"BEH",3) == 0 &&          /* check for high score initials */
   	memcmp(&RAM[0x1385],"\0\x20\0",3) == 0 &&    /* check for main high score value */
   	memcmp(&RAM[0x7ed],"\0\0\0",3) == 0)         /* see if main high score was written to screen */
   {
      if ((f = fopen(name,"rb")) != 0)
      {
         fread(&RAM[0x1460],1,40,f);
         fclose(f);

         /* also copy over the high score */
         RAM[0x1385] = RAM[0x1460];
         RAM[0x1386] = RAM[0x1461];
         RAM[0x1387] = RAM[0x1462];
      }

      /* this is a little gross, but necessary to get the high score on-screen */
      if (!writing) writing = (RAM[0x1385] >> 4);
      if ( writing) mappy_videoram_w (0x7f3, (RAM[0x1385] >> 4));
      if (!writing) writing = (RAM[0x1385] & 0x0f);
      if ( writing) mappy_videoram_w (0x7f2, (RAM[0x1385] & 0x0f));
      if (!writing) writing = (RAM[0x1386] >> 4);
      if ( writing) mappy_videoram_w (0x7f1, (RAM[0x1386] >> 4));
      if (!writing) writing = (RAM[0x1386] & 0x0f);
      if ( writing) mappy_videoram_w (0x7f0, (RAM[0x1386] & 0x0f));
      if (!writing) writing = (RAM[0x1387] >> 4);
      if ( writing) mappy_videoram_w (0x7ef, (RAM[0x1387] >> 4));
      if (!writing) writing = (RAM[0x1387] & 0x0f);
      if ( writing) mappy_videoram_w (0x7ee, (RAM[0x1387] & 0x0f));
      mappy_videoram_w (0x7ed, 0);

      return 1;
   }
   else return 0; /* we can't load the hi scores yet */
}


/* save the high score table */
static void hisave(const char *name)
{
   FILE *f;

   /* get RAM pointer (this game is multiCPU, we can't assume the global */
   /* RAM pointer is pointing to the right place) */
   unsigned char *RAM = Machine->memory_region[0];

   if ((f = fopen(name,"wb")) != 0)
   {
      fwrite(&RAM[0x1460],1,40,f);
      fclose(f);
   }
}


/* the core game driver */
struct GameDriver mappy_driver =
{
	"Mappy",
	"mappy",
	"AARON GILES\nMIRKO BUFFONI",
	&machine_driver,

	mappy_rom,
	0, 0,
	0,

	input_ports, trak_ports, mappy_dsw, keys,

	0,			/* color prom -- I wish I had one! */
	mappy_palette,		/* palette */
	mappy_color_table,	/* color table */

	8*11, 8*20,

	hiload, hisave
};
