用verilog实现Arithmetic circuit

1.half adder:

module top_module( 
    input a, b,
    output cout, sum );
    
    assign {cout,sum}=a+b;
endmodule

2.full adder:

module top_module( 
    input a, b, cin,
    output cout, sum );
    
    assign {cout,sum}=a+b+cin;
endmodule

运用全加器模块:

** 3. 3-bit binary ripple-carry adder:**

module top_module( 
    input [2:0] a, b,
    input cin,
    output [2:0] cout,
    output [2:0] sum );
    
    full u1(a[0],b[0],cin,cout[0],sum[0]);
    full u2(a[1],b[1],cout[0],cout[1],sum[1]);
    full u3(a[2],b[2],cout[1],cout[2],sum[2]);
endmodule

module full( 
    input a, b, cin,
    output cout, sum );
    
    assign {cout,sum}=a+b+cin;
endmodule

4.电路图如下:
在这里插入图片描述

module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    wire [2:0] cout;
    
    full u1(x[0],y[0],0,cout[0],sum[0]);
    full u2(x[1],y[1],cout[0],cout[1],sum[1]);
    full u3(x[2],y[2],cout[1],cout[2],sum[2]);
    full u4(x[3],y[3],cout[2],sum[4],sum[3]);
endmodule

module full( 
    input a, b, cin,
    output cout, sum );
    
    assign {cout,sum}=a+b+cin;
endmodule

自然也有更加简单的方法,由于不需要知道每一位的cout

module top_module (
    input [3:0] x,
    input [3:0] y, 
    output [4:0] sum);
    
	assign sum=x+y;
endmodule

5.计算并判断是否溢出:

module top_module (
    input [7:0] a,
    input [7:0] b,
    output [7:0] s,
    output overflow
); 


assign s=a+b;
assign overflow=(a[7] ^ ~b[7])&(a[7]^s[7]);


endmodule

详情见HDLbits Arithmetic circuit.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值