how to make a package(byte -> integer)

S

shin

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 -------------
 
C

charles.elias

shin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top