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

  vidhrdw.c

  Functions to emulate the video hardware of the machine.

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

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



#define MAX_STARS 250
#define STARS_COLOR_BASE 32

unsigned char *galaga_starcontrol;
static unsigned int stars_scroll;
static int flipscreen;

struct star
{
	int x,y,col,set;
};
static struct star stars[MAX_STARS];
static int total_stars;



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

  Convert the color PROMs into a more useable format.

  Galaga has one 32x8 palette PROM and two 256x4 color lookup table PROMs
  (one for characters, one for sprites). Only the first 128 bytes of the
  lookup tables seem to be used.
  The palette PROM is connected to the RGB output this way:

  bit 7 -- 220 ohm resistor  -- BLUE
        -- 470 ohm resistor  -- BLUE
        -- 220 ohm resistor  -- GREEN
        -- 470 ohm resistor  -- GREEN
        -- 1  kohm resistor  -- GREEN
        -- 220 ohm resistor  -- RED
        -- 470 ohm resistor  -- RED
  bit 0 -- 1  kohm resistor  -- RED

***************************************************************************/
void galaga_vh_convert_color_prom(unsigned char *palette, unsigned char *colortable,const unsigned char *color_prom)
{
	int i;


	for (i = 0;i < 32;i++)
	{
		int bit0,bit1,bit2;


		bit0 = (color_prom[31-i] >> 0) & 0x01;
		bit1 = (color_prom[31-i] >> 1) & 0x01;
		bit2 = (color_prom[31-i] >> 2) & 0x01;
		palette[3*i] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		bit0 = (color_prom[31-i] >> 3) & 0x01;
		bit1 = (color_prom[31-i] >> 4) & 0x01;
		bit2 = (color_prom[31-i] >> 5) & 0x01;
		palette[3*i + 1] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		bit0 = 0;
		bit1 = (color_prom[31-i] >> 6) & 0x01;
		bit2 = (color_prom[31-i] >> 7) & 0x01;
		palette[3*i + 2] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
	}

	/* characters */
	for (i = 0;i < 32*4;i++)
		colortable[i] = 15 - (color_prom[i + 32] & 0x0f);
	/* sprites */
	for (i = 32*4;i < 64*4;i++)
	{
		if (i % 4 == 0) colortable[i] = 0;	/* preserve transparency */
		else colortable[i] = (15 - (color_prom[i + 32] & 0x0f)) + 0x10;
	}

	/* now the stars */
	for (i = 32;i < 32 + 64;i++)
	{
		int bits;
		int map[4] = { 0x00, 0x88, 0xcc, 0xff };

		bits = ((i-32) >> 0) & 0x03;
		palette[3*i] = map[bits];
		bits = ((i-32) >> 2) & 0x03;
		palette[3*i + 1] = map[bits];
		bits = ((i-32) >> 4) & 0x03;
		palette[3*i + 2] = map[bits];
	}
}



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

  Start the video hardware emulation.

***************************************************************************/
int galaga_vh_start(void)
{
	int generator;
	int x,y;
	int set = 0;


	if (generic_vh_start() != 0)
		return 1;


	/* precalculate the star background */
	/* this comes from the Galaxian hardware, Galaga is probably different */
	total_stars = 0;
	generator = 0;

	for (x = 255;x >= 0;x--)
	{
		for (y = 511;y >= 0;y--)
		{
			int bit1,bit2;


			generator <<= 1;
			bit1 = (~generator >> 17) & 1;
			bit2 = (generator >> 5) & 1;

			if (bit1 ^ bit2) generator |= 1;

			if (x >= Machine->drv->visible_area.min_x &&
					x <= Machine->drv->visible_area.max_x &&
					((~generator >> 16) & 1) &&
					(generator & 0xff) == 0xff)
			{
				int color;

				color = (~(generator >> 8)) & 0x3f;
				if (color && total_stars < MAX_STARS)
				{
					stars[total_stars].x = x;
					stars[total_stars].y = y;
					stars[total_stars].col = Machine->pens[color + STARS_COLOR_BASE];
					stars[total_stars].set = set;
					if (++set > 3)
						set = 0;

					total_stars++;
				}
			}
		}
	}

	return 0;
}



void galaga_flipscreen_w(int offset,int data)
{
	if (flipscreen != (data & 1))
	{
		flipscreen = data & 1;
		memset(dirtybuffer,1,videoram_size);
	}
}



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

  Draw the game screen in the given osd_bitmap.
  Do NOT call osd_update_display() from this function, it will be called by
  the main emulation engine.

