/***************************************************************************

New Super Pac-Man memory map (preliminary)

CPU #1:
0000-03ff video RAM
0400-07ff color RAM
0800-0f7f RAM
0f80-0fff sprite data 1 (sprite number & color)
1000-177f RAM
1780-17ff sprite data 2 (x, y position)
1800-1f7f RAM
1f80-1fff sprite data 3 (high bit of y, flip flags, double-size flags)
2000      watchdog timer?
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
5008-5009 sound enable
500a-500b CPU #2 enable
8000      watchdog timer
c000-ffff ROM

CPU #2:
0000-0040 sound registers
0040-03ff RAM shared with CPU #1
f000-ffff ROM

Interrupts:
CPU #1 IRQ generated by VBLANK
CPU #2 uses no interrupts

***************************************************************************/

/***************************************************************************

Super Pacman (preliminary)
(from Kevin Brisley's superpac.keg)

Game CPU

0000-03ff Video RAM
0400-07ff Color RAM
c000-dfff ROM SPC-2.1C
e000-ffff ROM SPC-1.1B


memory mapped ports:

read:
10c6      DSW-2         BITS 0-3: Difficulty  BIT 6: Sound  BIT 7: Screen
10c5      DSW-3			BITS 0-2: Coins/Credit  BITS 3-5: Bonus Pacman  BITS 6-7: Starting Pacmen
10c0	  Insert Coin   BIT 0: Insert coin
10c1      Start Game    BIT 0: 1 Player Start  BIT 1: 2 Player Start
10c2	  Controls		BIT 0: UP  BIT 1: RIGHT  BIT 2: DOWN  BIT 3: LEFT  BIT 5: Speedup


write:
0819-0f19 56 sets of 32 bytes
		  the first byte contains size and flip information: x flip (bit 1), y flip (bit 0)
		  	 big sprite (bits 2-3?)
		  the second byte contains the sprite image number
		  the third byte contains the color info
		  the fourth byte contains the X position
		  the fifth byte contains the Y position
1619-16?? at least 2 sets of 32 bytes
		  the first byte contains size and flip: x flip: (bit 1), y flip (bit 2)
		     big sprite (bits 2-3?)
		  the second byte contains the sprite image number
		  the third byte contains the color info
		  the fourth byte contains the X position
		  the fifth byte contains the Y position
8000	  Watchdog reset

I/O ports:


***************************************************************************/

#include "driver.h"
#include "vidhrdw/generic.h"

extern unsigned char *mappy_soundregs;
extern void mappy_sound_enable_w(int offset,int data);
extern void mappy_sound_w(int offset,int data);
extern void mappy_sh_update(void);

extern unsigned char *superpac_sharedram;
extern unsigned char *superpac_customio_1,*superpac_customio_2;
extern int superpac_customio_r(int offset);
extern int superpac_sharedram_r(int offset);
extern int superpac_sharedram_r2(int offset);
extern void superpac_sharedram_w(int offset,int data);
extern void superpac_customio_w_1(int offset,int data);
extern void superpac_customio_w_2(int offset,int data);
extern int superpac_customio_r_1(int offset);
extern int superpac_customio_r_2(int offset);
extern void superpac_interrupt_enable_1_w(int offset,int data);
extern void superpac_cpu_enable_w(int offset,int data);
extern int superpac_interrupt_1(void);
extern int superpac_interrupt_2(void);

extern int superpac_vh_start(void);
extern void superpac_vh_screenrefresh(struct osd_bitmap *bitmap);
extern int superpac_init_machine(const char *gamename);


/* CPU 1 read addresses */
static struct MemoryReadAddress readmem_cpu1[] =
{
	{ 0x0000, 0x1fff, MRA_RAM },                                       /* general RAM */
	{ 0x4040, 0x43ff, superpac_sharedram_r, &superpac_sharedram },     /* shared RAM */
	{ 0x4800, 0x480f, superpac_customio_r_1, &superpac_customio_1 },   /* custom I/O chip #1 interface */
	{ 0x4810, 0x481f, superpac_customio_r_2, &superpac_customio_2 },   /* custom I/O chip #2 interface */
	{ 0xc000, 0xffff, MRA_ROM },                        /* SPC-2.1C at 0xc000, SPC-1.1B at 0xe000 */
	{ -1 }                                              /* end of table */
};


