- 对于两个3位无符号数字a(a2a1a0(和b(b2b1b0(,构建一个逻辑电路来输出较大的数字
- 我想比较三位数字,并显示较大的数字。但是代码无法工作
-
我找不到我犯的错误。
4.ABTB=A大于B
module comparator(
input [2:0]A,
input [2:0]B,
output reg [2:0]out,
output ABTB,
output ASTB,
output AEQB
);
assign ABTB=(A[2]&(~B[2]))||((~((A[2]&(~B[2]))||((~A[2])&B[2])))&(A[1]&(~B[1])))||((~((A[2]&(~B[2]))||((~A[2])&B[2])))&(~((A[1]&(~B[1]))||((~A[1])&B[1])))&(A[0]&(~B[0])));
assign ASTB=((~A[2])&B[2])||((~((A[2]&(~B[2]))||((~A[2]&B[2])))&((~A[1])&B[1])))||((~((A[2]&(~B[2]))||((~A[2])&B[2])))&(~((A[1]&(~B[1]))||((~A[1])&B[1])))&((~A[0])&B[0]));
assign AEQB=(~((A[2]&(~B[2]))||((~A[2])&B[2])))&(~((A[1]&(~B[1]))||((~A[1])&B[1])))&(~((A[0]&(~B[0]))||((~A[0])&B[0])));
always@*
if(ABTB==1)
assign out=A;
else if(ASTB==1)
assign out=B;
else if(AEQB==1)
assign out=A;
endmodule
module test_comparator;
reg [2:0]A;
reg [2:0]B;
wire ABTB;
wire ASTB;
wire AEQB;
comparator u0(.a(a),.b(b),.abtb(abtb),.astb(astb),.aeqb(aeqb));
initial
begin
A=000;B=001;
#10 A=001;B=001;
#10 A=010;B=001;
#10 A=011;B=001;
#10 A=100;B=001;
#10 A=101;B=001;
#10 A=110;B=001;
#10 A=111;B=001;
#10 A=001;B=001;
#10 A=001;B=001;
#10 A=001;B=001;
end
endmodule
条件(A>B)
可以表示为(对于3位输入A
和B
(:
assign Bit_3 = A[2] & (~B[2]);
assign Bit_2 = (A[2] ^~ B[2]) & (A[1] & (~B[1]));
assign Bit_1 = (A[2] ^~ B[2]) & (A[1] ^~ B[1]) & (A[0] & (~B[0]));
assign ABTB = (Bit_3 | Bit_2 | Bit_1);
然后可以将out
设置为:
assign out = (ABTB) ? A : B;