utils_pkg.tb
来自「VHDLVERILOG语言实现的CARDBUS的IP源码,已经实现现场应用」· TB 代码 · 共 581 行 · 第 1/2 页
TB
581 行
-------------------------------------------------------------------------------
-- :
-- File name : UTILS_PKG.TB
-- :
-- Title : Utilities Package
-- :
-- Comments : - Contains some basic functions
-- :
-- :
-- Developers : Eric Zhang
-- :
-------------------------------------------------------------------------------
LIBRARY IEEE, std;
USE IEEE.std_Logic_1164.ALL;
USE IEEE.VITAL_primitives.ALL;
USE IEEE.VITAL_timing.ALL;
USE IEEE.std_logic_textio.ALL;
USE std.textio.ALL;
-------------------------------------------------------------------------------
-- Package Declaration
-------------------------------------------------------------------------------
package utils_pkg is
-- define procedures
procedure RANDOM(variable Seed: inout real; variable X: out real);
procedure initialize_file(f : in string);
procedure print2s(s : in string);
procedure print2f(s : in string; f : in string);
procedure print(s : in string; f : in string);
-- define functions
function int2vec(num : integer; size : natural) return std_logic_vector;
function vec2int(vec : std_logic_vector) return integer;
function int2hstr(num : natural; size : natural) return string;
function int2bstr(num : natural; size : natural) return string;
function int2str(num: natural) return string;
function bool2str(bool : boolean) return string;
function bit2str(bit : std_logic) return string;
function vec2bstr(vec: std_logic_vector; size : natural) return string;
function vec2hstr(vec: std_logic_vector; size : natural) return string;
function str2vec(str : string) return bit_vector;
function bit2bool(bit : std_logic) return boolean;
function HiBit2Bool(bit : std_logic) return boolean;
function LoBit2Bool(bit : std_logic) return boolean;
function IsBit(sig : std_logic) return boolean;
-------------------------------------------------------------------------------
end utils_pkg; -- End Package Declaration
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Package Body
-------------------------------------------------------------------------------
package body utils_pkg is
-------------------------------------------------
-- define procedures
-------------------------------------------------
-- procedure to initialize file
procedure initialize_file(f : in string) is
variable status : FILE_OPEN_STATUS;
file textfile : TEXT;
begin
file_open(status, textfile, f, WRITE_MODE);
case status is
when OPEN_OK =>
-- file successfully opened
-- now, close file
file_close(textfile);
when STATUS_ERROR =>
-- file not opened because there was already
-- an open file associated with the file object
assert(false)
report "FILE: " & f & " not opened!" & LF &
"An open file already associated with file object" & LF &
"UTILS_PKG.VHD -> INITIALIZE_FILE"
severity error;
when NAME_ERROR =>
-- file not opened because there was a
-- system error with filename specified
assert(false)
report "FILE: " & f & " not opened!" & LF &
"System error with filename specified" & LF &
"UTILS_PKG.VHD -> INITIALIZE_FILE"
severity error;
when MODE_ERROR =>
-- file not opened because the specified mode
-- is invalid for the specified file
assert(false)
report "FILE: " & f & " not opened!" & LF &
"Mode not compatible with filename specified" & LF &
"UTILS_PKG.VHD -> INITIALIZE_FILE"
severity error;
when others =>
-- unknown error
assert(false)
report "FILE: " & f & " not opened!" & LF &
"Unknown Error" & LF &
"UTILS_PKG.VHD -> INITIALIZE_FILE"
severity error;
end case;
end initialize_file;
-- print to screen only procedure
procedure print2s(s : in string) is
variable l1: line;
variable str : string(1 downto 1) := " ";
begin
Write(l1,now);
Write(l1, str);
Write(l1, s);
WriteLine(output,l1);
end print2s;
-- print to file only procedure
procedure print2f(s : in string; f : in string) is
file textfile : TEXT;
variable l2: line;
variable str : string(1 downto 1) := " ";
begin
-- Setup Line
Write(l2,now);
Write(l2, str);
Write(l2, s);
file_open(textfile, f, APPEND_MODE);
WriteLine(textfile, l2);
file_close(textfile);
end print2f;
-- print to screen and file procedure
procedure print(s : in string; f : in string) is
begin
print2s(s);
print2f(s, f);
end print;
-- integer to std_logic_vector
function int2vec(num : integer; size : natural) return std_logic_vector is
variable ntmp: natural := num;
variable index: natural := 0;
variable slv: std_logic_vector(size-1 downto 0) := (others => '0');
variable j: integer;
begin
if num = 0 then
return slv;
elsif num = 1 then
slv(0) := '1';
return slv;
else
-- assign the string
while ntmp > 0 loop
case ntmp rem 2 is
when 0 => slv(index) := '0';
when 1 => slv(index) := '1';
when others =>
assert(false)
report "[int2vec] You've got a serious problem!!!"
severity failure;
return "";
end case;
ntmp := ntmp /2;
index := index + 1;
end loop;
return slv;
end if;
end;
-- std_logic_vector to integer
function vec2int(vec : std_logic_vector) return integer is
variable result : integer := 0;
begin
for i in vec'low to vec'high loop
if vec(i) = '1' then
result := result + (2**(i - vec'low));
elsif vec(i) /= '0' then
assert(false)
report "[vec2int] Trying to convert non-bit value!"
severity warning;
end if;
end loop;
return result;
end;
-- integer to hex formatted string
function int2hstr(num : natural; size : natural) return string is
variable ntmp: natural := num;
variable index: natural := 1;
variable stmp: string((size + 1) downto 1):= (others => '0');
variable j: integer;
begin
if num = 0 then
stmp(1) := 'H';
return stmp;
else
-- assign the string
stmp(index) := 'H';
index := index +1;
while ntmp > 0 loop
case ntmp rem 16 is
when 0 => stmp(index) := '0';
when 1 => stmp(index) := '1';
when 2 => stmp(index) := '2';
when 3 => stmp(index) := '3';
when 4 => stmp(index) := '4';
when 5 => stmp(index) := '5';
when 6 => stmp(index) := '6';
when 7 => stmp(index) := '7';
when 8 => stmp(index) := '8';
when 9 => stmp(index) := '9';
when 10 => stmp(index) := 'A';
when 11 => stmp(index) := 'B';
when 12 => stmp(index) := 'C';
when 13 => stmp(index) := 'D';
when 14 => stmp(index) := 'E';
when 15 => stmp(index) := 'F';
when others =>
assert(false)
report "[int2hstr] You've got a serious problem!!!"
severity failure;
return "";
end case;
ntmp := ntmp /16;
index := index + 1;
end loop;
return stmp;
end if;
end;
-- integer to binary string
function int2bstr(num : natural; size : natural) return string is
variable ntmp: natural := num;
variable index: natural := 1;
variable stmp: string(size downto 1) := (others => '0');
variable j: integer;
begin
if num = 0 then
return stmp;
elsif num = 1 then
stmp(1) := '1';
return stmp;
else
-- assign the string
while ntmp > 0 loop
case ntmp rem 2 is
when 0 => stmp(index) := '0';
when 1 => stmp(index) := '1';
when others =>
assert(false)
report "[int2bstr] You've got a serious problem!!!"
severity failure;
return "";
end case;
ntmp := ntmp /2;
index := index + 1;
end loop;
return stmp;
end if;
end;
-- integer to string function
function int2str(num: natural) return string is
variable ntmp: natural := num;
variable index: natural := 1;
variable stmp: string(32 downto 1);
variable j: integer;
begin
if num = 0 then
stmp(1) := '0';
return stmp(1 downto 1);
else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?