A factorial (denoted by n!n exclamation mark 𝑛! ) is the product of all positive integers up to a given positive integer nn 𝑛, meaning
n!=n×(n−1)×(n−2)×…×1n exclamation mark equals n cross open paren n minus 1 close paren cross open paren n minus 2 close paren cross … cross 1
𝑛!=𝑛×(𝑛−1)×(𝑛−2)×…×1
, used to count permutations and combinations in math, probability, and computer science, with
0!0 exclamation mark
0!Defined as 1
module factorial_seq (
input clk,
input rst,
input start,
input [3:0] n,
output reg [31:0] fact,
output reg done
);
integer i;
always @(posedge clk or posedge rst) begin
if (rst) begin
fact <= 1;
i <= 1;
done <= 0;
end
else if (start) begin
if (i <= n) begin
fact <= fact * i;
i <= i + 1;
done <= 0;
end
else begin
done <= 1; // factorial completed
end
end
end
endmodule
RTL SCHEMATIC:
POWER:
TIMING: