我正在编写一个Verilog代码,使用8个全加器构造一个8位加法器。该8位加法器应该添加2个输入输入,每个输入8位总线。这是单个全加器的代码:
module FullAdder(
input a_,
input b_,
input cin_,
output sout_,
output cout_
);
wire temp1, temp2, temp3;
assign sout_ = a_ ^ b_ ^ cin_;
assign cout_ = ((a_ & b_) | (b_ & cin_) | (cin_ & a_));
endmodule
这里是8位加法器模块的代码,它将调用全加器8次。
module EightBitAdder(
input [7:0] a,
input [7:0] b,
//input cin,
output cout, //carry; will be sent as OP, but won't be further used.
output [7:0] sout //sum, sent as OP
);
wire try;
begin
FullAdder mg0(.a_(a[0]), .b_(b[0]), .cin_(0), .cout_(try), .sout_(sout[0]));
FullAdder mg1(.a_(a[1]), .b_(b[1]), .cin_(try), .cout_(try), .sout_(sout[1]));
FullAdder mg2(.a_(a[2]), .b_(b[2]), .cin_(try), .cout_(try), .sout_(sout[2]));
FullAdder mg3(.a_(a[3]), .b_(b[3]), .cin_(try), .cout_(try), .sout_(sout[3]));
FullAdder mg4(.a_(a[4]), .b_(b[4]), .cin_(try), .cout_(try), .sout_(sout[4]));
FullAdder mg5(.a_(a[5]), .b_(b[5]), .cin_(try), .cout_(try), .sout_(sout[5]));
FullAdder mg6(.a_(a[6]), .b_(b[6]), .cin_(try), .cout_(try), .sout_(sout[6]));
FullAdder mg7(.a_(a[7]), .b_(b[7]), .cin_(try), .cout_(try), .sout_(sout[7]));
end
endmodule
问题是输出显示不正确。它始终显示第一位,然后用不小心(X)填充其余位。这可能是什么问题?
答案 0 :(得分:1)
相同的try
线由所有8个完整加法器驱动,因此如果同时使用X
和0
驱动,则很可能会产生1
值来自全加器执行。
考虑改为执行导线,例如:
wire [7:0] cout;
并使用不同的全部加法器的不同位,例如:
FullAdder mg0(.a_(a[0]), .b_(b[0]), .cin_(0), .cout_(cout[0]), .sout_(sout[0]));
FullAdder mg1(.a_(a[1]), .b_(b[1]), .cin_(cout[0]), .cout_(cout[1]), .sout_(sout[1]));
...