I want to convert the data in a wire to an integer. For example:
wire [2:0] w = 3'b101;
I want a method that converts this to '5' and stores it in an integer. How can I do that in a better way than this:
j=1;
for(i=0; i<=2; i=i+1)
begin
a=a+(w[i]*j);
j=j*2;
end
Also, how do I convert it back to binary once I have the value in an integer? This seems a clumsy way. Thank you.
By default, 'reg' and 'wire' data type are 'unsigned number, whereas 'integer' is signed number.
Verilog Number Format.The <base format> consist of an apostrophe ( ' ) followed by b (binary, base-2), d (decimal, base-10), o (octal, base-8) or h (hexadecimal, base-16) for integer numbers. If the <base format> is not used, the base is assumed to be decimal.
Some net data types are wire, tri, wor, trior, wand, triand, tri0, tri1, supply0, supply1 and trireg. Wire is the most frequently used type.
In Verilog-1995[6], there are two ways to define constants: the parameter, a constant that is local to a module and macro definitions, created using the `define compiler directive. A parameter, after it is declared, is referenced using the parameter name.
Easy! Conversion is automatic in verilog if you assign to an integer. In verilog, all the data types are just collection on bits.
integer my_int;
always @( w )
my_int = w;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With