`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2022/03/21 17:21:31
// Design Name: 
// Module Name: sec40
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module down_count(
    output reg [3:0] value,
    output reg borrow,
    input clk,
    input [3:0] set,
    input [3:0] limit,
    input rst_n,
    input decrease
    );
    reg [3:0] value_tmp;
    
    always@*
        if(value == 4'b0000 && decrease)
            begin
            value_tmp = limit;
            borrow = 1'b1;
            end
        else if(decrease)
            begin
            value_tmp = value - 1'b1;
            borrow = 0;
            end
        else
            begin
            value_tmp = value;
            borrow = 0;
            end
            
     always@(posedge clk or negedge rst_n)
        if(~rst_n)
            value <= set;
        else
            value <= value_tmp;

endmodule