/* CPU 1 write addresses */
static struct MemoryWriteAddress writemem_cpu1[] =
{
	{ 0x0000, 0x03ff, videoram_w, &videoram, &videoram_size },          /* video RAM */
	{ 0x0400, 0x07ff, colorram_w, &colorram },          /* color RAM */
	{ 0x0800, 0x0f7f, MWA_RAM },                        /* RAM */
	{ 0x0f80, 0x0fff, MWA_RAM, &spriteram },            /* sprite RAM, area 1 */
	{ 0x1000, 0x177f, MWA_RAM },                        /* RAM */
	{ 0x1780, 0x17ff, MWA_RAM, &spriteram_2 },          /* sprite RAM, area 2 */
	{ 0x1800, 0x1f7f, MWA_RAM },                        /* RAM */
	{ 0x1f80, 0x1fff, MWA_RAM, &spriteram_3 },          /* sprite RAM, area 3 */
	{ 0x2000, 0x2000, MWA_NOP },                        /* watchdog timer */
	{ 0x4040, 0x43ff, MWA_RAM },                        /* shared RAM */
	{ 0x4800, 0x480f, superpac_customio_w_1 },          /* custom I/O chip #1 interface */
	{ 0x4810, 0x481f, superpac_customio_w_2 },          /* custom I/O chip #2 interface */
	{ 0x5002, 0x5003, superpac_interrupt_enable_1_w },  /* interrupt enable */
	{ 0x5008, 0x5009, mappy_sound_enable_w },           /* sound enable */
	{ 0x500a, 0x500b, superpac_cpu_enable_w },          /* interrupt enable */
	{ 0x8000, 0x8000, MWA_NOP },                        /* watchdog timer */
	{ 0xc000, 0xffff, MWA_ROM },                        /* SPC-2.1C at 0xc000, SPC-1.1B at 0xe000 */
	{ -1 }                                              /* end of table */
};


/* CPU 2 read addresses */
static struct MemoryReadAddress readmem_cpu2[] =
{
	{ 0xf000, 0xffff, MRA_ROM },                        /* ROM code */
	{ 0x0040, 0x03ff, superpac_sharedram_r2 },          /* shared RAM with the main CPU */
	{ -1 }                                              /* end of table */
};


/* CPU 2 write addresses */
static struct MemoryWriteAddress writemem_cpu2[] =
{
	{ 0x0040, 0x03ff, superpac_sharedram_w },           /* shared RAM with the main CPU */
	{ 0x0000, 0x003f, mappy_sound_w, &mappy_soundregs },/* sound control registers */
	{ 0xf000, 0xffff, MWA_ROM },                        /* ROM code */
	{ -1 }                                              /* end of table */
};


