module top_module(
input clk,
input reset,
input ena,
output pm,
output [7:0] hh,
output [7:0] mm,
output [7:0] ss);
wire ena_mm, ena_hh;
assign ena_mm = (ss == 8'h59);
assign ena_hh = ena_mm&&(mm == 8'h59);
count_sm count_s(.clk(clk), .reset(reset), .ena(ena), .q(ss));
count_sm count_m(.clk(clk), .reset(reset), .ena(ena_mm), .q(mm));
count_h count_h(.clk(clk), .reset(reset), .ena(ena_hh), .pm(pm), .ss(ss), .mm(mm), .q(hh));
endmodule
module count_sm(input clk,
input reset,
input ena,
output [7:0] q);
always @(posedge clk) begin
if(reset)
q <= 8'h0;
else begin
if(ena) begin
if(q[3:0] == 4'h9) begin
q[7:4] <= q[7:4] + 1'b1;
q[3:0] <= 4'h0;
end
else
q[3:0] <= q[3:0] +1'b1;
if(q == 8'h59)
q <= 8'h0;
end
else
q <= q;
end
end
endmodule
module count_h(input clk,
input reset,
input ena,
input [7:0] ss,
input [7:0] mm,
output pm,
output [7:0] q);
always @(posedge clk) begin
if(reset) begin
pm <= 0;
q <= 8'h12;
end
else begin
if(ena) begin
if(q[3:0] == 4'h9) begin
q[7:4] <= q[7:4] + 1'b1;
q[3:0] <= 4'h0;
end
else
q[3:0] <= q[3:0] +1'b1;
if(q == 8'h11 && ss == 8'h59 && mm == 8'h59) begin
q <= 8'h12;
pm <= ~pm;
end
//这里不需要与该if对于的else语句,有了反而出错
if(q == 8'h12 && ss == 8'h59 && mm == 8'h59)
q <= 8'h01;
end
else
q <= q;
end
end
endmodule