
 //-----------------------------------------------------
 // Design Name : HEX data decoder
 // File Name   : HEX_coder.v
 // Function    : decodes data using switches for setting value and HEX display to show value
 // Coder       : Fred Aulich
 //-----------------------------------------------------
 module HEX_decoder(
 
 hex0_out, 		// 7 bit binary Output
 hex1_out, 		// 7 bit binary Output
 hex2_out, 		// 7 bit binary Output
 hex3_out, 		// 7 bit binary Output
 hex4_out, 		// 7 bit binary Output
 hex5_out, 		// 7 bit binary Output
 hex6_out, 		// 7 bit binary Output
 hex7_out,		// 7 bit binary Output
 clk_50,			// 50 MHz clock
 RESET,			// switch 0 on DE2 board
 swt			   // switches 17 downto 0 on DE2 board
 );
 
 output [6:0] hex0_out ;
 output [6:0] hex1_out ;
 output [6:0] hex2_out ;
 output [6:0] hex3_out ;
 output [6:0] hex4_out ;
 output [6:0] hex5_out ;
 output [6:0] hex6_out ;
 output [6:0] hex7_out ;
 
 
 input clk_50;	// 50 MHz clock on DE2 board
 input RESET;    // switch 0 DE2 board
 input [6:0]swt; // switches 17 downto 10 on DE2 board
 
 reg [6:0] hex0_out ;
 reg [6:0] hex1_out ;
 reg [6:0] hex2_out ;
 reg [6:0] hex3_out ;
 reg [6:0] hex4_out ;
 reg [6:0] hex5_out ; 
 reg [6:0] hex6_out ;
 reg [6:0] hex7_out ;
 
 
 reg [3:0] I2C_value0; // store low nibble of data
 reg [3:0] I2C_value1; // store high nibble of data
 reg [3:0] I2C_value2; // store low nibble of address
 reg [3:0] I2C_value3; // store high nibble of address
 reg [23:0] I2C_DATA;  // store value to be loaded into I2C [8 command, 8 address, 8 data]
 
 reg [6:0] hex_out;
 reg [3:0] I2C_value;
 
// 
// command never changes so keep it fix

 parameter I2C_value5 = 4'h4; 		//-- command value HEX 4
 parameter I2C_value4 = 4'h0; 		//-- command value HEX 0
 
 
 // setting to create HEX value on display
 
 parameter HEX_0 = 7'b1000000;		// zero
 parameter HEX_1 = 7'b1111001;		// one
 parameter HEX_2 = 7'b0100100;		// two
 parameter HEX_3 = 7'b0110000;		// three
 parameter HEX_4 = 7'b0011001;		// four
 parameter HEX_5 = 7'b0010010;		// five
 parameter HEX_6 = 7'b0000010;		// six
 parameter HEX_7 = 7'b1111000;		// seven
 parameter HEX_8 = 7'b0000000;		// eight
 parameter HEX_9 = 7'b0011000;		// nine
 parameter HEX_10 = 7'b0001000;		// ten
 parameter HEX_11 = 7'b0000011;		// eleven
 parameter HEX_12 = 7'b1000110;		// twelve
 parameter HEX_13 = 7'b0100001;		// thirteen
 parameter HEX_14 = 7'b0000110;		// fourteen
 parameter HEX_15 = 7'b0001110;		// fifteen
 parameter zero   = 7'b1111111;		// all off
 
 
 ///////////////////////////////////////////////////
 ////// switches for values for address and data ///
 ///////////////////////////////////////////////////
 
 always @(posedge clk_50 or posedge RESET)	
 
	begin
	
	 
		I2C_value = 4'h0; // default setting
		hex_out = 7'b0;   // default setting
		

	if (RESET) begin