/* input ports & dip switches */
static struct InputPort superpac_input_ports[] =
{
	{	/* DSW1 */
		0x00,
		{ 0, 0, 0, 0, 0, 0, 0, 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,
		{ OSD_KEY_UP, OSD_KEY_RIGHT, OSD_KEY_DOWN, OSD_KEY_LEFT, 0, 
OSD_KEY_CONTROL, 0, 0 },
		{ OSD_JOY_UP, OSD_JOY_RIGHT, OSD_JOY_DOWN, OSD_JOY_LEFT, 0, 
OSD_JOY_FIRE, 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 }
};


/* keyboard info -- is fire really only used for high scores?? */
static struct KEYSet keys[] =
{
        { 2, 0, "MOVE UP" },
        { 2, 3, "MOVE LEFT"  },
        { 2, 1, "MOVE RIGHT" },
        { 2, 2, "MOVE DOWN" },
        { 2, 5, "FIRE" },
        { -1 }
};


/* dip switches... */
static struct DSW superpac_dsw[] =
{
	{ 0, 0x0F, "DIFFICULTY", { "R0 NORMAL","R1 EASIEST","R2","R3","R4","R5","R6","R7","R8 DEFAULT",\
								"R9","RA","RB HARDEST","RC EASIEST AUTO","RD","RE","RF HARDEST AUTO" } },
	{ 0, 0x40, "DEMO SOUND", { "ON", "OFF" }, 1 },
	{ 1, 0x07, "COINS", { "1 COIN 1 CREDIT", "1 COIN 2 CREDIT", "1 COIN 3 CREDIT",\
						 "1 COIN 6 CREDIT", "1 COIN 7 CREDIT", "2 COIN 1 CREDIT", \
						 "2 COIN 3 CREDIT", "3 COIN 1 CREDIT" } },
	{ 1, 0x38, "BONUS AT", { "30000 100000", "30000 80000", "30000 120000", "30000 80000@@@",\
							 "30000 100000@@@", "30000 120000@@@", "80000", "NONE" } },
	{ 1, 0xc0, "LIVES", { "3", "1", "2", "5" } },
	{ -1 }
};


/* SUPERPAC -- ROM SPV-1.3C (4K) */
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 */
};


/* SUPERPAC -- ROM SPV-2.3F (8K) */
static struct GfxLayout spritelayout =
{
	16,16,                                         /* 16*16 sprites */
	128,                                           /* 128 sprites */
	2,                                             /* 2 bits per pixel */
	{ 0, 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 */
};


/* bumped color table to 69 entries -- see color table for details */
static struct GfxDecodeInfo gfxdecodeinfo[] =
{
	{ 1, 0x0000, &charlayout,      0, 69 },
	{ 1, 0x1000, &spritelayout, 32*4, 37 },
	{ -1 } /* end of array */
};


static unsigned char superpac_palette[] =
{
	0x00, 0x00, 0x00,	/* 00 black */
	0xff, 0x00, 0x00,	/* 01 red */
	0xde, 0x97, 0x47,	/* 02 tan */
	0xff, 0xb8, 0xde,	/* 03 pink */
	0x00, 0x00, 0x00,	/* 04 black */
	0x00, 0xff, 0xde,	/* 05 lt blue*/
	0x47, 0xb8, 0xde,	/* 06 sky blue?*/
	0xff, 0xb8, 0x47,	/* 07 orange */
	0x00, 0x00, 0x00,	/* 08 black */
	0xff, 0xff, 0x00,	/* 09 yellow */
	0x00, 0x00, 0x00,	/* 0A black */
	0x21, 0x21, 0xde,	/* 0B blue */
	0x00, 0xff, 0x00,	/* 0C green */
	0x47, 0xb8, 0x97,	/* 0D bluish */
	0xff, 0xb8, 0x97,	/* 0E peach */
	0xde, 0xde, 0xde	/* 0F white */
};


/* CHARACTERS											SPRITES
#00	??													power pill blink off
#01 bottom 2 lines of screen							pacman
#02 CREDIT 0, ROM test, score digits					blinking pacman, fruit pts,
#03 1UP,HIGH SCORE,GAME OVER,attract maze&doors			blinking ghosts
#04 POWER,SUPER,(C)notice,doors,maze,READY!				keys, red ghost
#05 ??													pink ghost
#06	??													lt blue ghost
#07	??													lt orange ghost
#08 ??													blue ghosts
#09 top of logo (containing super)						??
#0a bottom of logo (containing PAC-MAN)					dead ghosts (eyes)
#0b ??													power pill blink on
#0c level indicator apple								super pill (big)
#0d level indicator bananas								super pill (medium)
#0e level indicator donut								super pill (small)
#0f level indicator burger								super pill blink off
#10 ??													apple
#11 ??													bananas
#12 ??													donut
#13 ??													hamburger
#14 ??													fried egg
#15 ??													ear of corn
#16 ??													sneaker
#17 ??													cake
#18 ??													peach
#19 ??													melon
#1a ??													coffee cup
#1b ??													mushroom
#1c ??													ghost points, bell
#1d ??													clover
#1e ??													galaxian
#1f ??													gift
*/

/* Color table is a little confused because SP has several large values it uses in color RAM.
   These values were being reduced with a modulus operator, which caused them to try to use the
   same color entry as another element. Also, I could be mistaken but it looks like characters
   and sprites do not share the same color table. So I put the sprite color table at offset $20
   and expanded it to 37 entries. $20 is added to all sprite entries unless they are >$20.
   Large values (>$20) are:	SPRITES					USES ENTRY		SHARES ENTRY WITH
	   						Bonus Star:		$28 	sprite+$08			blue ghosts
	   						Key:			$44 	sprite+$24			n/a
	   						Ghost Pts:		$3C		sprite+$1C			bell

	   						CHARACTERS				USES ENTRY		SHARES ENTRY WITH
	   						Pacmen Remain. 	$41		sprite+$21			n/a
	   						Score Digits	$42		sprite+$22			n/a
	   						1UP,HIGH SCORE	$43		sprite+$23			n/a

*/

static unsigned char superpac_color_table[] =
{
	0x00,0x00/*off*/,0x0B,0x0c, /* power pill blink OFF */
	0x00,0x09,0x09,0x00, /* pacman, [PAUSED] */
	0x00,0x0f,0x0f,0x0f, /* score digits,CREDIT 0, ROM test */
	0x00,0x03/*doors*/,0x0f/*text*/,0x0b/*maze*/, /* attract screen maze & doors */
	0x00,0x03/*doors*/,0x0f/*text*/,0x0b/*maze*/, /* POWER,SUPER,(C)notice,maze&doors,PUSH START,STAGE 1,READY! */
	0x00,0x03/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* was ghost 1 */
	0x00,0x05/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* was ghost 2 */
	0x00,0x07/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* was ghost 3 */
	0x00,0x0b/*cloak*/,0x03/*mouth*/,0x0f, /* was blue ghosts */
	0x00,0x09,0x07,0x03, /* top of logo (containing SUPER) */
	0x00,0x09,0x07,0x01, /* bottom of logo (containing PAC-MAN)*/
	0x00,0x0f,0x0f,0x0f, /* maze/doors blink */
	0x00,0x01,0x02,0x0f, /* level indicator apple */
	0x00,0x09,0x0f,0x02, /* level indicator banana */
	0x00,0x02,0x07,0x0f, /* level indicator donut */
	0x00,0x02,0x0f,0x0c, /* level indicator burger*/
	0x00,0x0f,0x09,0x02, /* level indicator egg */
	0x00,0x02,0x09,0x0c, /* level indicator corn */
	0x00,0x01,0x03,0x0c, /* level indicator sneaker */
	0x00,0x0f,0x0e,0x01, /* level indicator cake */
	0x00,0x0e,0x03,0x0c, /* level indicator peach */
	0x00,0x0c,0x05,0x0b, /* level indicator melon */
	0x00,0x0e,0x0f,0x0b, /* level indicator coffee cup */
	0x00,0x07,0x02,0x0f, /* level indicator mushroom */
	0x00,0x09,0x0f,0x02, /* level indicator bell */
	0x00,0x0c,0x0f,0x0b, /* level indicator clover */
	0x00,0x01,0x09,0x0b, /* level indicator galaxian */
	0x00,0x0f,0x03,0x01, /* level indicator gift */
	0x00,0x0c,0x0c,0x0c, /* level indicator bosconian? */
	0x00,0x0c,0x0c,0x0c, /* ??? */
	0x00,0x0c,0x0c,0x0c, /* ??? */
	0x00,0x0c,0x0c,0x0c, /* ??? */
	/* ------begin sprites------ */
	0x00,0x00/*off*/,0x00/*eaten.ghst*/,0x0c, /* power pill blink OFF, ghost as eaten (invis.) */
	0x00,0x09,0x09,0x00, /* pacman */
	0x00,0x0f,0x0f,0x0f, /* fruit pts, blinking pacman */
	0x00,0x0f/*doors/bl.ghst*/,0x01/*text*/,0x0b/*maze*/, /* blinking ghost,1UP,HIGH SCORE,GAME OVER,attract maze,doors */
	0x00,0x01/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* ghost0 */
	0x00,0x03/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* ghost 1 */
	0x00,0x05/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* ghost 2 */
	0x00,0x07/*cloak*/,0x0F/*eyes*/,0x0B/*pupils*/, /* ghost 3 */
	0x00,0x0b/*cloak*/,0x03/*mouth*/,0x0f, /* blue ghosts, bonus star */
	0x00,0x09,0x07,0x03, /* top of logo (containing SUPER) */
	0x00,0x00,0x0f,0x0b, /* dead ghosts (eyes) */
	0x00,0x0e,0x00,0x0b, /* power pill blink ON */
	0x00,0x0c,0x0c,0x0c, /* super pill blink (big) */
	0x00,0x0c,0x0c,0x00, /* super pill blink (intermediate) */
	0x00,0x0c,0x00,0x00, /* super pill blink (small) */
	0x00,0x00,0x00,0x00, /* super pill blink OFF */
	0x00,0x01/*red*/,0x02/*tan*/,0x0F/*white*/,	/* apple */
	0x00,0x09/*yellow*/,0x0F/*white*/,0x02/*tan*/, /* bananas */
	0x00,0x02/*tan*/,0x07/*orange*/,0x0f/*white*/, /* donut */
	0x00,0x02/*topbun*/,0x0f/*bt.bun*/,0x0c/*lettuce*/, /* hamburger */
	0x00,0x0f/*white*/,0x09/*yolk*/,0x02/*hilite*/, /* fried egg */
	0x00,0x02/*tan*/,0x09/*yellow*/,0x0c/*green*/, /* ear of corn */
	0x00,0x01/*red*/,0x03/*pink*/,0x0c/*green*/, /* sneaker */
	0x00,0x0f/*white*/,0x0e/*peach*/,0x01/*red*/, /* cake */
	0x00,0x0e/*peach*/,0x03/*pink*/,0x0c/*green*/, /* peach */
	0x00,0x0c/*green*/,0x05/*lt.blue*/,0x0b/*blue*/, /* melon */
	0x00,0x0e/*peach*/,0x0f/*white*/,0x0b/*blue*/, /* coffee cup */
	0x00,0x07/*orange*/,0x02/*tan*/,0x0f/*white*/, /* mushroom */
	0x00,0x09/*yellow*/,0x0f/*pts/white*/,0x02/*tan*/, /* ghost pts, bell */
	0x00,0x0c/*green*/,0x0f/*white*/,0x0b/*blue*/, /* clover */
	0x00,0x01/*red*/,0x09/*yellow*/,0x0b/*blue*/, /* galaxian */
	0x00,0x0f/*white*/,0x03/*pink*/,0x01/*red*/, /* gift package */
	/* -------miscellaneous------- */
	0x00,0x0c,0x0c,0x0c, /* Bosconian? */
	0x00,0x09,0x0c,0x0c, /* bottom 2 rows (pacmen remaining) */
	0x00,0x0f,0x0f,0x0f, /* 2nd row text -- score digits */
	0x00,0x01,0x01,0x01, /* top row text -- 1 UP, HIGH SCORE */
	0x00,0x05,0x0f,0x0c  /* keys */
};

/* waveforms for the audio hardware */
static unsigned char samples[8*32] =
{
	0xff,0x11,0x22,0x33,0x44,0x55,0x55,0x66,0x66,0x66,0x55,0x55,0x44,0x33,0x22,0x11,
	0xff,0xdd,0xcc,0xbb,0xaa,0x99,0x99,0x88,0x88,0x88,0x99,0x99,0xaa,0xbb,0xcc,0xdd,

	0xff,0x11,0x22,0x33,0x44,0x55,0x55,0x66,0x66,0x66,0x55,0x55,0x44,0x33,0x22,0x11,
	0xff,0xdd,0xcc,0xbb,0xaa,0x99,0x99,0x88,0x88,0x88,0x99,0x99,0xaa,0xbb,0xcc,0xdd,

	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,

	0xff,0x55,0x33,0x00,0x33,0x55,0x11,0xee,0x33,0x66,0x44,0xff,0x11,0x22,0xee,0xaa,
	0xff,0x44,0x00,0xcc,0xdd,0xff,0xaa,0x88,0xbb,0x00,0xdd,0x99,0xbb,0xee,0xbb,0x99,

	0xff,0x66,0x44,0x11,0x44,0x66,0x22,0xff,0x44,0x77,0x55,0x00,0x22,0x33,0xff,0xaa,
	0x00,0x55,0x11,0xcc,0xdd,0xff,0xaa,0x88,0xbb,0x00,0xdd,0x99,0xbb,0xee,0xbb,0x99,

	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,

	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,
};


static struct MachineDriver machine_driver =
{
	/* basic machine hardware  */
	{
		{
			CPU_M6809,
			1100000,             /* 1.1 Mhz */
			0,                   /* memory region */
			readmem_cpu1,        /* MemoryReadAddress */
			writemem_cpu1,       /* MemoryWriteAddress */
			0,                   /* IOReadPort */
			0,                   /* IOWritePort */
			interrupt,           /* interrupt routine */
			1                    /* interrupts per frame */
		},
		{
			CPU_M6809,
			1100000,             /* 1.1 Mhz */
			2,                   /* memory region */
			readmem_cpu2,        /* MemoryReadAddress */
			writemem_cpu2,       /* MemoryWriteAddress */
			0,                   /* IOReadPort */
			0,                   /* IOWritePort */
			superpac_interrupt_2,/* interrupt routine */
			1                    /* interrupts per frame */
		}
	},
	60,                        /* frames per second */
	superpac_init_machine,     /* init machine routine */

