verilog八位数码管显示(输入一个二进制数,对应输出一个十进制数,要求能输出八位十进制数)

2024年12月18日 08:12
有1个网友回答
网友(1):

module b_bcd8 (binary, bcd);
parameter B_SIZE = 8; //定义参数,由于有参数的存在使得本程序有一定的通用性
input binary;
output bcd;//IO声明
wire [B_SIZE-1 : 0] binary;
reg [B_SIZE-1 : 0] bin;//输入的二进制数
reg [B_SIZE+3 : 0] bcd; //用于存储最终结果
reg [B_SIZE+3 : 0] result; //用于存储过程结果

always@( binary )
begin
bin = binary;
result = 0;
repeat ( B_SIZE-1 ) //采用循环移位并进行十进制转换的方法
begin
result[0] = bin[B_SIZE-1];
if ( result[3 : 0] > 4 )
result[3 : 0] = result[3 : 0] + 4'd3;//这里的操作类似于汇编语言中的DA命令
if ( result[7 : 4] > 4 )
result[7 : 4] = result[7 : 4] + 4'd3;
if ( result[11 : 8] > 4 )
result[11 : 8] = result[11 : 8] + 4'd3;
result = result << 1;
bin = bin << 1;//在循环体的末端加入移位语句
end
result[0] = bin[B_SIZE-1];
bcd <= result;
end
endmodule
//********************************************************************************************************

//测试程序

module test_b_bcd8;
reg CLK;
reg [7:0] BINARY;
wire [11:0]BCD;
b_bcd8 zo(BINARY,BCD);
initial
begin
CLK=0;
forever #5 CLK=~CLK;
end

initial
begin
end

initial
begin
repeat (20)@(posedge CLK);BINARY=$random;
repeat (20)@(posedge CLK);BINARY=$random;
repeat (20)@(posedge CLK);BINARY=$random;
repeat (20)@(posedge CLK);BINARY=$random;
repeat (20)@(posedge CLK);BINARY=$random;
$stop;

end
endmodule