题解 | #4位数值比较器电路#
4位数值比较器电路
http://www.nowcoder.com/practice/e02fde10f1914527b6b6871b97aef86d
最后A<B的结果可以用((A>B)+(A<B))'=Y2'*Y1'得到。
`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
);
//求~B,方便比较A>B
not (_B3,B[3]);
not (_B2,B[2]);
not (_B1,B[1]);
not (_B0,B[0]);
//每一位比较A>B还是A=B
and (A3_B3,A[3],_B3);//A[3]>B[3]
xnor (A3B3,A[3],B[3]);//A[3]=B[3]
and (A2_B2,A[2],_B2);
xnor (A2B2,A[2],B[2]);
and (A1_B1,A[1],_B1);
xnor (A1B1,A[1],B[1]);
and (A0_B0,A[0],_B0);
xnor (A0B0,A[0],B[0]);
//Y1,A=B
and (Y1,A3B3,A2B2,A1B1,A0B0);
//Y2,A>B
and (Y2_2,A3B3,A2_B2);
and (Y2_3,A3B3,A2B2,A1_B1);
and (Y2_4,A3B3,A2B2,A1B1,A0_B0);
or (Y2,A1_B1,Y2_2,Y2_3,Y2_4);
//Y0=Y2'*Y1'
not (_Y1,Y1);
not (_Y2,Y2);
and (Y0,_Y1,_Y2);
endmodule