// MicroVIP_tb.v
// Test bench for MicroVIP
// (c) 2017 David R. Hunter
//
// Revisions
// 	0		08/11/17	DH - initial version
//		1		08/27/17	DH	- add speaker and video outputs
//
`timescale 1ns/1ns
`define SIM

module MicroVIP_tb();

	reg	CLK50;                 	// FPGA clock
	reg   RESET_N;						// FPGA reset (POR)
	reg   RUN;   						// RUN toggle switches
	wire	TXD;							// serial TX/RX
	reg	RXD;
	reg	KEY_PRESS;					// keypad press (EF(3)
	wire 	SPI_MOSI;					// SPI interconnect (wrap around)
	wire	SPI_MISO;
  
  integer j;                    	// counting variable
  parameter CLK_PERIOD = 20;		// 50 MHz clock for simulation
  
  // system clock period = CLK_PERIOD ticks
  initial CLK50 <= 1'b0;
  always #( CLK_PERIOD/2.0)
        CLK50 <= ~CLK50;

//  initial
//	begin
		assign SPI_MISO = !SPI_MOSI;	// read in inverted SPI data out for test
//	end
	
  // main simulation
  initial 
    begin
      $display("MicroVIP Test Bench");
      
      // set initial states of signal inputs
      RESET_N <= 1'b0;  // power on reset
      RUN <= 1'b0;
		RXD <= 1'b1;
		KEY_PRESS <= 1'b1;
 
      for (j = 0; j < 20; j = j + 1)
        begin
            @(posedge CLK50);            
        end
        
      RESET_N <= 1'b1;

		$display("Power on reset complete");
      for (j = 0; j < 100; j = j + 1)
        begin
            @(posedge CLK50);
        end

		KEY_PRESS <= 1'b0;	// indicate C key pressed

		$display("Begin Running");
   	RUN <= 1'b1;
      for (j = 0; j < 500000; j = j + 1)
        begin
            @(posedge CLK50);
        end
  
		
		$display("Simulation End");
		$finish ;
    end
    
	// connect up the UUT
   MicroVIP U1(
		.inclk0_sig (CLK50),
		.led_1_n    (),
		.led_2_n    (),
		.led_3_n    (),
		.ext_por_n  (RESET_N),
		.run_sw     (RUN),
		.q_led      (),
		.uart_txd	(TXD),
		.ef_n       ({1'b1,KEY_PRESS,RXD}),	// ef_n[4:2], ef_n[1] is reserved
		.keypad_out	(),
		.gpio_out	(),
		.gpio_in		(8'h5A),
		.gpio_out_stb(),
		.gpio_out_stb_n(),
		.flash_miso	(SPI_MISO),
		.flash_mosi	(SPI_MOSI),
		.flash_sclk	(),
		.flash_cs_n	(),
		.flash_wp_n	(),
		.pbtn			(1'b1),		// alternate ROM boot disabled
		.speaker		(),
		.sync_n		(),
		.video		()
	);

endmodule
 