how to make a package(byte -> integer)

S

shin

Guest
package sample is
type byte is range 00000000 to 1111111;
type integer is range -2147483647 to 2147483647;
function byte_to_integer(x : byte) return integer;
end sample;

PACKAGE BODY sample IS
function byte_to_integer (x : byte) return integer is
++++variable sum : integer :=0;
begin
for i in 0 to 7 loop
if x(i)='1' then
sum :=sum+2**i;
end if;
end loop;
return sum;
end ;
end sample;


-------------- ŔÎĹÍłÝ ÄŤ¸Ž˝ş¸ś KORNET -------------
 
shin wrote:
package sample is
type byte is range 00000000 to 1111111;
type integer is range -2147483647 to 2147483647;
function byte_to_integer(x : byte) return integer;
end sample;

PACKAGE BODY sample IS
function byte_to_integer (x : byte) return integer is
++++variable sum : integer :=0;
begin
for i in 0 to 7 loop
if x(i)='1' then
sum :=sum+2**i;
end if;
end loop;
return sum;
end ;
end sample;


-------------- 인터넷 카리스마 KORNET -------------
You have at least 2 problems here:

1. You have created a type "integer" that already exists. When you
then declare a variable of type integer you get an error. Why not use
the existing integer type?

2. Your type byte cannot accessed as if it were an array. If you
declare byte as a subtype of std_logic vector, for example:

subtype byte is std_logic_vector( 7 downto 0 );

then you can use a loop variable as an index as you did in your code.

Best regards,

Charles
 

Welcome to EDABoard.com

Sponsor

Back
Top