// ###########################################################################
// #
// # OSCAVideo - Implementation of OSCA Video Module
// # Copyright (C) 2009 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#ifndef OSCAVideoH
#define OSCAVideoH

#include "z80.h"
#include "GR32.hpp"

///////////////////////////////////////////////////////////////////////
//
//		Definition of Video Module Types and Constants
//
///////////////////////////////////////////////////////////////////////

typedef enum
{
	VIDEO_PAL_P,		// PAL, Non-Interlaced
	VIDEO_PAL_I,		// PAL, Interlaced
	VIDEO_NTSC_P,		// NTSC, Progressive
	VIDEO_NTSC_I,		// NTSC, Interlaced
	VIDEO_VGA			// VGA
} VideoSystem;

typedef enum
{
	BITMAP_PLANAR,		// Planar Bitmap Mode
	BITMAP_CHUNKY,		// Chunky Bitmap Mode
	TILE_MAP				// Tilemap Mode (Legacy or Extended)
} DisplayMode;

///////////////////////////////////////////////////////////////////////
//
//		Definition of Video Module Class
//
///////////////////////////////////////////////////////////////////////

class TOSCAVideo
{
	private:
		VideoSystem				CurrentVidSystem;				// Current Video Type (PAL/NTSC/VGA)
		DisplayMode				CurrentVidMode;				// Mode (Planar/Chunky/Tilemap)
		byte  					PaletteRAM[512];				// 256 Palette Entries (12-bit)
		byte						VideoRAM[(512 * 1024)];		// 512K Video RAM
		byte 						SpriteRAM[(128 * 1024)];	// 128K Sprite RAM
		int						fScanPrescaler;				// Prescaler for Scanline Interval
		int						fScanLine;						// Current Scanline
		bool						fRasterIRQ;						// Raster IRQ Flag
		bool						fWindowXYSwitch;				// XY Switch in rasthi
		word						fRasterIRQTarget;				// Raster IRQ Target
		int						fWindowTop;						// Window Top Position
		int						fWindowBottom;					// Window Bottom Position
		int						fWindowLeft;					// Window Left Position
		int						fWindowRight;					// Window Right Position
		byte						fVideoPage;						// Current Video Page
		byte						fSpritePage;					// Current Sprite Page
		TPalette32				fVideoPalette;					// Screen Palettes
		TBitmap32				*outputImage;					// Final target to be rendered to
		byte						fPlaneCount;					// Number of planar bitmaps
		bool						fPlaneSet;						// Bitplane Set (A = false, B = true)
		unsigned int			fPlaneBaseAddr[16];			// Bitplane Address Bases
		bool						fBGVideoInhibit;				// Inhibit Background Video
		bool						fWideBorder;					// Wide left border?
		bool						fBitmapFloodMode;				// Bitmap Flood Mode? (chunky)

		byte						fXHWS;							// vreg_xhws
		byte						fSpriteControl;				// vreg_sprctrl
		byte						fLineColour;					// linedraw_colour

		// Blitter
		byte						fBlitDestMSB;					// blit_dst_msb
		byte 						fBlitSrcMSB;					// blit_src_msb
		byte						fBlitWidth;              	// blit_width
		byte 						fBlitHeight;					// blit_height
		byte						fBlitMisc;						// blit_misc
		short						fBlitSrcModulo;				// blit_src_mod (including MSB from blit_misc)
		short						fBlitDestModulo;				// blit_dst_mod (including MSB from blit_misc)
		word						fBlitSrcLoc;					// blit_src_loc
		word						fBlitDestLoc;					// blit_dst_loc

	protected:

	public:
		// Generic Operations
		bool __fastcall		Tick(unsigned int CycleCount);
		void __fastcall		Reset(void);
		bool __fastcall		GetRasterIRQ(void);
		void __fastcall		ClearIRQ(void);

		// Write Operations
		void __fastcall		WritePalette(word Address, byte Value);
		void __fastcall		WriteRegister(word Address, byte Value);
		void __fastcall		WriteDirectVRAM(word Address, byte Value);
		void __fastcall		WriteMappedVRAM(word Address, byte Value);
		void __fastcall		WriteSpriteRAM(word Address, byte Value);
		void __fastcall		WriteBlitter(byte Address, byte Value);

		// Read Operations
		byte __fastcall		ReadMappedVRAM(word Address);
		byte __fastcall		ReadSpriteRAM(word Address);
		byte __fastcall		ReadVREG(void);

		__fastcall 				TOSCAVideo();
		__fastcall 				~TOSCAVideo();
};

//---------------------------------------------------------------------------

#endif

