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

    Cinematronics vector hardware

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

#include "driver.h"
#include "vector.h"
#include "cpu/ccpu/ccpu.h"
#include "cinemat.h"

#ifdef NEW_RENDER
#include "render.h"
#endif


/*************************************
 *
 *  Constants
 *
 *************************************/

enum
{
	COLOR_BILEVEL,
	COLOR_16LEVEL,
	COLOR_64LEVEL,
	COLOR_RGB,
	COLOR_QB3
};



/*************************************
 *
 *  Local variables
 *
 *************************************/

static int color_mode;
static rgb_t vector_color;
static INT16 lastx, lasty;
static UINT8 last_control;



/*************************************
 *
 *  Vector rendering
 *
 *************************************/

void cinemat_vector_callback(INT16 sx, INT16 sy, INT16 ex, INT16 ey, UINT8 shift)
{
	int intensity = 0xff;

	/* adjust for slop */
	sx = sx - Machine->visible_area[0].min_x;
	ex = ex - Machine->visible_area[0].min_x;
	sy = sy - Machine->visible_area[0].min_y;
	ey = ey - Machine->visible_area[0].min_y;

	/* point intensity is determined by the shift value */
	if (sx == ex && sy == ey)
		intensity = 0x1ff * shift / 8;

	/* move to the starting position if we're not there already */
	if (sx != lastx || sy != lasty)
		vector_add_point(sx << 16, sy << 16, 0, 0);

	/* draw the vector */
	vector_add_point(ex << 16, ey << 16, vector_color, intensity);

	/* remember the last point */
	lastx = ex;
	lasty = ey;
}



/*************************************
 *
 *  Vector color handling
 *
 *************************************/

WRITE8_HANDLER(cinemat_vector_control_w)
{
	int r, g, b, i;

	switch (color_mode)
	{
		case COLOR_BILEVEL:
			/* color is either bright or dim, selected by the value sent to the port */
			vector_color = (data & 1) ? MAKE_RGB(0x80,0x80,0x80) : MAKE_RGB(0xff,0xff,0xff);
			break;

		case COLOR_16LEVEL:
			/* on the rising edge of the data value, latch bits 0-3 of the */
			/* X register as the intensity */
			if (data != last_control && data)
			{
				int xval = cpunum_get_reg(0, CCPU_X) & 0x0f;
				i = (xval + 1) * 255 / 16;
				vector_color = MAKE_RGB(i,i,i);
			}
			break;

		case COLOR_64LEVEL:
			/* on the rising edge of the data value, latch bits 2-7 of the */
			/* X register as the intensity */
			if (data != last_control && data)
			{
				int xval = cpunum_get_reg(0, CCPU_X);
				xval = (~xval >> 2) & 0x3f;
				i = (xval + 1) * 255 / 64;
				vector_color = MAKE_RGB(i,i,i);
			}
			break;

		case COLOR_RGB:
			/* on the rising edge of the data value, latch the X register */
			/* as 4-4-4 BGR values */
			if (data != last_control && data)
			{
				int xval = cpunum_get_reg(0, CCPU_X);
				r = (~xval >> 0) & 0x0f;
				r = r * 255 / 15;
				g = (~xval >> 4) & 0x0f;
				g = g * 255 / 15;
				b = (~xval >> 8) & 0x0f;
				b = b * 255 / 15;
				vector_color = MAKE_RGB(r,g,b);
			}
			break;

		case COLOR_QB3:
			{
				static int lastx, lasty;

				/* on the falling edge of the data value, remember the original X,Y values */
				/* they will be restored on the rising edge; this is to simulate the fact */
				/* that the Rockola color hardware did not overwrite the beam X,Y position */
				/* on an IV instruction if data == 0 here */
				if (data != last_control && !data)
				{
					lastx = cpunum_get_reg(0, CCPU_X);
					lasty = cpunum_get_reg(0, CCPU_Y);
				}

				/* on the rising edge of the data value, latch the Y register */
				/* as 2-3-3 BGR values */
				if (data != last_control && data)
				{
					int yval = cpunum_get_reg(0, CCPU_Y);
					r = (~yval >> 0) & 0x07;
					r = r * 255 / 7;
					g = (~yval >> 3) & 0x07;
					g = g * 255 / 7;
					b = (~yval >> 6) & 0x03;
					b = b * 255 / 3;
					vector_color = MAKE_RGB(r,g,b);

					/* restore the original X,Y values */
					cpunum_set_reg(0, CCPU_X, lastx);
					cpunum_set_reg(0, CCPU_Y, lasty);
				}
			}
			break;
	}

	/* remember the last value */
	last_control = data;
}



/*************************************
 *
 *  Video startup
 *
 *************************************/

VIDEO_START( cinemat_bilevel )
{
	color_mode = COLOR_BILEVEL;
	return video_start_vector();
}


VIDEO_START( cinemat_16level )
{
	color_mode = COLOR_16LEVEL;
	return video_start_vector();
}


VIDEO_START( cinemat_64level )
{
	color_mode = COLOR_64LEVEL;
	return video_start_vector();
}


VIDEO_START( cinemat_color )
{
	color_mode = COLOR_RGB;
	return video_start_vector();
}


VIDEO_START( cinemat_qb3color )
{
	color_mode = COLOR_QB3;
	return video_start_vector();
}



/*************************************
 *
 *  End-of-frame
 *
 *************************************/

VIDEO_EOF( cinemat )
{
	vector_clear_list();
}



/*************************************
 *
 *  Space War update
 *
 *************************************/

VIDEO_UPDATE( spacewar )
{
	int sw_option = readinputportbytag("INPUTS");

	video_update_vector(screen, bitmap, cliprect);

	/* set the state of the artwork */
#ifndef NEW_RENDER
	artwork_show("pressed3", (~sw_option >> 0) & 1);
	artwork_show("pressed8", (~sw_option >> 1) & 1);
	artwork_show("pressed4", (~sw_option >> 2) & 1);
	artwork_show("pressed9", (~sw_option >> 3) & 1);
	artwork_show("pressed1", (~sw_option >> 4) & 1);
	artwork_show("pressed6", (~sw_option >> 5) & 1);
	artwork_show("pressed2", (~sw_option >> 6) & 1);
	artwork_show("pressed7", (~sw_option >> 7) & 1);
	artwork_show("pressed5", (~sw_option >> 10) & 1);
	artwork_show("pressed0", (~sw_option >> 11) & 1);
#else
	render_view_item_set_state("pressed3", (~sw_option >> 0) & 1);
	render_view_item_set_state("pressed8", (~sw_option >> 1) & 1);
	render_view_item_set_state("pressed4", (~sw_option >> 2) & 1);
	render_view_item_set_state("pressed9", (~sw_option >> 3) & 1);
	render_view_item_set_state("pressed1", (~sw_option >> 4) & 1);
	render_view_item_set_state("pressed6", (~sw_option >> 5) & 1);
	render_view_item_set_state("pressed2", (~sw_option >> 6) & 1);
	render_view_item_set_state("pressed7", (~sw_option >> 7) & 1);
	render_view_item_set_state("pressed5", (~sw_option >> 10) & 1);
	render_view_item_set_state("pressed0", (~sw_option >> 11) & 1);
#endif
	return 0;
}

