Implement the following circuit:
译:
实现以下电路:
解法一:
module top_module (
input [3:0] x,
input [3:0] y,
output [4:0] sum);
assign sum = x + y;
endmodule
解法二:
module top_module (
input [3:0] x,
input [3:0] y,
output [4:0] sum);
wire [3:0]cout;
FA fa1(x[0],y[0],0,cout[0],sum[0]);
FA fa2(x[1],y[1],cout[0],cout[1],sum[1]);
FA fa3(x[2],y[2],cout[1],cout[2],sum[2]);
FA fa4(x[3],y[3],cout[2],cout[3],sum[3]);
assign sum[4] = cout[3];
endmodule
module FA(input x,input y,input cin,output cout,output sum);
assign {cout,sum}=x+y+cin;
endmodule
分析:
解法一 是用verilog的计算有进位的特性,但是不可以写成assign sum = {x + y};否则就会被限制在4位;
解法二就是普通的全加器;
运行结果: