//------------------------------------------------------------------------------
//
//  lynx_rgb_tb.v : Spartan3A display test testbench
//
//  LICENSE : "as-is"
//  copyright (C) 2009, TakeshiNagashima caramelgate@gmail.com
//------------------------------------------------------------------------------
//  2009/aug/17 release 0.0  
//
//------------------------------------------------------------------------------

`include "sim_xilinx_inc.v"
`include "lynx_dvi.v"
`include "xil_dvi.v"
`include "dvi_data_enc.v"
`include "xil_dcm_x5.v"
`include "dispgen.v"

`include "lynx_rgb.v"
`include "xil_dvi_rx.v"
`include "dvi_data_dec.v"
`include "xil_dcm_x10.v"

`timescale 1ns / 100ps

module tb(
);

	wire	CLK25;
	wire	CLK50;
	wire	RST_N;

	// ---- 25MHz ----

	reg		CLK25_r;

	initial
	begin
		forever
		begin
			CLK25_r <= 1'b0; #20;
			CLK25_r <= 1'b1; #20;
		end
	end

	assign CLK25=CLK25_r;

	// ---- 50MHz ----

	reg		CLK50_r;

	initial
	begin
		forever
		begin
			CLK50_r <= 1'b0; #10;
			CLK50_r <= 1'b1; #10;
		end
	end

	assign CLK50=CLK50_r;

	// ---- reset ----

	reg		[8:0] RST_N_r;

	initial
	begin
		RST_N_r[0] <= 'b1; #2;
		RST_N_r[0] <= 'b0; #200;
		RST_N_r[0] <= 'b1; #1;
	end

	always @(posedge CLK50 or negedge RST_N_r[0])
	begin
		if (RST_N_r[0]==1'b0)
			begin
				RST_N_r[8:1] <= 8'h00;
			end
		else
			begin
				RST_N_r[8] <= (RST_N_r[7:1]==7'h0f) ? 1'b1 : RST_N_r[8];
				RST_N_r[7:1] <= RST_N_r[7:1]+7'h01;
			end
	end

	assign RST_N=RST_N_r[8];

	wire	[7:0] TX_RED;
	wire	[7:0] TX_GRN;
	wire	[7:0] TX_BLU;
	wire	TX_HS;
	wire	TX_VS;
//	wire	TX_C0;
//	wire	TX_C1;
//	wire	TX_C2;
//	wire	TX_C3;
	wire	TX_DE;
	wire	TX_CLK;

	wire	DVI_TX0_N;
	wire	DVI_TX0_P;
	wire	DVI_TX1_N;
	wire	DVI_TX1_P;
	wire	DVI_TX2_N;
	wire	DVI_TX2_P;
	wire	DVI_TXC_N;
	wire	DVI_TXC_P;

	wire	[7:0] RX_RED;
	wire	[7:0] RX_GRN;
	wire	[7:0] RX_BLU;
	wire	RX_HS;
	wire	RX_VS;
//	wire	RX_C0;
//	wire	RX_C1;
//	wire	RX_C2;
//	wire	RX_C3;
	wire	RX_DE;
	wire	RX_CLK;

	assign TX_CLK=CLK25;

dispgen dispgen(
	.D_RED(TX_RED[7:0]),			// out   [CRT] [7:0] red
	.D_GRN(TX_GRN[7:0]),			// out   [CRT] [7:0] green
	.D_BLU(TX_BLU[7:0]),			// out   [CRT] [7:0] blue
	.D_HSYNC_N(TX_HS),				// out   [CRT] #hsync
	.D_VSYNC_N(TX_VS),				// out   [CRT] #vsync
	.D_BLANK_N(TX_DE),				// out   [CRT] #blank

	.D_MODE(4'b0),					// in    [] [3:0] sim/#normal

	.D_RST_N(RST_N),				// in    [] : #reset
	.D_CLK(TX_CLK)					// in    [CRT] dot clock
);

	// ---- dvi encode ----

lynx_dvi lynx_dvi(
	.DVI_TX0_N(DVI_TX0_N),			// out   [DVI] ch0-n tmds33(CML)/lvds
	.DVI_TX0_P(DVI_TX0_P),			// out   [DVI] ch0-p tmds33(CML)/lvds
	.DVI_TX1_N(DVI_TX1_N),			// out   [DVI] ch1-n tmds33(CML)/lvds
	.DVI_TX1_P(DVI_TX1_P),			// out   [DVI] ch1-p tmds33(CML)/lvds
	.DVI_TX2_N(DVI_TX2_N),			// out   [DVI] ch2-n tmds33(CML)/lvds
	.DVI_TX2_P(DVI_TX2_P),			// out   [DVI] ch2-p tmds33(CML)/lvds
	.DVI_TXC_N(DVI_TXC_N),			// out   [DVI] clk-n tmds33(CML)/lvds
	.DVI_TXC_P(DVI_TXC_P),			// out   [DVI] clk-p tmds33(CML)/lvds
	.DVI_TX_SDA(),					// inout [DVI] dcc sda
	.DVI_TX_SCL(),					// inout [DVI] dcc scl
	.DVI_TX_CEC(),					// inout [DVI] cec
	.DVI_TX_HPD(1'b1),				// in    [DVI] detect

	.DVI_RX0_N(),					// out   [DVI] ch0-n tmds33(CML)/lvds
	.DVI_RX0_P(),					// out   [DVI] ch0-p tmds33(CML)/lvds
	.DVI_RX1_N(),					// out   [DVI] ch1-n tmds33(CML)/lvds
	.DVI_RX1_P(),					// out   [DVI] ch1-p tmds33(CML)/lvds
	.DVI_RX2_N(),					// out   [DVI] ch2-n tmds33(CML)/lvds
	.DVI_RX2_P(),					// out   [DVI] ch2-p tmds33(CML)/lvds
	.DVI_RXC_N(),					// out   [DVI] clk-n tmds33(CML)/lvds
	.DVI_RXC_P(),					// out   [DVI] clk-p tmds33(CML)/lvds
	.DVI_RX_SDA(),					// inout [DVI] dcc sda
	.DVI_RX_SCL(),					// inout [DVI] dcc scl
	.DVI_RX_CEC(),					// inout [DVI] cec
	.DVI_RX_HPD(1'b1),				// in    [DVI] detect

	.LCD_TX0_N(),					// out   [LCD] ch0-n lvds
	.LCD_TX0_P(),					// out   [LCD] ch0-p lvds
	.LCD_TX1_N(),					// out   [LCD] ch1-n lvds
	.LCD_TX1_P(),					// out   [LCD] ch1-p lvds
	.LCD_TX2_N(),					// out   [LCD] ch2-n lvds
	.LCD_TX2_P(),					// out   [LCD] ch2-p lvds
	.LCD_TX3_N(),					// out   [LCD] ch3-n lvds
	.LCD_TX3_P(),					// out   [LCD] ch3-p lvds
	.LCD_TXC_N(),					// out   [LCD] clk-n lvds
	.LCD_TXC_P(),					// out   [LCD] clk-p lvds

//	.TX_OE_N(1'b0),					// in    [RGB] tx #oe

	.TX_RED(TX_RED[7:0]),			// in    [RGB] red [7:0]
	.TX_GRN(TX_GRN[7:0]),			// in    [RGB] grn [7:0]
	.TX_BLU(TX_BLU[7:0]),			// in    [RGB] blu [7:0]
	.TX_HS(TX_HS),					// in    [RGB] #hsync
	.TX_VS(TX_VS),					// in    [RGB] #vsync
//	.TX_C0(TX_C0),					// in    [RGB] c0
//	.TX_C1(TX_C1),					// in    [RGB] c1
//	.TX_C2(TX_C2),					// in    [RGB] c2
//	.TX_C3(TX_C3),					// in    [RGB] c3
	.TX_DE(TX_DE),					// in    [RGB] de
	.TX_CLK(TX_CLK),				// in    [RGB] clock

	.sys_clk(CLK50),				// in    [SYS] clock
	.sys_rst_n(RST_N)				// in    [SYS] #reset
);

	// ---- dvi decode ----

lynx_rgb lynx_rgb(
	.DVI_TX0_N(),					// out   [DVI] ch0-n tmds33(CML)/lvds
	.DVI_TX0_P(),					// out   [DVI] ch0-p tmds33(CML)/lvds
	.DVI_TX1_N(),					// out   [DVI] ch1-n tmds33(CML)/lvds
	.DVI_TX1_P(),					// out   [DVI] ch1-p tmds33(CML)/lvds
	.DVI_TX2_N(),					// out   [DVI] ch2-n tmds33(CML)/lvds
	.DVI_TX2_P(),					// out   [DVI] ch2-p tmds33(CML)/lvds
	.DVI_TXC_N(),					// out   [DVI] clk-n tmds33(CML)/lvds
	.DVI_TXC_P(),					// out   [DVI] clk-p tmds33(CML)/lvds
	.DVI_TX_SDA(),					// inout [DVI] dcc sda
	.DVI_TX_SCL(),					// inout [DVI] dcc scl
	.DVI_TX_CEC(),					// inout [DVI] cec
	.DVI_TX_HPD(1'b0),				// in    [DVI] detect

	.DVI_RX0_N(DVI_TX0_N),			// in    [DVI] ch0-n tmds33(CML)/lvds
	.DVI_RX0_P(DVI_TX0_P),			// in    [DVI] ch0-p tmds33(CML)/lvds
	.DVI_RX1_N(DVI_TX1_N),			// in    [DVI] ch1-n tmds33(CML)/lvds
	.DVI_RX1_P(DVI_TX1_P),			// in    [DVI] ch1-p tmds33(CML)/lvds
	.DVI_RX2_N(DVI_TX2_N),			// in    [DVI] ch2-n tmds33(CML)/lvds
	.DVI_RX2_P(DVI_TX2_P),			// in    [DVI] ch2-p tmds33(CML)/lvds
	.DVI_RXC_N(DVI_TXC_N),			// in    [DVI] clk-n tmds33(CML)/lvds
	.DVI_RXC_P(DVI_TXC_P),			// in    [DVI] clk-p tmds33(CML)/lvds
	.DVI_RX_SDA(),					// inout [DVI] dcc sda
	.DVI_RX_SCL(),					// inout [DVI] dcc scl
	.DVI_RX_CEC(),					// inout [DVI] cec
	.DVI_RX_HPD(),					// out   [DVI] detect

	.TX_RED(8'b0),					// in    [RGB] red [7:0]
	.TX_GRN(8'b0),					// in    [RGB] grn [7:0]
	.TX_BLU(8'b0),					// in    [RGB] blu [7:0]
	.TX_HS(1'b0),					// in    [RGB] #hsync
	.TX_VS(1'b0),					// in    [RGB] #vsync
//	.TX_C0(1'b0),					// in    [RGB] c0
//	.TX_C1(1'b0),					// in    [RGB] c1
//	.TX_C2(1'b0),					// in    [RGB] c2
//	.TX_C3(1'b0),					// in    [RGB] c3
	.TX_DE(1'b0),					// in    [RGB] de
	.TX_CLK(1'b0),					// in    [RGB] clock

	.RX_RED(RX_RED[7:0]),			// out   [RGB] red [7:0]
	.RX_GRN(RX_GRN[7:0]),			// out   [RGB] grn [7:0]
	.RX_BLU(RX_BLU[7:0]),			// out   [RGB] blu [7:0]
	.RX_HS(RX_HS),					// out   [RGB] #hsync
	.RX_VS(RX_VS),					// out   [RGB] #vsync
//	.RX_C0(),						// out   [RGB] c0
//	.RX_C1(),						// out   [RGB] c1
//	.RX_C2(),						// out   [RGB] c2
//	.RX_C3(),						// out   [RGB] c3
	.RX_DE(RX_DE),					// out   [RGB] de
	.RX_CLK(RX_CLK),				// out   [RGB] clock

	.sys_clk(CLK50),				// in    [SYS] clock
	.sys_rst_n(RST_N)				// in    [SYS] #reset
);

/*

	// ---- video dac : de1 ----

	wire	[35:0] GPIO_1;

	assign {GPIO_1[15],GPIO_1[13],GPIO_1[11],GPIO_1[9]}=RX_BLU[7:4];
	assign {GPIO_1[23],GPIO_1[21],GPIO_1[19],GPIO_1[17]}=RX_GRN[7:4];
	assign {GPIO_1[31],GPIO_1[29],GPIO_1[27],GPIO_1[25]}=RX_RED[7:4];
	assign GPIO_1[3]=RX_HS;
	assign GPIO_1[4]=RX_VS;

	assign GPIO_1[2:0]=3'b0;
	assign GPIO_1[8:5]=4'b0;
	assign GPIO_1[10]=1'b0;
	assign GPIO_1[10]=1'b0;
	assign GPIO_1[12]=1'b0;
	assign GPIO_1[13]=1'b0;
	assign GPIO_1[16]=1'b0;
	assign GPIO_1[18]=1'b0;
	assign GPIO_1[20]=1'b0;
	assign GPIO_1[22]=1'b0;
	assign GPIO_1[24]=1'b0;
	assign GPIO_1[26]=1'b0;
	assign GPIO_1[28]=1'b0;
	assign GPIO_1[30]=1'b0;
	assign GPIO_1[35:32]=4'b0;

de1_rgb de1_rgb(
	.GPIO_0(),	// Altera SC conv
	.GPIO_1(GPIO_1[35:0]),

	// global signals
	.CLK50(1'b0),
	.CLK27(1'b0),
	.CLK27a(1'b0),
	.CLK24(1'b0),
	.CLK24a(1'b0),
	.CLKEXT(1'b0),

	// the_ps2_0
	.PS2_CLK(),
	.PS2_DATA(),

	// the_vga_controller_0
	.VGA_B(VGA_B[3:0]),
	.VGA_G(VGA_G[3:0]),
	.VGA_R(VGA_R[3:0]),
	.VGA_HS(VGA_HS),
	.VGA_VS(VGA_VS),

	// the_Audio_0
	.I2C_SDAT(),
	.I2C_SCLK(),
	.AUD_ADCLRCK(),
	.AUD_ADCDAT(),
	.AUD_DACLRCK(),
	.AUD_DACDAT(),
	.AUD_BCLK(),
	.AUD_XCK(),

	// the_SEG7_Display
	.HEX0(),
	.HEX1(),
	.HEX2(),
	.HEX3(),

	// the_button_pio
	.KEY0(),
	.KEY1(),
	.KEY2(),
	.KEY3(),

	// the_led_green
	.LEDG(),

	// the_led_red
	.LEDR(),

	// the_sdram_0
	.DRAM_ADDR(),
	.DRAM_BA_0(),
	.DRAM_BA_1(),
	.DRAM_CAS_N(),
	.DRAM_CKE(),
	.DRAM_CLK(),
	.DRAM_CS_N(),
	.DRAM_DATA(),
	.DRAM_LDQM(),
	.DRAM_UDQM(),
	.DRAM_RAS_N(),
	.DRAM_WE_N(),

	// the_spi_0
	.SD_DAT(),
	.SD_CMD(),
	.SD_CLK(),
	.SD_DAT3(),

	// the_sram_0
	.SRAM_ADDR(),
	.SRAM_CE_N(),
	.SRAM_DATA(),
	.SRAM_LB_N(),
	.SRAM_OE_N(),
	.SRAM_UB_N(),
	.SRAM_WE_N(),

	// the_switch_pio
	.SW(),

	// the_tri_state_bridge_0_avalon_slave
	.FL_CE_N(),
	.FL_ADDR(),
	.FL_DATA(),
	.FL_OE_N(),
	.FL_WE_N(),
	.FL_RST_N(),

	// the_uart_0
	.UART_RXD(),
	.UART_TXD()
);

*/

endmodule