***************************************************************************/
void galaga_vh_screenrefresh(struct osd_bitmap *bitmap)
{
	int offs;


	/* for every character in the Video RAM, check if it has been modified */
	/* since last time and update it accordingly. */
	for (offs = videoram_size - 1;offs >= 0;offs--)
	{
		if (dirtybuffer[offs])
		{
			int sx,sy,mx,my;


			dirtybuffer[offs] = 0;

		/* Even if Galaga's screen is 28x36, the memory layout is 32x32. We therefore */
		/* have to convert the memory coordinates into screen coordinates. */
		/* Note that 32*32 = 1024, while 28*36 = 1008: therefore 16 bytes of Video RAM */
		/* don't map to a screen position. We don't check that here, however: range */
		/* checking is performed by drawgfx(). */

			mx = offs / 32;
			my = offs % 32;

			if (mx <= 1)
			{
				sx = 29 - my;
				sy = mx + 34;
			}
			else if (mx >= 30)
			{
				sx = 29 - my;
				sy = mx - 30;
			}
			else
			{
				sx = 29 - mx;
				sy = my + 2;
			}

			if (flipscreen)
			{
				sx = 27 - sx;
				sy = 35 - sy;
			}

			drawgfx(tmpbitmap,Machine->gfx[0],
					videoram[offs],
					colorram[offs],
					flipscreen,flipscreen,
					8*sx,8*sy,
					&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
		}
	}


	/* copy the character mapped graphics */
	copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);


	/* Draw the sprites. */
	for (offs = 0;offs < spriteram_size;offs += 2)
	{
		if ((spriteram_3[offs + 1] & 2) == 0)
		{
			int code,color,flipx,flipy,sx,sy;


			code = spriteram[offs];
			color = spriteram[offs + 1];
			flipx = spriteram_3[offs] & 2;
			flipy = spriteram_3[offs] & 1;
			sx = spriteram_2[offs] - 16;
			sy = spriteram_2[offs + 1] - 40 + 0x100*(spriteram_3[offs + 1] & 1);

			if (flipscreen)
			{
				flipx = !flipx;
				flipy = !flipy;
			}

			if (spriteram_3[offs] & 8)	/* double width */
			{
				drawgfx(bitmap,Machine->gfx[1],
						code+2,color,flipx,flipy,sx,sy,
						&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);
				drawgfx(bitmap,Machine->gfx[1],
						code,color,flipx,flipy,sx+16,sy,
						&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);

				if (spriteram_3[offs] & 4)	/* double width, double height */
				{
					drawgfx(bitmap,Machine->gfx[1],
							code+3,color,flipx,flipy,sx,sy+16,
							&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);
					drawgfx(bitmap,Machine->gfx[1],
							code+1,color,flipx,flipy,sx+16,sy+16,
							&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);
				}
			}
			else if (spriteram_3[offs] & 4)	/* double height */
			{
				drawgfx(bitmap,Machine->gfx[1],
						code,color,flipx,flipy,sx,sy,
						&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);
				drawgfx(bitmap,Machine->gfx[1],
						code+1,color,flipx,flipy,sx,sy+16,
						&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);
			}
			else	/* normal */
				drawgfx(bitmap,Machine->gfx[1],
						code,color,flipx,flipy,sx,sy,
						&Machine->drv->visible_area,TRANSPARENCY_THROUGH,0);
		}
	}


	/* draw the stars */
	if (galaga_starcontrol[5] & 1)
	{
		int bpen;


		bpen = Machine->pens[0];
		for (offs = 0;offs < total_stars;offs++)
		{
			int x,y;
			int set;
			int starset[4][2] = {{0,3},{0,1},{2,3},{2,1}};

			x = stars[offs].x;
			y = (stars[offs].y + stars_scroll/2) % 256 + 16;

			if (Machine->orientation & ORIENTATION_SWAP_XY)
			{
				int temp;


				temp = x;
				x = y;
				y = temp;
			}
			if (Machine->orientation & ORIENTATION_FLIP_X)
				x = bitmap->width - 1 - x;
			if (Machine->orientation & ORIENTATION_FLIP_Y)
				y = bitmap->height - 1 - y;

			set = ((galaga_starcontrol[4] << 1) | galaga_starcontrol[3]) & 3;
			if ((bitmap->line[y][x] == bpen) &&
					((stars[offs].set == starset[set][0]) ||
					 (stars[offs].set == starset[set][1])))
				bitmap->line[y][x] = stars[offs].col;
		}
	}
}



void galaga_vh_interrupt(void)
{
	/* this function is called by galaga_interrupt_1() */
	int s0,s1,s2;
	int speeds[8] = { 2, 3, 4, 0, -4, -3, -2, 0 };


	s0 = galaga_starcontrol[0] & 1;
	s1 = galaga_starcontrol[1] & 1;
	s2 = galaga_starcontrol[2] & 1;

	stars_scroll -= speeds[s0 + s1*2 + s2*4];
}
