`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    15:47:25 08/30/2009 
// Design Name: 
// Module Name:    CountingInDecimal 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module CountingInDecimal(
    input CLK,
    input RESET,
    input ENABLE,
    output [3:0] SEG_SELECT,
    output [7:0] DEC_OUT
    );

wire TriggerOut_16b, DownCounterEnable, TriggerOut_DownCounter, TriggerOut_CounterDeci, TriggerOut_CounterUnits, TriggerOut_CounterDecades, TriggerOut_CounterThousands;
wire [3:0] DownCount;
wire [3:0] CountDeci;
wire [3:0] CountUnits;
wire [3:0] CountDecades;
wire [3:0] CountThousands;
wire [1:0] Select;
wire [4:0] Bin_Dec;
wire [4:0] DownCounterOut;
wire [4:0] CountDeciOut;
wire [4:0] CountUnitsOut;
wire [4:0] CountDecadesOut;
wire [4:0] CountThousandsOut;


GenericCounter  #(.COUNTER_WIDTH(16), .COUNTER_MAX(49999))
Counter16b(
		.CLK(CLK),
		.RESET(1'b0),
 		.ENABLE_IN(1'b1),
		.TRIG_OUT(TriggerOut_16b)
);

assign DownCounterEnable = ENABLE && TriggerOut_16b;

GenericCounter  #(.COUNTER_WIDTH(7), .COUNTER_MAX(99))
DownCounter(
		.CLK(CLK),
		.RESET(RESET),
 		.ENABLE_IN(DownCounterEnable),
		.TRIG_OUT(TriggerOut_DownCounter),
		.COUNT(DownCount)
);

GenericCounter  #(.COUNTER_WIDTH(4), .COUNTER_MAX(9))
CounterDeci(
		.CLK(CLK),
		.RESET(RESET),
 		.ENABLE_IN(TriggerOut_DownCounter),
		.TRIG_OUT(TriggerOut_CounterDeci),
		.COUNT(CountDeci)
);


GenericCounter  #(.COUNTER_WIDTH(4), .COUNTER_MAX(9))
CounterUnits(
		.CLK(CLK),
		.RESET(RESET),
 		.ENABLE_IN(TriggerOut_CounterDeci),
		.TRIG_OUT(TriggerOut_CounterUnits),
		.COUNT(CountUnits)
);

GenericCounter   #(.COUNTER_WIDTH(4), .COUNTER_MAX(9))
CounterDecades(
		.CLK(CLK),
		.RESET(RESET),
 		.ENABLE_IN(TriggerOut_CounterUnits),
		.TRIG_OUT(TriggerOut_CounterDecades),
		.COUNT(CountDecades)
);

GenericCounter  #(.COUNTER_WIDTH(4), .COUNTER_MAX(9))
CounterThousands(
		.CLK(CLK),
		.RESET(RESET),
 		.ENABLE_IN(TriggerOut_CounterDecades),
		.TRIG_OUT(TriggerOut_CounterThousands),
		.COUNT(CountThousands)
);

GenericCounter  #(.COUNTER_WIDTH(2), .COUNTER_MAX(3))
Counter2b(
		.CLK(CLK),
		.RESET(1'b0),
 		.ENABLE_IN(TriggerOut_16b),
		.COUNT(Select)
);

assign CountDeciOut = {1'b0,CountDeci};
assign CountUnitsOut = {1'b1,CountUnits};
assign CountDecadesOut = {1'b0,CountDecades};
assign CountThousandsOut = {1'b0,CountThousands};

FourWayMux Mux4(
    .CounterDeci(CountDeciOut),
    .CounterUnit(CountUnitsOut),
    .CounterDec(CountDecadesOut),
    .CounterThousand(CountThousandsOut),
    .Select(Select),
    .Bin_Out(Bin_Dec)
    );
	 
SevenSegment Seg7(
    .SEG_SELECT_IN(Select),
    .SEG_SELECT_OUT(SEG_SELECT),
    .BIN_IN(Bin_Dec[3:0]),
    .HEX_OUT(DEC_OUT),
    .DOT_IN(Bin_Dec[4]) 
);




endmodule
