Verilog-比较器
Verliog- comparator
- 对于两个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;
- 对于两个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;