Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a wire value to an integer in verilog

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.

like image 406
Brahadeesh Avatar asked Apr 14 '11 16:04

Brahadeesh


People also ask

Can WIRE be signed in Verilog?

By default, 'reg' and 'wire' data type are 'unsigned number, whereas 'integer' is signed number.

How do you write numbers in Verilog?

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.

What are the data types in Verilog?

Some net data types are wire, tri, wor, trior, wand, triand, tri0, tri1, supply0, supply1 and trireg. Wire is the most frequently used type.

How do you define a constant in Verilog?

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.


1 Answers

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;
like image 103
Marty Avatar answered Oct 03 '22 06:10

Marty