HDLBits刷题记录 Circuits—Combinational Logic—Arithmetic Circuits

"这篇博客介绍了两种方式来用Verilog实现4位 Ripple-Carry 加法器。第一种方法通过逐位加法器(fadder)模块进行组合;第二种方法则直接用赋值语句实现,更简洁。博客中提到了Verilog在处理加法时的特性,即自动产生进位位,但需要注意的是,使用连接操作符({ }
摘要由CSDN通过智能技术生成

·Adder

 1、typical and fundamental way

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

module fadder(
	input a,b,
    input cin,
    output sum, cout);
    assign sum = a ^ b ^ cin;
    assign cout = (a&b) | (cin&(a^b));
endmodule

2、more suitable and simple way

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值