////////////////////////////////////////////////////////////////////
/// switches 3 down to 0 are used to create hex value         //////
/// switch 14 most significant and switch 11 lest significant //////	
////////////////////////////////////////////////////////////////////
		
		case (swt[3:0])
		
		4'b0000: begin I2C_value = 4'h0; hex_out = HEX_0; end
		4'b0001: begin I2C_value = 4'h1; hex_out = HEX_1; end
		4'b0010: begin I2C_value = 4'h2; hex_out = HEX_2; end
		4'b0011: begin I2C_value = 4'h3; hex_out = HEX_3; end 
		4'b0100: begin I2C_value = 4'h4; hex_out = HEX_4; end
		4'b0101: begin I2C_value = 4'h5; hex_out = HEX_5; end
		4'b0110: begin I2C_value = 4'h6; hex_out = HEX_6; end
		4'b0111: begin I2C_value = 4'h7; hex_out = HEX_7; end
		4'b1000: begin I2C_value = 4'h8; hex_out = HEX_8; end
		4'b1001: begin I2C_value = 4'h9; hex_out = HEX_9; end
		4'b1010: begin I2C_value = 4'ha; hex_out = HEX_10; end
		4'b1011: begin I2C_value = 4'hb; hex_out = HEX_11; end
		4'b1011: begin I2C_value = 4'hc; hex_out = HEX_12; end
		4'b1101: begin I2C_value = 4'hd; hex_out = HEX_13; end
		4'b1110: begin I2C_value = 4'he; hex_out = HEX_14; end
		4'b1111: begin I2C_value = 4'hf; hex_out = HEX_15; end
 
	endcase
	end
  end
 
 
  ///////////////////////////////////////////////
 ////   latch data for command and display   ///
 ///////////////////////////////////////////////
 
 /// HEX 0 /////
 
  always @(posedge clk_50 or negedge RESET)
  begin 
		if (!RESET)
		begin
			
			hex0_out <= zero;   // -- blank display 
			I2C_value0 <= 4'b0; // -- initial value
			
		end
		
		else
		
			if(swt[6] & !swt[5] & !swt[4]) begin // binary [100]
			
			I2C_value0 <= I2C_value; 
			hex0_out <= hex_out;
		end
	end
	
/// HEX 1	////

	 always @(posedge clk_50 or negedge RESET)
  begin 
		if (!RESET)
		begin
			
			hex1_out <= zero; // -- blank display 
			I2C_value1 <= 4'b0; // -- initial value
		end
		
		else
		
			if(swt[6] & !swt[5] & swt[4]) begin // binary [101]
			
			I2C_value1 <= I2C_value; 
			hex1_out <= hex_out;
		end
	end
	
//// HEX 2  /////

	 always @(posedge clk_50 or negedge RESET)
  begin 
		if (!RESET)
		begin
			
			hex2_out <= zero; // -- blank display 
			I2C_value2 <= 4'b0; // -- initial value
		end
		
		else
		
			if(swt[6] & swt[5] & !swt[4]) begin // [binary 110]
			
			I2C_value2 <= I2C_value; 
			hex2_out <= hex_out;
		end
	end
	
///HEX 3	////

	 always @(posedge clk_50 or negedge RESET)
  begin 
		if (!RESET)
		begin
			
			hex3_out <= zero; // -- blank display 
			I2C_value3 <= 4'b0; // -- initial value
		end
		
		else
		
			if(swt[6] & swt[5] & swt[4]) begin // binary [111]
			
			I2C_value3 <= I2C_value; 
			hex3_out <= hex_out;
		end
	end
		
	
/////// HEX 5 and HEX 4 ///////////

 always	hex5_out = HEX_4;// fixed value to HEX display "4"
 always  hex4_out = HEX_0;// fixed value to HEX display "0"
 always  I2C_DATA <= {I2C_value5,I2C_value4,I2C_value3,I2C_value2,I2C_value1,I2C_value0}; // value to be loaded into I2C register
 always	hex6_out = zero; // fixed value to HEX display "off"
 always	hex7_out = zero; // fixed value to HEX display "off"

endmodule 