VHDL integer와 std_logic_vector 사이의 변환
library IEEE;
use IEEE.Std_Logic_arith.all;
use IEEE.Std_Logic_1164.all;
entity conv is
port ( input : in std_logic_vector (7 downto 0); — std_logic_vector 입력과 integer 입력
input_i : in integer
output: out std_logic_vector (7 downto 0); — std_logic_vector 출력과 integer 출력
output_i : out integer );
end conv;
architecture conv_int of conv is — std_logic_vector를 integer로 변환
function conv_integer (X: std_logic_vector) return integer is
variable result: integer; — 결과로 result 변수 지정
begin
result := 0; — result값 초기화
for i in X’range loop — std_logic_vector 배열의 크기 만큼 반복
result := result * 2; — 2진수의 10진수 변경을 위해 2를 곱함
case X(i) is
when ‘0’ | ‘L’ => null;
when ‘1’ | ‘H’ => result := result +1; — 배열의 값이 1일 때 result에 1을 더함
when others => null;
end case;
end loop;
return result;
end conv_integer;
begin
output_i <= conv_integer(input); — 결과를 output_i에 출력
end conv_int;
architecture conv_vec of conv is — integer를 std_logic_vector로 변환
function conv_vector (Y, L: INTEGER) return std_logic_vector is
constant y_length : integer := L; — 변환할 std_logic_vector의 크기 지정
variable result2: std_logic_vector ( y_length-1 downto 0); — 지정한 크기의 vector 변수 지정
variable y2: integer := Y; — 입력한 integer를 y2 변수로 지정
variable y_div, y_eq, y_res: integer := 0 ; — 몫,곱,나머지를 위한 변수를 지정, 초기화
begin
if ( y2 >= 0) then — 입력이 0보다 클 때 수행
for i in 0 to y_length-1 loop — 지정한 크기 만큼 반복 수행
y_div := y2 / 2; — 입력을 2로 나눔
y_eq := y_div * 2; — 2로 나눈 몫에 2를 곱하고
y_res := y2 – y_eq; — 원래의 값에서 빼서 나머지를 구함
if (y_res >= 1) then — 나머지가 1이면
result2(i) := ‘1’; — std_logic_vector에 1을 입력
else
result2(i) := ‘0’; — std_logic_vector에 0을 입력
end if;
y2 := y2 / 2; — 입력 값을 2로 나누고 위로 반복
end loop;
else
for i in y_length-1 downto 0 loop — 입력이 0보다 작으면 모든 값을 0으로 지정
result2(i) := ‘0’;
end loop;
end if;
return result2;
end conv_vector;
begin
output <= conv_vector(input_i,8);
end conv_vec;

