mms.vhd

来自「一个航天航空用的Sparc处理器(配美国欧洲宇航局用的R_tems嵌入式操作系统」· VHDL 代码 · 共 1,831 行 · 第 1/5 页

VHD
1,831
字号
------------------------------------------------------------
-- File name : stdioimp.vhd
-- Title : StdIoImp
-- project : SPARC
-- Library : MMS
-- Author(s) : E. Laubacher
-- Purpose : package for improved IO declarations
-- notes : This library was initially developed in the fram of the
--         ERC32 project. It is reused for the LEON test bench.
------------------------------------------------------------
-- Modification history :
------------------------------------------------------------
-- Version No : | Author | Mod. Date : | Changes made :
------------------------------------------------------------
-- v 1.0        | EL     | 92/06/14    | first version
--..........................................................
-- v 1.1        | MR     | 93/10/13    | 2nd version
-- + bug fix in functions ToString when the parameter is either
--   a bit_vector or a std_ulogic_vector or a std_logic_vector.
--   Error in the range declaration.
------------------------------------------------------------
-- Copyright MATRA MARCONI SPACE FRANCE

--  This library is free software; you can redistribute it and/or
--  modify it under the terms of the GNU Library General Public
--  License as published by the Free Software Foundation; either
--  version 2 of the License, or (at your option) any later version.
 
--  This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--  Library General Public License for more details.
 
--  You should have received a copy of the GNU Library General Public
--  License along with this library; if not, write to the Free
--  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
------------------------------------------------------------

library IEEE;
use IEEE.std_logic_1164.all;
use std.textio.all;
package StdIoImp is

	subtype digit is character;	-- case insensitive ('a' = 'A', ... 'f' = 'F')
	subtype base_type is integer range 2 to 16;

	------------------------------------------------------------
	-- Conventions : 
	--   1/ A string is finished by NUL or last character
	--        following characters should not be considered
	--	 2/ Positions in a string refer to the range (1 to S'length)
	--   3/ Blank means ' ' or HT
	------------------------------------------------------------
	-- Function StrLen returns the length of string S
	-- Function StrEqu returns TRUE if S1 equal S2
	--   the comparison goes to the length of the longest string
	--   with N /= 0, the comparison is limited to N characters
	--  Ex : StrEqu("abc", "ab") -> FALSE
	--  Ex : StrEqu("abc", "ab", 2) -> TRUE
	--  Ex : StrEqu("abc", "abd", 2) -> TRUE
	--  Function StrNcEqu is similar but case insensitive
	------------------------------------------------------------

	function StrLen (S : string) return natural;
	function StrEqu (S1, S2 : string; N : natural := 0) return boolean;
	function StrNcEqu (S1, S2 : string; N : natural := 0) return boolean;

	------------------------------------------------------------
	-- Procedure Shift shifts left the string S for N characters,
	--   NUL characters <0> are used for filling
	-- Function UpTo returns the number of character up to 
	--   substring PATTERN included
	--   returns StrLen(S) if pattern not found
	--   the pattern "" is found at position 0
	--
	--  Ex : Shift("ABCDEF", 2)   -> "CDEF<0><0>"
	--  Ex : Shift("ABCDEF")   -> "BCDEF<0>"
	--  Ex : UpTo("ABCDEF", "CD")   ->  4
	--  Ex : UpTo("ABCDEF", "YZ")  -> 6
	--  Ex : UpTo("ABCDEF", "")  -> 0
	--  Ex : Shift("ABCDEF", UpTo("ABCDEF", "CD")) -> "EF<0><0><0><0>"
	------------------------------------------------------------

	procedure Shift(variable S : inout string; N : natural := 1);
	function UpTo(S : string; PATTERN : string) return integer;

	------------------------------------------------------------
	-- function WordLen returns the length of first word 
	-- (separator = blank or NUL)
	-- returns 0 if leading blank or empty string
	-- function NextWord returns the index of the next word in a string
	-- from a given position, the string being considered circular
	-- if S'length < INDEX, then behave as if INDEX = 1
	-- The condition StrLen(S) < INDEX is not verified
	--  test it before use to avoid this case if necessary
	-- for blank or empty string, return 0
	---------------

	function WordLen(S : string) return natural;
	function NextWord(S : string; INDEX : positive) return natural;

	------------------------------------------------------------
	-- Function DigitValue return digit value; 
	--	 assertion error if digit not in base BASE
	-- Function IsDigit returns TRUE iff VALUE is a digit in base BASE
	-- Function IsBlank returns TRUE iff string empty or blank
	-- Function LeadBlank returns the number of leading blanks
	--	for empty string, returns 0
	--  for blank string, returns StrLen(S)
	--  (use IsBlank to avoid these unpleasant cases)
	------------------------------------------------------------

	function DigitValue (VALUE : digit; BASE : base_type := 10) return integer;
	function IsDigit (VALUE : character; BASE : base_type := 10) return boolean;
	function IsBlank (S : string) return boolean;
	function LeadBlank (S : string) return integer;

	-----------------------------------------------------------------------
	-- Functions ToUpper return the upper-case equivalent of the parameter
	-- Functions ToLower return the lower-case equivalent of the parameter
	-- Constant ToDigit gives the equivalent upper-case digit of an integer
	-- Function Mirror reverts a string. Ex : ABCD -> DCBA
	--  Return parameter has same size than operand
	-----------------------------------------------------------------------
	function ToUpper (VALUE : character) return character;
	function ToUpper (S : string) return string;
	function ToLower (VALUE : character) return character;
	function ToLower (S : string) return string;
	function ToDigit (VALUE : natural) return character;
	function Mirror (S : string) return string;

	-----------------------------------------------------------------------
	-- Functions ToString return the string equivalent of VALUE
	-- Fhe size of the return string is adapted to VALUE
	-- For integer, real and time, a format can be given to control the result
	-- These functions are well suited for assertions and Print
	-----------------------------------------------------------------------

	function ToString (VALUE : integer; FORMAT : string := "%d") return string;
	-----------------------------------------------------------------------
	--	format (C-style)|	decimal	|	lower-case 	|		upper-case 	| binary	|
	--	format (C-style)|					|	 	hexa			|		 	hexa			|					|
	-----------------------------------------------------------------------
	--	automatic				|		%d		|			%x			|			%X				| 	%b		|
	--	minimum length	|		%3d		|			%3x			|			%3X				|		%3b		|
	--	fill with 0			|		%03d	|			%03x		|			%03X			|		%03b	|
	-----------------------------------------------------------------------	

	function ToString (VALUE : real; FORMAT : string := "%") return string;
	---------------------------------
	--	format (C-style)|						|
	---------------------------------
	--	exponent				|		%				|
	--	minimum length	|		%3			|
	--	radix length		|		%.2			|	-- 0 stands for exponent
	--	fill with 0			|		%03.2		|
	---------------------------------

	function ToString (VALUE : time; FORMAT : string := "% ns") return string;
	-----------------------------------
	--	format (C-style)|							|
	-----------------------------------
	--	automatic				|		% <unit>	|	 -- unit case insensitive
	--	minimum length	|		%3 ns			|
	-----------------------------------

	function ToString (VALUE : boolean) return string;
	function ToString (VALUE : bit) return string;
	function ToString (VALUE : std_ulogic) return string;
	function ToString (VALUE : bit_vector) return string;
	function ToString (VALUE : std_ulogic_vector) return string;
	function ToString (VALUE : std_logic_vector) return string;

	------------------------------------------------------------
	-- Functions FromString transform a string in a value
	-- For vectors, the size of the vector must be given
	-- For integer, real, time, boolean, the value read must
	--  be followed by end of string or blank
	-- Any error in the input provokes an assertion error
	-- These functions are suited to read a value in a file along with Scan
	------------------------------------------------------------
	function FromString (S : string; BASE : base_type := 10) return integer;
	function FromString (S : string) return real;
	function FromString (S : string) return time;
	function FromString (S : string) return boolean;	-- case insensitive
	function FromString (S : string) return bit;
	function FromString (S : string) return std_ulogic;
	function FromString (S : string; N : natural) return bit_vector;
	function FromString (S : string; N : natural) return std_ulogic_vector;
	function FromString (S : string; N : natural) return std_logic_vector;

	------------------------------------------------------------
	-- Print string on standard output or in a file
	-- An entire line is appended to the file
	------------------------------------------------------------
	procedure Print (S : string);
	procedure Print (F : out text; S : string); 

	------------------------------------------------------------
	-- Scan string from standard input or from a file
	-- Gets a whole line and write it in the string
	-- There is an assertion error if the string is too short
	------------------------------------------------------------
	procedure Scan (S : out string);
	procedure Scan (variable F : in text; S : out string); 

	------------------------------------------------------------
	-- Function GetTiming gets a timing array from a file
	-- The file format must be lines of couples name value
	-- Ex : TPLH  12 ns
	-- Blanks, comments in VHDL style (--) are authorized
	-- Actually when a line does not fit, it is simply skipped
	-- Anything following the value is ignored
	-- The function is case insensitive regarding TP_NAME
	-- TP_NAME gives the names to search for (separated by blanks)
	-- The order needs not to be the same in TP_NAME and in file,
	--  but the function is much faster if it is so (especially if N is high !).
	------------------------------------------------------------
	type tp_array is array (natural range <>) of time;

	function GetTiming (	TP_NAME : string;	-- names of values to load
												N : natural;			-- number of values to load
												FILE_NAME : string) return tp_array;	

	------------------------------------------------------------
	-- The function GetVal gets an integer in the same way
	--  It is very useful for loading a value of any type
	--   by using its position and the attribute 'val
	--  With INF and SUP, assertion error if value outside [INF, SUP]
	--   and return value = INF
	-- In case of error, GetVal returns 0 (first form) or INF (second form)
	------------------------------------------------------------
	function GetVal (FILE_NAME, FIELD : string) return integer;
	function GetVal (	FILE_NAME, FIELD : string; 
										INF, SUP : integer) return integer;

end StdIoImp;	-- package

------------------------------------------------------------
------------------------------------------------------------

package body StdIoImp is

	constant BUF_SIZE : natural := 256;	-- supposed greater than necessary
	constant ZERO_BUFFER : string (1 to BUF_SIZE) := (others => '0');

	----------------------------

	function StrLen (S : string) return natural is
		constant L : integer := S'length;
		alias Si : string(1 to L) is S;
	begin
		for i in 1 to L loop
			if Si(i) = NUL then return i - 1; end if;
		end loop;
		return L;
	end StrLen;	-- function

	----------------------------
	-- the function StrEqu is written for optimal speed
	function StrEqu (S1, S2 : string; N : natural := 0) return boolean is
		constant L1 : integer := S1'length + 1;
		constant L2 : integer := S2'length + 1;
		constant Si1 : string(1 to L1) := S1 & NUL;
		constant Si2 : string(1 to L2) := S2 & NUL;
		variable MinBound : natural := L1;
	begin
		if L2 < MinBound then MinBound := L2; end if;
		if N > 0 and N < MinBound then MinBound := N; end if;
		for i in 1 to MinBound loop
			if Si1(i) /= Si2(i) then return FALSE;
			elsif Si1(i) = NUL then return TRUE;
			end if;
		end loop;
		return TRUE;
	end StrEqu;	-- function

	----------------------------
	-- the function StrNcEqu is written for optimal speed
	function StrNcEqu (S1, S2 : string; N : natural := 0) return boolean is
		constant L1 : integer := S1'length + 1;
		constant L2 : integer := S2'length + 1;
		constant Si1 : string(1 to L1) := S1 & NUL;
		constant Si2 : string(1 to L2) := S2 & NUL;
		variable MinBound : natural := L1;
	begin
		if L2 < MinBound then MinBound := L2; end if;
		if N > 0 and N < MinBound then MinBound := N; end if;
		for i in 1 to MinBound loop
			if ToUpper(Si1(i)) /= ToUpper(Si2(i)) then return FALSE;
			elsif Si1(i) = NUL then return TRUE;
			end if;
		end loop;
		return TRUE;
	end StrNcEqu;	-- function

	----------------------------

	procedure Shift(variable S : inout string; N : natural := 1) is
		constant L : integer := StrLen(S);
		alias Si : string (1 to S'length) is S;
		variable Res : string (1 to S'length) := (others => NUL);
	begin
		if L > 0 then
			if N < L then Res(1 to L - N) := Si(N + 1 to L); end if;
			Si := Res;
		end if;
	end Shift;	-- procedure

	----------------------------

	function UpTo(S : string; PATTERN : string) return integer is
		alias Si : string(1 to S'length) is S;
		constant L1 : integer := StrLen(Si);
		constant L2 : integer := StrLen(PATTERN);
	begin
		if L1 < L2 then return L1; end if;
		for i in 1 to L1 - L2 + 1 loop
			if StrEqu(Si(i to L1), PATTERN, L2) then return i + L2 - 1; end if;
		end loop;
		return L1;
	end UpTo;	-- function

	----------------------------

	function WordLen(S : string) return natural is
		constant L : natural := S'length;
		alias Si : string (1 to L) is S;
	begin
		for i in 1 to L loop
			if Si(i) = NUL or Si(i) = ' ' or Si(i) = HT then return i - 1; end if;
		end loop;
		return L;
	end WordLen;	-- function

	----------------------------

	function NextWord(S : string; INDEX : positive) return natural is
		constant L : natural := S'length;
		alias Si : string (1 to L) is S;
		variable InBlank : boolean := FALSE;
		variable LeftBound : natural;
	begin
		if L = 0 then return 0; end if;
		if L < INDEX then LeftBound := 1;
		else LeftBound := INDEX;
		end if;
		for i in LeftBound to L loop
			case S(i) is
				when NUL 			=> exit;	-- respect string convention
				when ' ' | HT => InBlank := TRUE;
				when others 	=> if InBlank then return i; end if;
			end case;
		end loop;
		InBlank := TRUE;
		for i in 1 to LeftBound loop
			case S(i) is
				when NUL 			=> exit;	-- respect string convention
				when ' ' | HT => InBlank := TRUE;
				when others 	=> if InBlank then return i; end if;
			end case;
		end loop;
		return 0;
	end NextWord;	-- function

	----------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?