题解 | #4位数值比较器电路#
4位数值比较器电路
https://www.nowcoder.com/practice/e02fde10f1914527b6b6871b97aef86d
`timescale 1ns/1ns
module comparator_4(
input [3:0] A ,
input [3:0] B ,
output wire Y2 , //A>B
output wire Y1 , //A=B
output wire Y0 //A<B
);
wire [2:0] y0;
wire [2:0] y1;
wire [2:0] y2;
wire [2:0] y3;
//Y0=P(A<B)=P(A3<B3)+P(A3=B3)P(A2<B2)+P(A3=B3)P(A2=B2)P(A1<B1)+P(A3=B3)P(A2=B2)P(A1=B1)P(A0<B0)
//Y1=P(A=B)=P(A3=B3)P(A2=B2)P(A1=B1)P(A0=B0)
//Y2=P(A>B)=P(A3>B3)+P(A3=B3)P(A2>B2)+P(A3=B3)P(A2=B2)P(A1>B1)+P(A3=B3)P(A2=B2)P(A1=B1)P(A0>B0)
function [2:0] comparator_1;
input A;
input B;
begin
comparator_1[0] = (!A)&B;
comparator_1[1] = (A&B)|((!A)&(!B));
comparator_1[2] = A&(!B);
end
endfunction
assign y0 = comparator_1(A[0],B[0]);
assign y1 = comparator_1(A[1],B[1]);
assign y2 = comparator_1(A[2],B[2]);
assign y3 = comparator_1(A[3],B[3]);
assign Y0 = y3[0]|(y3[1]&y2[0])|(y3[1]&y2[1]&y1[0])|(y3[1]&y2[1]&y1[1]&y0[0]);
assign Y1 = y3[1]&y2[1]&y1[1]&y0[1];
assign Y2 = y3[2]|(y3[1]&y2[2])|(y3[1]&y2[1]&y1[2])|(y3[1]&y2[1]&y1[1]&y0[2]);
endmodule
#刷题#
module comparator_4(
input [3:0] A ,
input [3:0] B ,
output wire Y2 , //A>B
output wire Y1 , //A=B
output wire Y0 //A<B
);
wire [2:0] y0;
wire [2:0] y1;
wire [2:0] y2;
wire [2:0] y3;
//Y0=P(A<B)=P(A3<B3)+P(A3=B3)P(A2<B2)+P(A3=B3)P(A2=B2)P(A1<B1)+P(A3=B3)P(A2=B2)P(A1=B1)P(A0<B0)
//Y1=P(A=B)=P(A3=B3)P(A2=B2)P(A1=B1)P(A0=B0)
//Y2=P(A>B)=P(A3>B3)+P(A3=B3)P(A2>B2)+P(A3=B3)P(A2=B2)P(A1>B1)+P(A3=B3)P(A2=B2)P(A1=B1)P(A0>B0)
function [2:0] comparator_1;
input A;
input B;
begin
comparator_1[0] = (!A)&B;
comparator_1[1] = (A&B)|((!A)&(!B));
comparator_1[2] = A&(!B);
end
endfunction
assign y0 = comparator_1(A[0],B[0]);
assign y1 = comparator_1(A[1],B[1]);
assign y2 = comparator_1(A[2],B[2]);
assign y3 = comparator_1(A[3],B[3]);
assign Y0 = y3[0]|(y3[1]&y2[0])|(y3[1]&y2[1]&y1[0])|(y3[1]&y2[1]&y1[1]&y0[0]);
assign Y1 = y3[1]&y2[1]&y1[1]&y0[1];
assign Y2 = y3[2]|(y3[1]&y2[2])|(y3[1]&y2[1]&y1[2])|(y3[1]&y2[1]&y1[1]&y0[2]);
endmodule
#刷题#