Search code examples
veriloginteger-arithmetic

Using " * " for multiplication of binary numbers, only gives me addition, why? (Code here)


I'm learning operations with " + ", " - " and " * ", addition and subtraction works well, but multiplication gives me only additions, link for the code:

http://www.edaplayground.com/x/NvT

I checked the code, can't understand what's going on. I gave enough space (bits) the result variable.

BTW, It's a code intended for fixed-point operations including fractional numbers, but everything is calculated as integers.


Solution

  • Your select signal is only on 1bit.
    Then when you set select = 2 it assigns the lower bit of 2(2'b10) i.e. 0.
    You should change select declaration by :

    input [1:0] select; // In the module  
    reg   [1:0] select; // In the testbench
    

    To avoid such errors I would advise you to use the complete notation of values:

    x'tnnn...nnn
    

    where x is the width of the signal, t is the type (d for decimal, h for hexa, b for binary,...) and nnn...nnn the value in the type specified.

    For example for the decimal value 2 you will have several notations that will make sense in certain situations:

    2'd2 //2 bits decimal
    2'h2 //2 bits hexadecimal  
    2'b10//2 bits binary
    

    For more informations about these notations you can read this pdf.