verilog学习笔记——Arithmetic Circuits 算数电路

1. Half adder  半加法器

module top_module( 
    input a, b,
    output cout, sum );
    //  方法一:  采用加法
    //  assign {cout , sum} = a + b ;
    
    //  方法二:  采用逻辑门
    assign cout=a&b;
    assign sum=a^b;
    
endmodule

2. 全加法器

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

endmodule

3. 3-bit binary adder  三位二进制加法器

module top_module( 
    input [2:0] a, b,
    input cin,
    output [2:0] cout,
    output [2:0] sum );
    //方法一 : 分步实现 加法器 的每一位
    assign { cout[0] , sum[0]} = a[0] + b[0] + cin;
    assign { cout[1] , sum[1]} = a[1] + b[1] + cout[0];
    assign { cout[2] , sum[2]} = a[2] + b[2] + cout[1];

endmodule
module top_module( 
    input [2:0] a, b,
    input cin,
    output [2:0] cout,
    output [2:0] sum );
    
    // 方法二  : 每一步 都用 一位加法器 实例来完成
    addr addr_0(.a(a[0]),.b(b[0]),.cin(cin),.cout(cout[0]),.sum(sum[0])) ;
    addr addr_1(.a(a[1]),.b(b[1]),.cin(cout[0]),.cout(cout[1]),.sum(sum[1])) ;
    addr addr_2(.a(a[2]),.b(b[2]),.cin(cout[1]),.cout(cout[2]),.sum(sum[2])) ;

endmodule
        
        // 创建 一位二进制加法器 模块
        module addr(
            input a,b,cin,
            output cout,sum);
            assign { cout , sum } = a + b + cin ;
        endmodule

4. Adder


module top_module (
	input [3:0] x,
	input [3:0] y,
	output [4:0] sum
);
    //隐藏内部所有进位, 是个比较简单的加法器,不要复杂化
	// This circuit is a 4-bit ripple-carry adder with carry-out.
	assign sum = x+y;	// Verilog addition automatically produces the carry-out bit.

	// Verilog quirk: Even though the value of (x+y) includes the carry-out, (x+y) is still considered to be a 4-bit number (The max width of the two operands).
	// This is correct:
	// assign sum = (x+y);
	// But this is incorrect:
	// assign sum = {x+y};	// Concatenation operator: This discards the carry-out
endmodule

5. Signed addition overflow

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]&s[7]) || (a[7]&b[7]&~s[7]) ;
    // ~a[7]&~b[7]&s[7]) 正数相加补码是否溢出
    // (a[7]&b[7]&~s[7]) 负数相加补码是否溢出 

endmodule

用二进制表示数字,首位为符号位,0表示正数,1表示负数。

正数的原码、补码、反码一致。

负数的原码,首位为符号位1,其余位表示大小。

负数的反码,首位为符号位1,其余位为原码取反。

负数的补码,首位为负号位1,其余位为反码+1;

 5. 100-bit binary adder

module top_module (
	input [99:0] a,
	input [99:0] b,
	input cin,
	output cout,
	output [99:0] sum
);

	// The concatenation {cout, sum} is a 101-bit vector.
	assign {cout, sum} = a+b+cin;

endmodule

 6. 4-digit BCD adder

 

module top_module ( 
    input [15:0] a, b,
    input cin,
    output cout,
    output [15:0] sum );
    wire w1,w2,w3;
    bcd_fadd di(a[3:0],b[3:0],cin,w1,sum[3:0]);
    bcd_fadd d2(a[7:4],b[7:4],w1,w2,sum[7:4]);
    bcd_fadd d3(a[11:8],b[11:8],w2,w3,sum[11:8]);
    bcd_fadd d4(a[15:12],b[15:12],w3,cout,sum[15:12]);
endmodule

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值