📄 std_logic_plus.bdy
字号:
-- Source: T:/hwdev/generic/VHDL/std_logic_plus.bdy
-- Designer: Tim Pagden
-- Description: Extensions to the std_logic_class package to allow
-- conversion functions and mixed operators
-- Revision history: T:/hwdev/generic/etc/std_logic_plus_pkg.txt
-- 29.12.97 TP 0.0 created from std_logic_plus.pkg
library IEEE;
library vfp;
package body std_logic_plus is
use IEEE.std_logic_1164.all;
use vfp.integer_class.all;
use vfp.std_logic_class.all;
-- use vfp.integer_class.all;
-- use vfp.real_class.all;
-- extensions to IEEE.std_logic_class
-- std_logic-related types defined in std_logic_1164
-- ____________________
-- conversion functions
-- vfp.std_logic_plus.to_std_ulogic_vector (integer) -> std_ulogic_vector
function to_std_ulogic_vector (
a: integer
) return std_ulogic_vector is
constant y_length: integer := integer_wordlength (a);
constant a_threshold: integer := next_greater_binary_power_minus_1 (a);
variable y_ref: integer;
variable y: integer;
variable y_std_ulogic_vector: std_ulogic_vector(y_length-1 downto 0);
begin
y := a; -- y is a temp variable for conversion
if (a >= 0) then
y_ref := (a_threshold / 2) + 1; -- set the initial threshold for comparison
y_std_ulogic_vector(y_length-1) := '0';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of positive
if (y < y_ref) then
y_std_ulogic_vector(i) := '0';
else
y := y - y_ref; -- y = 0 signifies the end of the process
y_std_ulogic_vector(i) := '1';
end if;
y_ref := y_ref / 2;
end loop;
else
y_ref := -(a_threshold / 2) - 1; -- set the initial threshold for comparison
y_std_ulogic_vector(y_length-1) := '1';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of negative
if (y >= y_ref) then
y_std_ulogic_vector(i) := '1';
else
y := y - y_ref; -- y = -1 at the end of the process
y_std_ulogic_vector(i) := '0';
end if;
y_ref := y_ref / 2;
end loop;
end if;
return y_std_ulogic_vector;
end to_std_ulogic_vector;
-- vfp.std_logic_plus.to_std_logic_vector (integer) -> std_logic_vector
function to_std_logic_vector (
a: integer
) return std_logic_vector is
constant y_length: integer := integer_wordlength (a);
constant a_threshold: integer := next_greater_binary_power_minus_1 (a);
variable y_ref: integer;
variable y: integer;
variable y_std_logic_vector: std_logic_vector(y_length-1 downto 0);
begin
y := a; -- y is a temp variable for conversion
if (a >= 0) then
y_ref := (a_threshold / 2) + 1; -- set the initial threshold for comparison
y_std_logic_vector(y_length-1) := '0';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of positive
if (y < y_ref) then
y_std_logic_vector(i) := '0';
else
y := y - y_ref; -- y = 0 signifies the end of the process
y_std_logic_vector(i) := '1';
end if;
y_ref := y_ref / 2;
end loop;
else
y_ref := -(a_threshold / 2) - 1; -- set the initial threshold for comparison
y_std_logic_vector(y_length-1) := '1';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of negative
if (y >= y_ref) then
y_std_logic_vector(i) := '1';
else
y := y - y_ref; -- y = -1 at the end of the process
y_std_logic_vector(i) := '0';
end if;
y_ref := y_ref / 2;
end loop;
end if;
return y_std_logic_vector;
end to_std_logic_vector;
-- vfp.std_logic_plus.to_std_ulogic_vector (integer, width:integer) -> std_ulogic_vector
function to_std_ulogic_vector (
a: integer;
width: integer
) return std_ulogic_vector is
constant y_length: integer := width;
constant a_threshold: integer := 2 ** (width-2);
variable y_ref: integer;
variable y: integer;
variable y_std_ulogic_vector: std_ulogic_vector(y_length-1 downto 0);
begin
y := a; -- y is a temp variable for conversion
if (a >= 0) then
y_ref := a_threshold; -- set the initial threshold for comparison
y_std_ulogic_vector(y_length-1) := '0';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of positive
if (y < y_ref) then
y_std_ulogic_vector(i) := '0';
else
y := y - y_ref; -- y = 0 signifies the end of the process
y_std_ulogic_vector(i) := '1';
end if;
y_ref := y_ref / 2;
end loop;
else
y_ref := -a_threshold; -- set the initial threshold for comparison
y_std_ulogic_vector(y_length-1) := '1';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of negative
if (y >= y_ref) then
y_std_ulogic_vector(i) := '1';
else
y := y - y_ref; -- y = -1 at the end of the process
y_std_ulogic_vector(i) := '0';
end if;
y_ref := y_ref / 2;
end loop;
end if;
return y_std_ulogic_vector;
end to_std_ulogic_vector;
-- vfp.std_logic_plus.to_std_ulogic_vector (twos_complement) -> std_ulogic_vector
function to_std_ulogic_vector (
a: twos_complement
) return std_ulogic_vector is
variable y: std_ulogic_vector(a'length-1 downto 0);
begin
for i in a'length-1 downto 0 loop
y(i) := a(i);
end loop;
return y;
end to_std_ulogic_vector;
-- vfp.std_logic_plus.to_twos_complement (integer) -> twos_complement
function to_twos_complement (
a: integer
) return twos_complement is
constant y_length: integer := integer_wordlength (a);
constant a_threshold: integer := next_greater_binary_power_minus_1 (a);
variable y_ref: integer;
variable y: integer;
variable y_2sC: twos_complement(y_length-1 downto 0);
begin
y := a; -- y is a temp variable for conversion
if (a >= 0) then
y_ref := (a_threshold / 2) + 1; -- set the initial threshold for comparison
y_2sC(y_length-1) := '0';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of positive
if (y < y_ref) then
y_2sC(i) := '0';
else
y := y - y_ref; -- y = 0 signifies the end of the process
y_2sC(i) := '1';
end if;
y_ref := y_ref / 2;
end loop;
else
y_ref := -(a_threshold / 2) - 1; -- set the initial threshold for comparison
y_2sC(y_length-1) := '1';
for i in y_length-2 downto 0 loop -- perform algorithmic conversion of negative
if (y >= y_ref) then
y_2sC(i) := '1';
else
y := y - y_ref; -- y = -1 at the end of the process
y_2sC(i) := '0';
end if;
y_ref := y_ref / 2;
end loop;
end if;
return y_2sC;
end to_twos_complement;
-- vfp.std_logic_plus.to_twos_complement (std_ulogic_vector) -> twos_complement
function to_twos_complement (
a: std_ulogic_vector
) return twos_complement is
variable y: twos_complement(a'length-1 downto 0);
begin
for i in a'length-1 downto 0 loop
y(i) := a(i);
end loop;
return y;
end to_twos_complement;
-- vfp.std_logic_plus.to_twos_complement (std_logic_vector) -> twos_complement
function to_twos_complement (
a: std_logic_vector
) return twos_complement is
variable y: twos_complement(a'length-1 downto 0);
begin
for i in a'length-1 downto 0 loop
y(i) := a(i);
end loop;
return y;
end to_twos_complement;
-- ____________________
-- arithmetic operators
-- vfp.std_logic_plus."+" (std_ulogic_vector, integer) -> std_ulogic_vector
function "+" (
a: std_ulogic_vector;
b: integer
) return std_ulogic_vector is
variable b_length: integer := integer_wordlength (b);
variable b_std_ulogic_vector: std_ulogic_vector(b_length-1 downto 0);
variable y_length: integer := longest (a, b_std_ulogic_vector);
variable y: std_ulogic_vector(y_length-1 downto 0);
begin
b_std_ulogic_vector := to_std_ulogic_vector (b);
y := a + b_std_ulogic_vector;
return y;
end "+";
function "+" (
a: std_logic_vector;
b: integer
) return std_logic_vector is
variable b_length: integer := integer_wordlength (b);
variable b_std_logic_vector: std_logic_vector(b_length-1 downto 0);
variable y_length: integer := longest (a, b_std_logic_vector);
variable y: std_logic_vector(y_length-1 downto 0);
begin
b_std_logic_vector := to_std_logic_vector (b);
y := a + b_std_logic_vector;
return y;
end "+";
function "-" (
a: std_ulogic_vector;
b: integer
) return std_ulogic_vector is
variable b_length: integer := integer_wordlength (b);
variable b_std_vector: std_ulogic_vector(b_length-1 downto 0);
variable y_length: integer := longest (a, b_std_vector);
variable y: std_ulogic_vector(y_length-1 downto 0);
begin
b_std_vector := to_std_ulogic_vector (b);
y := a - b_std_vector;
return y;
end "-";
function "-" (
a: integer;
b: std_ulogic_vector
) return std_ulogic_vector is
variable a_length: integer := integer_wordlength (a);
variable a_std_vector: std_ulogic_vector(a_length-1 downto 0);
variable y_length: integer := longest (b, a_std_vector);
variable y: std_ulogic_vector(y_length-1 downto 0);
begin
a_std_vector := to_std_ulogic_vector (a);
y := a_std_vector - b;
return y;
end "-";
function "=" (
a: std_ulogic_vector;
b: integer
) return std_ulogic_vector is
variable b_length: integer := integer_wordlength (b);
variable b_std_vector: std_ulogic_vector(b_length-1 downto 0);
variable y: std_ulogic_vector(a'length-1 downto 0);
begin
b_std_vector := to_std_ulogic_vector (b);
for i in 0 to (a'length - 1) loop if (i > (b_length - 1)) then y(i) := '0';
else y(i) := b_std_vector(i); end if; end loop; return y;
end "=";
function "=" (
a: std_logic_vector;
b: integer
) return std_logic_vector is
variable b_length: integer := integer_wordlength (b);
variable b_std_vector: std_logic_vector(b_length-1 downto 0);
variable y: std_logic_vector(a'length-1 downto 0);
begin
b_std_vector := to_std_logic_vector (b);
for i in 0 to (a'length - 1) loop if (i > (b_length - 1)) then y(i) := '0';
else y(i) := b_std_vector(i); end if; end loop; return y;
end "=";
function "=" (
a: twos_complement;
b: integer
) return twos_complement is
variable b_length: integer := integer_wordlength (b);
variable b_2sC_vector: twos_complement(b_length-1 downto 0);
variable y: twos_complement(a'length-1 downto 0);
begin
b_2sC_vector := to_twos_complement (b);
for i in 0 to (a'length - 1) loop if (i > (b_length - 1)) then y(i) := b_2sC_vector(b_length-1);
else y(i) := b_2sC_vector(i); end if; end loop; return y;
end "=";
-- _________________________
-- general purpose functions
-- ____________________
-- hardware functions
-- ____________________
-- mathematical functions
end std_logic_plus;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -