根据输入的选择数和数字(最大为5),输出数字的立方、平方或者阶乘,
代码如下
module mux2_1
(
input wire sys_clk,
input wire sys_rst_n,
input wire [1:0] select,
input wire [2:0] num, //输入的最大数为5
output reg [6:0] result
);
always@ (posedge sys_clk or negedge sys_rst_n)
if(!sys_rst_n)
result <= 7'd0;
else
case(select)
2'b00: result <= num * num * num; //立方
2'b01: result <= num * num; //平方
2'b10: result <= factorial(num); //阶乘
default:result <= 7'd0;
endcase
function [6:0] factorial;
input [2:0] n;
reg [2:0] index;
begin
factorial = 7'd1;
index = 3'd1;
if(n == 3'd0 || n == 3'd1)
factorial = 7'd1;
else
while(n != index ) begin
index = index + 1;
Verilog实现数字立方、平方和阶乘计算

该代码实现了一个Verilog模块,根据输入的选择数和不超过5的数字,输出其立方、平方或阶乘值。测试代码用于验证模块功能,通过随机生成选择数和数字,观察结果输出。在时钟上升沿更新输入并产生输出,输出可能会有1个时钟周期的延迟。
最低0.47元/天 解锁文章
4347

被折叠的 条评论
为什么被折叠?



