module usr_clk(
input clk, //clock signal
input reset, //reset signal
input i_run, //running signal
input [9:0] i_num_cnt, //count number (set user)
output reg o_idle, // idle set = 1
output reg o_running,
output reg o_done
);
localparam S_IDLE = 2'b00;
localparam S_IRUN = 2'b01;
localparam S_IDONE = 2'b10;
reg [1:0] c_state = S_IDLE;
reg [9:0] count;
reg i_done;
//signal reset
always @(negedge reset) begin
if(!reset)begin
count <= 1'd0;
i_done <= 1'd0;
o_running <= 1'b0;
o_done <= 1'b0;
end
end
// #1 update output signal
always @(*) begin
case (c_state)
S_IDLE : begin
o_idle <= 1'd1;
o_running <= 1'd0;
o_done <= 1'd0;
end
S_IRUN : begin
o_idle <= 1'd0;
o_running <= 1'd1;
o_done <= 1'd0;
end
S_IDONE : begin
o_idle <= 1'd0;
o_running <= 1'd0;
o_done <= 1'd1;
end
endcase
end
// #2 user count
always @(*) begin
if (count >= i_num_cnt-1) begin
i_done <= 1'd1;
c_state <= S_IDONE;
end
else i_done <= 1'd0;
end
// #3 following signal and counting
always @(posedge clk) begin
if(i_run) c_state <= S_IRUN;
case (c_state)
S_IDLE : count <= 1'd0;
S_IRUN : count <= count + 1'd1;
S_IDONE : begin
count <= 1'd0;
c_state <= S_IDLE;
end
endcase
end
endmodule