EmbDev.net

Forum: FPGA, VHDL & Verilog type conversion of a custom enumeration type


Author: M. R. (rojo)
Posted on:

Rate this post
0 useful
not useful
Hi,

I defined an enumeration type ("reg") in a package header:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
...
...
type reg is (R0, R1, R2, R3);
...and a conversion function which converts type "std_logic_vector" to 
my type "reg":
function CONVTOREG (DATA :std_logic_vector) return reg;

In the body of the package I have the corresponding implementation of 
this conversion function:
  function CONVTOREG (DATA :std_logic_vector) return reg IS
    variable result :integer; 
      begin 
       result := CONV_INTEGER(unsigned(DATA));
      return reg'VAL(result);
  end function;

This works fine. Now I need another conversion function which converts 
the type "reg" back to std_logic_vector" which is declared like this:
function CONV_REG_TO_STDLV (DATA :reg) return std_logic_vector;

But I don't know how to implement that since there is no type conversion 
which accepts my enumeration type. Has anybody an idea how to do that?

Thanks a lot!

Best regards.

M.R.

Author: user (Guest)
Posted on:

Rate this post
0 useful
not useful
this will do it:

function CONV_REG_TO_STDLV (DATA :reg) return std_logic_vector;
  return std_logic_vector(to_unsigned(reg'pos(DATA),2));

Author: M. R. (rojo)
Posted on:

Rate this post
0 useful
not useful
The 'pos attribute helped me.

Thanks a lot!

Best regards.

M.R.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.