`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    19:24:49 08/30/2009 
// Design Name: 
// Module Name:    ColouringTheWorld 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module ColouringTheWorld(
    input CLK,
    input [7:0] COLOUR_IN,
    output [7:0] COLOUR_OUT,
    output HS,
    output HV,
	 output [7:0] FrameCountOut
    );
// Time in Vertical Lines
parameter VertTimeToPulseWidthEnd  = 10'd2;
parameter VertTimeToBackPorchEnd   = 10'd39;
parameter VertTimeToDisplayTimeEnd = 10'd519;
parameter VertTimeToFrontPorchEnd  = 10'd521;

// Time in Horizontal Lines
parameter HorzTimeToPulseWidthEnd  = 10'd96;
parameter HorzTimeToBackPorchEnd   = 10'd152;
parameter HorzTimeToDisplayTimeEnd = 10'd792;
parameter HorzTimeToFrontPorchEnd  = 10'd800;

wire TrigHOut, TrigVOut, TrigFOut, TrigDiv;
wire [9:0] HorzCount;
wire [9:0] VertCount;
//reg [9:0] HorzCountD;
//reg [8:0] VertCountD;
reg [7:0] Colour;
reg [15:0] ShiftColour;
wire [7:0] FrameCount;

always@(HorzCount or VertCount or  COLOUR_IN or FrameCount) begin

	if( ((HorzTimeToBackPorchEnd+100 >= HorzCount) && ( HorzCount>= HorzTimeToBackPorchEnd )) &&
		 ((VertTimeToBackPorchEnd+100 >= VertCount) && ( VertCount >= VertTimeToBackPorchEnd ))
		)
		Colour <= COLOUR_IN + FrameCount[7:0] + HorzCount[7:0] + VertCount[7:0]-100;
	else begin	
	if( ((HorzTimeToBackPorchEnd+100 >= HorzCount) && ( HorzCount>= HorzTimeToBackPorchEnd )) &&
		 ((VertTimeToDisplayTimeEnd >= VertCount) && ( VertCount >= VertTimeToDisplayTimeEnd-100 ))
		) 
		Colour <= COLOUR_IN + FrameCount[7:0] + HorzCount[7:0] + VertCount[7:0]-400;
	else begin
	if( ((HorzTimeToDisplayTimeEnd >= HorzCount) && ( HorzCount >= HorzTimeToDisplayTimeEnd-100)) &&
		 ((VertTimeToBackPorchEnd+100 >= VertCount) && ( VertCount >= VertTimeToBackPorchEnd )) 
		)
		Colour <= COLOUR_IN + FrameCount[7:0] + HorzCount[7:0] + VertCount[7:0]-500;
	else begin
	if( ((HorzTimeToDisplayTimeEnd >= HorzCount) && ( HorzCount >= HorzTimeToDisplayTimeEnd-100)) &&
   	 ((VertTimeToDisplayTimeEnd >= VertCount) && ( VertCount >= VertTimeToDisplayTimeEnd-100) )
	  )		 
		Colour <= COLOUR_IN + FrameCount[7:0] + HorzCount[7:0] + VertCount[7:0]-900;
	else
		Colour <= 8'b00000000;
	end
	end
	end
	
end


//

GenericCounter  #(.COUNTER_WIDTH(1), .COUNTER_MAX(1))
FreqDivider
(
	.CLK(CLK),
	.RESET(1'b0),
	.ENABLE_IN(1'b1),
	.TRIG_OUT(TrigDiv)
);


GenericCounter  #(.COUNTER_WIDTH(10), .COUNTER_MAX(HorzTimeToFrontPorchEnd))
HorzAddrCounter
(
	.CLK(CLK),
	.RESET(1'b0),
	.ENABLE_IN(TrigDiv),
	.TRIG_OUT(TrigHOut),
	.COUNT(HorzCount)
);

GenericCounter  #(.COUNTER_WIDTH(10), .COUNTER_MAX(513))
VertAddrCounter
(
	.CLK(CLK),
	.RESET(1'b0),
	.ENABLE_IN(TrigHOut),
	.TRIG_OUT(TrigVOut),
	.COUNT(VertCount)
);

GenericCounter  #(.COUNTER_WIDTH(8), .COUNTER_MAX(127))
FrameCounter
(
	.CLK(CLK),
	.RESET(1'b0),
	.ENABLE_IN(TrigVOut),
	.TRIG_OUT(TrigFOut),
	.COUNT(FrameCount)
);

//always@(posedge CLK) begin
//	if(TrigFOut) ShiftColour <= ShiftColour + 1;
//end


//always@(posedge CLK) begin
//	HorzCountD <= HorzCount;
//   VertCountD <= VertCount;
//end


VGAInterface VGAInst(
    .CLK(CLK),
    .COLOUR_IN(Colour),
    .COLOUR_OUT(COLOUR_OUT),
    .ADDRH(HorzCount),
    .ADDRV(VertCount),
    .HS(HS),
    .HV(HV)
);


assign FrameCountOut = FrameCount;

endmodule
