EmbDev.net

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


von M. R. (rojo)


Rate this post
useful
not useful
Hi,

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

In the body of the package I have the corresponding implementation of 
this conversion function:
1
  function CONVTOREG (DATA :std_logic_vector) return reg IS
2
    variable result :integer; 
3
      begin 
4
       result := CONV_INTEGER(unsigned(DATA));
5
      return reg'VAL(result);
6
  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:
1
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.

von user (Guest)


Rate this post
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));

von M. R. (rojo)


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

Thanks a lot!

Best regards.

M.R.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.