	/* video hardware */
	28*8, 36*8,                /* screen_width, screen_height */
	 { 0*8, 28*8-1, 0*8, 36*8-1 },/* struct rectangle visible_area */
	gfxdecodeinfo,             /* GfxDecodeInfo * */
	16,                        /* total colors */
	4*(32+37),                 /* color table length */
	0,                         /* convert color prom routine */

	0,                         /* vh_init routine */
	generic_vh_start,          /* vh_start routine */
	generic_vh_stop,           /* vh_stop routine */
	superpac_vh_screenrefresh, /* vh_update routine */

	/* sound hardware */
	samples,                   /* pointer to samples */
	0,                         /* sh_init routine */
	0,                         /* sh_start routine */
	0,                         /* sh_stop routine */
	mappy_sh_update            /* sh_update routine */
};


ROM_START( superpac_rom )
	ROM_REGION(0x10000)	/* 64k for code */
	ROM_LOAD( "SPC-2.1C", 0xc000, 0x2000 )
	ROM_LOAD( "SPC-1.1B", 0xe000, 0x2000 )

	ROM_REGION(0x3000)	/* temporary space for graphics (disposed after conversion) */
	ROM_LOAD( "SPV-1.3C", 0x0000, 0x1000 )
	ROM_LOAD( "SPV-2.3F", 0x1000, 0x2000 )

