0

Can we compare integer and binary values in the if statement i.e I have a comparing statement as

 if(o_remainder=remainder and o_quotient=quotient)

where o_remainder and o_quotient are of type std_logic_vector, and remainder and quotient are of type integer.

3
  • What is type std_logic_vector? Should they not be logic, reg or wire compared against integer. Did this not work when you tried it? Commented Nov 14, 2013 at 8:46
  • o_remainder is a reg of type std_logic_vector. and it worked only for few values. So i asked to be sure whether the comparision is valid or not. Commented Nov 14, 2013 at 9:19
  • Should this be tagged as VHDL rather than Verilog? Commented Nov 14, 2013 at 11:29

3 Answers 3

1

Here is a simple test comparing regs to integers:

module test;

reg        [9:0]   val;
reg signed [9:0]   sval;


initial begin
  #1ns;

  val = 10'd1;
  #1 $display("is it equal %b", val == 1);
  val = 10'd2;
  #1 $display("is it equal %b", val == 2);
  val = 10'd3;
  #1 $display("is it equal %b", val == 3);

  //Signed
  val = -10'd1;
  #1 $display("is it equal %b", val == -1);
  val = -10'd2;
  #1 $display("is it equal %b", val == -2);
  val = -10'd3;
  #1 $display("is it equal %b", val == -3);

  // Using Signed register
  sval = -10'd1;
  #1 $display("is it equal %b", sval == -1);
  sval = -10'd2;
  #1 $display("is it equal %b", sval == -2);
  sval = -10'd3;
  #1 $display("is it equal %b", sval == -3);
end
endmodule

Gives me:

is it equal 1
is it equal 1
is it equal 1
is it equal 0
is it equal 0
is it equal 0
is it equal 1
is it equal 1
is it equal 1

For me the positive numbers work fine but fails with negative values. You have to declare the reg as signed. For the comparison to work. With that in mind you should be able to compare reg to integer.

Sign up to request clarification or add additional context in comments.

Comments

1

From your mention of std_logic_vector, I assume you are actually working in VHDL, not Verilog.

If your vectors represent numbers, use the unsigned or signed types from ieee.numeric_std, instead of vectors. Then you can compare directly with integers.

If you insist on using std_logic vector, then convert it before:

 if unsigned(some_vector) < some_integer then

Comments

0

You can compare. The difference between integer and reg is that reg by default is an unsigned type single bit value; whereas integer is signed type 32 bit value. Make sure that o_reminder and reminder are of same type i.e, both are either signed or unsigned. same case with the o_quotient and quotient.

I don't see any issue if you have declared as below.

integer o_reminder, o_quotient;
reg signed [31:0] reminder,quotient;

or

integer unsigned o_reminder, o_quotient;
reg [31:0] reminder,quotient;

1 Comment

Setting the width to 32 bits is not required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.