4位全加器模块 Verilog 实现
// 4位全加器模块 module full_adder4_1( input [3:0] a, // 4位输入a input [3:0] b, // 4位输入b input cin, // 进位输入 output [3:0]s, // 4位输出s output c); // 进位输出 wire [2:0] count; // 定义三位计数器 full_adder1 u1(.a(a[0]),.b(b[0]),.cin(cin),.s(s[0]),.c(count[0])); // 调用一位全加器 full_adder1 u2(.a(a[1]),.b(b[1]),.cin(count[0]),.s(s[1]),.c(count[1])); full_adder1 u3(.a(a[2]),.b(b[2]),.cin(count[1]),.s(s[2]),.c(count[2])); full_adder1 u4(.a(a[3]),.b(b[3]),.cin(count[2]),.s(s[3]),.c(c)); endmodule
// 仿真模块 module sim_full_adder4_1(); reg [3:0]a; reg [3:0]b; reg cin; wire [3:0]s; wire c; full_adder4_1 u0(a,b,cin,s,c); initial {a,b,cin}=0; always #100 {a,b,cin}={a,b,cin}+1; endmodule
// 译码器模块
module decoder_xianshi2_1(
input wire [4:0]a, // 输入a
output [7:0]bitcode, // 输出8位二进制码
output [6:0]a_to_g1, // 输出7位BCD码
output reg [6:0]a_to_g2); // 输出7位BCD反码
reg [3:0]y1; // 定义4位数字
reg [1:0]y2; // 定义2位数字
assign bitcode=8'b00011000; // 二进制码为00011000
always @(a)
begin
if(a>=30) begin y2=2'b11;y1=a-5'b11110; end // a大于等于30,y2为11,y1为a-11110
else if ((a>=20) & (a<30)) begin y2=2'b10;y1=a-5'b10100; end // a大于等于20且小于30,y2为10,y1为a-10100
else if ((a>=10) & (a<20)) begin y2=2'b01;y1=a-5'b01010; end // a大于等于10且小于20,y2为01,y1为a-01010
else begin y2=2'b00;y1=a; end // a小于10,y2为00,y1为a本身
end
sw_smg10 u1(y1,a_to_g1); // 调用BCD转7段数码管模块
always@(y2)
case(y2)
2'b00:a_to_g2=7'b0000000;
2'b01:a_to_g2=7'b0000110;
2'b10:a_to_g2=7'b1011011;
2'b11:a_to_g2=7'b1001111;
default: a_to_g2=7'b0000000;
endcase
endmodule
// 顶层模块 module full_adder4_1_top( input wire[3:0] a, // 4位输入a input wire[3:0] b, // 4位输入b input wire cin, // 进位输入 output [7:0]bitcode, // 输出8位二进制码 output wire [6:0]a_to_g1, // 输出7位BCD码 output wire [6:0]a_to_g2 ); // 输出7位BCD反码 wire [3:0]s; // 定义4位输出s wire c; full_adder4_1 u1(.a(a),.b(b),.cin(cin),.s(s),.c(c)); // 调用4位全加器模块 decoder_xianshi2_1 u2({c,s},bitcode,a_to_g1, a_to_g2); // 调用译码器模块 endmodule
// 仿真模块 module sim_full_adder_4_1_top(); reg [3:0]a,b; reg cin; wire [7:0]bitcode; wire [6:0]a_to_g1,a_to_g2; full_adder4_1_top u0(a,b,cin,bitcode,a_to_g1,a_to_g2); initial {a,b,cin}=0; always #50 a=a+1; always #200 b=b+1; always #500 cin=~cin; endmodule
原文地址: https://www.cveoy.top/t/topic/oSc4 著作权归作者所有。请勿转载和采集!