top of page
  • Magnitude Comparator

module magnitude_comparator(p,q,r,a,b);
input [3:0]a;
input [3:0]b;
output reg p,q,r;
always@(a or b)
begin
if(a===b) {p,q,r}=3'b100;
else if(a>b)  {p,q,r}=3'b010;
else if(a<b)  {p,q,r}=3'b001;
end
endmodule

//testbench

module magnitude_comparator_test();
reg [3:0]a;
reg [3:0]b;
wire p,q,r;
magnitude_comparator m1(p,q,r,a,b);
initial begin
a=4'b0000; b=4'b0000;
#20 a=4'b0011; b=4'b0001;
#20 a=4'b0010; b=4'b0111;
#20 a=4'b0011; b=4'b0010;
#20 a=4'b0100; b=4'b0100;
#20 a=4'b0101; b=4'b0001;
#20 a=4'b0110; b=4'b1010;
#20 a=4'b0111; b=4'b0101;
end
endmodule

  • Behavioral Modeling
bottom of page