// File name	: topcounter.v
// Written by	: Jianjian Song, 27 October 2000
// /top level module of a decimal counter with 4-digit 7-segment display and stop/start
module topcounter(FIRST, SECOND, THIRD, FOURTH,_STOP, CLK);
input		CLK;	// Clock signal
input		_STOP;	// negative pulse to stop and resume counting
output	[6:0]	FIRST, SECOND, THIRD, FOURTH;	// four 7-segment digits

wire	[3:0]	ONES, TENS, HUNDREDS, THOUSANDS;
wire	[6:0]	D2, D3, D4;
wire	HAULT_LINE;
reg	Q;	// toggle flip-flop output.

// generate one pulse on each negative push of _STOP
oneshot	BUFFER1(.BUFFER_IN(_STOP),.BUFFER_OUT(HAULT_LINE),.CLK(CLK));

// Implement a toggle flip-flop;
always @ (posedge CLK)
	if (HAULT_LINE==0)
		Q = ~Q;
// to connect the counter with inputs of 7-segment display modules
bcd_counter COUNTER1 (.ONES(ONES),.TENS(TENS),.HUNDREDS(HUNDREDS),.THOUSANDS(THOUSANDS),._HAULT(Q),.CLK(CLK));

bcd_to_seven_segment BCD1(.A(ONES), .SEGMENTS(FIRST));

// The following displays are of common anode, and active low.
bcd_to_seven_segment BCD2(.A(TENS), .SEGMENTS(D2));

bcd_to_seven_segment BCD3(.A(HUNDREDS), .SEGMENTS(D3));

bcd_to_seven_segment BCD4(.A(THOUSANDS), .SEGMENTS(D4));

// generate active low signal

assign	SECOND	= ~D2;

assign	THIRD 	= ~D3;

assign	FOURTH	= ~D4;

endmodule