//--------------------------------------------------------------------------------------------------
//
// Title       : Counter
// Design      : quaddecode
// Author      : mbari585
// Company     : MBARI
//
//-------------------------------------------------------------------------------------------------
//
// File        : C:\projects\genosensor\ESP 2003\quaddecode\quaddecode\src\Counter.v
// Generated   : Tue Oct 15 14:20:57 2002
// 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 {Counter}}
module Counter ( Zero ,ce ,count ,Reset ,updown ,Sclk ,sense );

input Zero ;
wire Zero ;
input ce ;
wire ce ;
input Reset ;
wire Reset ;
input updown ;
wire updown ;
input Sclk ;
wire Sclk ;
input sense ;
wire sense ;

output [15:0] count ;
wire [15:0] count ;

//}} End of automatically maintained section

// -- Enter your statements here -- //
//sense is used to case the counter to count up or down based on the sense of
//updown.  This will invert count direction to compensate for the encoder phase.
reg [15:0] location;
assign count = location;

always @ (posedge Sclk)
	begin  
		if (Reset || Zero)location <= 16'h0000;
		else if (ce)
				begin
					if ((updown & !sense) || (!updown & sense))
						begin
							location <= location + {15'b0,1'b1};
						end
					else 
						begin
							location <= location + 16'hFFFF;
							//							location <= location - {15'b0,1'b1};
						end	 
				end	
		else location <= location;
	end
endmodule
