Continue to Site

# [SOLVED]Comparator gives wrong Output

Status
Not open for further replies.

##### Newbie level 1
Hi guys,

I had been practicing a few problems in verilog and got stuck on this :

Question : write a program so that output should be 1 when 'x' is greater than or equal to 'y'
x and y are binary

My logic module:

Code Verilog - [expand]1
2
3
4
5
6
7
module comparator(
input x,
input y,
output z);

assign z = ~y + x ;
endmodule

My stimulus/ test-bench module :

Code Verilog - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
timescale 1ns / 1ps
module stimulus;
reg x;
reg y;
wire z;
comparator uut (
.x(x),
.y(y),
.z(z)
);
initial
begin
x=0;
y=0;
#20 x=1;
#20 y=1;

#20 x=0;
#40;

end
initial
begin

\$monitor("x=%d, y=%d, z=%d \n",x,y,z);

end
endmodule`

--------------------------------------------

OUTPUT OBTAINED:

Thank you!!

Last edited by a moderator:

##### Super Moderator
Staff member
Since the question wants you to compare values for greater than the inputs x and y should not be single bits but should instead be a vector, so they can be more than just 1'b1 and 1'b0.

If they are supposed to be 1-bit then the assignment z = ~y + x is wrong since adding won't tell you if x >= y

It should be simply z = ~(~x & y); // result is 0 only if x < y, otherwise if x==y or x>y it is 1