	ROM_REGION(0x10000)	/* 64k for the second CPU */
	ROM_LOAD( "SPC-3.1K", 0xf000, 0x1000 )
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[0x113c],"N@N",3) == 0 &&        /* check for high score initials */
       RAM[0x1087] == 0 && RAM[0x1089] == 0 && RAM[0x1088] != 0 && /* check for main high score value */
   	 memcmp(&RAM[0x3ee],"000",3) == 0)           /* see if main high score was written to screen */
   {
      if ((f = fopen(name,"rb")) != 0)
      {
         fread(&RAM[0x1138],1,40,f);
         fclose(f);
         
         /* also copy over the high score */
         RAM[0x1087] = RAM[0x1138];
         RAM[0x1088] = RAM[0x1139];
         RAM[0x1089] = RAM[0x113a];
      }

      /* this is a little gross, but necessary to get the high score on-screen */
      if (!writing) writing = (RAM[0x1087] >> 4);
      if ( writing) videoram_w (0x3f4, (RAM[0x1087] >> 4) + '0');
      if (!writing) writing = (RAM[0x1087] & 0x0f);
      if ( writing) videoram_w (0x3f3, (RAM[0x1087] & 0x0f) + '0');
      if (!writing) writing = (RAM[0x1088] >> 4);
      if ( writing) videoram_w (0x3f2, (RAM[0x1088] >> 4) + '0');
      if (!writing) writing = (RAM[0x1088] & 0x0f);
      if ( writing) videoram_w (0x3f1, (RAM[0x1088] & 0x0f) + '0');
      if (!writing) writing = (RAM[0x1089] >> 4);
      if ( writing) videoram_w (0x3f0, (RAM[0x1089] >> 4) + '0');
      if (!writing) writing = (RAM[0x1089] & 0x0f);
      if ( writing) videoram_w (0x3ef, (RAM[0x1089] & 0x0f) + '0');
      videoram_w (0x3ee, 0 + '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[0x1138],1,40,f);
      fclose(f);
   }
}


struct GameDriver superpac_driver =
{
	"Super Pac-Man",
	"superpac",
	"KEVIN BRISLEY\nAARON GILES",
	&machine_driver,          /* MachineDriver * */

	superpac_rom,             /* RomModule * */
	0, 0,                     /* ROM decrypt routines */
	0,                        /* samplenames */

	superpac_input_ports,     /* InputPort  */
        trak_ports,               /* Trakball   */
	superpac_dsw,             /* DSW        */
        keys,                     /* KEY def    */

	0,                        /* color prom */
	superpac_palette,         /* palette */
	superpac_color_table,     /* color table */

	8*11, 8*20,               /* paused_x, paused_y, paused_color for PAUSED */

	hiload, hisave            /* hi score save/load */
};

