//--------------------------------------------------------------------------------------------------
//
// Title       : BitCounter
// Design      : U4
// Author      : mbari###
// Company     : MBARI
//
//-------------------------------------------------------------------------------------------------
//
// File        : c:\projects\genosensor\2g\cplds\core\u4\src\BitCounter.v
// Generated   : Fri Feb 20 16:17:40 2004
// From        : interface description file
// By          : Itf2Vhdl ver. 1.20
//
//-------------------------------------------------------------------------------------------------
//
// Description : 
//
//-------------------------------------------------------------------------------------------------
`timescale 1ps / 1ps

//{{ Section below this comment is automatically maintained
//   and may be overwritten
//{module {BitCounter}}
module BitCounter ( outDATA ,NegInc ,SCLreset ,Mclk ,SCLenable ,Inc ,DriveSCL ,NegSCL ,ResetDetect ,DCORI2CCNTr ,SCLint );

input Inc ;
wire Inc ;
input ResetDetect ;
wire ResetDetect ;
input NegSCL ;
wire NegSCL ;
input NegInc ;
wire NegInc ;
input DCORI2CCNTr ;
wire DCORI2CCNTr ;
input SCLreset ;
wire SCLreset ;
input Mclk ;
wire Mclk ;

output SCLenable ;
wire SCLenable ;
output [7:0] outDATA ;
wire [7:0] outDATA ;
output SCLint ;
wire SCLint ;

inout DriveSCL ;
wire DriveSCL ;

//}} End of automatically maintained section

// -- Enter your statements here -- //

reg [5:0] counterr;

assign SCLint = (counterr == 6'b0) ? 1'b1 : 1'b0;

assign SCLenable = (counterr == 6'b0) ? 1'b1 : 1'b0; 

assign DriveSCL = (SCLenable) ? 1'b0 : 1'bz;

assign outDATA = (DCORI2CCNTr) ? {2'b0,counterr} : 8'bz;

always @ (negedge Mclk)
	begin  
//		if (SCLreset) counterr <= 6'd9;  //reset to 10 which releases SCL and prepares for counting
//		else
		case ({SCLreset,ResetDetect,Inc,NegInc,NegSCL})
			5'b10000,					
			5'b11000,
			5'b10001,
			5'b11001,
			5'b10010,
			5'b11010,
			5'b10011,
			5'b11011 : counterr <= 6'd10;   //Inc determins if reset 9 or 10, Inc low = 10
			5'b10100,
			5'b11100,
			5'b10101,
			5'b11101,
			5'b10110,
			5'b11110,
			5'b10111,
			5'b11111 : counterr <= 6'd9;   //Inc determins if reset 9 or 10, Inc low = 9
			5'b01010,
			5'b01011 : counterr <= 6'd1;   //Inc and reset down together gives reset 1
			5'b00000,
			5'b01000,
			5'b00100,
			5'b01100 : counterr <= counterr; //do nothing condition
			5'b00001,
			5'b01001,
			5'b00101,
			5'b01101 : counterr <= ((counterr == 6'b0) ? 6'b0 : (counterr - 6'd1));  //subtract one	if not 0
			5'b00010,
			5'b00110,
			5'b01110 : counterr <= counterr + 6'd9; //add 9 more
			5'b00011,
			5'b00111,
			5'b01111 : counterr <= counterr + 6'd8; //add 9 , subtract 1
	 	endcase
	end

endmodule
