ch10.12.htm
来自「介绍asci设计的一本书」· HTM 代码 · 共 297 行 · 第 1/2 页
HTM
297 行
<B>constant</B> ARG_LEFT : INTEGER := ARG'LENGTH-1;
<B>alias</B> XARG : UNSIGNED(ARG_LEFT <B>downto</B> 0) <B>is</B> ARG; -- Descending range.
<B>variable</B> RESULT : UNSIGNED(NEW_SIZE-1 <B>downto</B> 0) := (<B>others</B> => '0');
<B>begin</B> -- resize the input ARG to length NEW_SIZE
<B>if</B> (NEW_SIZE < 1) <B>then</B> <B>return</B> NAU; <B>end</B> <B>if</B>; -- Return null array.
<B>if</B> XARG'LENGTH = 0 <B>then</B> <B>return</B> RESULT; <B>end</B> <B>if</B>; -- Null to empty.
<B>if</B> (RESULT'LENGTH < ARG'LENGTH) <B>then</B> -- Check lengths.
RESULT(RESULT'LEFT <B>downto</B> 0) := XARG(RESULT'LEFT <B>downto</B> 0);
<B>else </B>-- Need to pad the result with some '0's.
RESULT(RESULT'LEFT <B>downto</B> XARG'LEFT + 1) := (<B>others</B> => '0');
RESULT(XARG'LEFT <B>downto</B> 0) := XARG;
<B>end</B> <B>if</B>; <B>return</B> RESULT;
<B>end</B> RESIZE;
<B>function</B> "+" (L, R : UNSIGNED) <B>return</B> UNSIGNED <B>is </B>-- Overloaded '+'.
<B>constant</B> SIZE : NATURAL := MAX(L'LENGTH, R'LENGTH);
<B>begin</B> -- If length of L or R < 1 return a null array.
<B>if</B> ((L'LENGTH < 1) <B>or</B> (R'LENGTH < 1)) <B>then</B> <B>return</B> NAU; <B>end</B> <B>if</B>;
<B>return</B> ADD_UNSIGNED(RESIZE(L, SIZE), RESIZE(R, SIZE), '0'); <B>end</B> "+";
<B>end</B> Part_NUMERIC_BIT;</PRE>
<P><A NAME="pgfId=278450"></A>The following conversion functions are also
part of the NUMERIC_BIT package:</P>
<PRE><B>function</B> TO_INTEGER (ARG : UNSIGNED) <B>return</B> NATURAL;
<B>function</B> TO_INTEGER (ARG : SIGNED) <B>return</B> INTEGER;
<B>function</B> TO_UNSIGNED (ARG, SIZE : NATURAL) <B>return</B> UNSIGNED;
<B>function</B> TO_SIGNED (ARG : INTEGER; SIZE : NATURAL) <B>return</B> SIGNED;
<B>function</B> RESIZE (ARG : SIGNED; NEW_SIZE : NATURAL) <B>return</B> SIGNED;
<B>function</B> RESIZE (ARG : UNSIGNED; NEW_SIZE : NATURAL) <B>return</B> UNSIGNED;
-- set XMAP to convert unknown values, default is 'X'->'0'
<B>function</B> TO_01(S : UNSIGNED; XMAP : STD_LOGIC := '0') <B>return</B> UNSIGNED;
<B>function</B> TO_01(S : SIGNED; XMAP : STD_LOGIC := '0') <B>return</B> SIGNED;</PRE>
<P><A NAME="pgfId=209183"></A>The NUMERIC_STD package is almost identical
to the NUMERIC_BIT package except that the UNSIGNED and SIGNED types are
declared in terms of the STD_LOGIC type from the <TT>Std_Logic_1164</TT>
package as follows:</P>
<PRE><B>library</B> IEEE; <B>use</B> IEEE.STD_LOGIC_1164.<B>all</B>;
<B>package</B> Part_NUMERIC_STD <B>is</B>
<B>type</B> UNSIGNED <B>is</B> <B>array</B> (NATURAL <B>range</B> <>) <B>of</B> STD_LOGIC;
<B>type</B> SIGNED <B>is</B> <B>array</B> (NATURAL <B>range</B> <>) <B>of</B> STD_LOGIC;
<B>end</B> Part_NUMERIC_STD;</PRE>
<P><A NAME="pgfId=216844"></A>The NUMERIC_STD package body is similar to
<TT>NUMERIC_BIT</TT> with the addition of a comparison function called <TT>STD_MATCH</TT>
, illustrated by the following:</P>
<PRE>-- function STD_MATCH (L, R: T) return BOOLEAN;
-- T = STD_ULOGIC UNSIGNED SIGNED STD_LOGIC_VECTOR STD_ULOGIC_VECTOR</PRE>
<P><A NAME="pgfId=252916"></A>The <TT>STD_MATCH</TT> function uses the following
table to compare logic values:</P>
<PRE><B>type</B> BOOLEAN_TABLE <B>is</B> <B>array</B>(STD_ULOGIC, STD_ULOGIC) <B>of</B> BOOLEAN;
<B>constant</B> MATCH_TABLE : BOOLEAN_TABLE := (
---------------------------------------------------------------------
-- U X 0 1 Z W L H -
---------------------------------------------------------------------
(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | U |
(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | X |
(FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE,FALSE, TRUE), -- | 0 |
(FALSE,FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE, TRUE), -- | 1 |
(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | Z |
(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | W |
(FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE,FALSE, TRUE), -- | L |
(FALSE,FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE, TRUE), -- | H |
( TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE));-- | - |</PRE>
<P><A NAME="pgfId=252917"></A>Thus, for example (notice we need type conversions):</P>
<PRE><TT>IM_TRUE = STD_MATCH(</TT>STD_LOGIC_VECTOR (<TT>"10HLXWZ-"), </TT>
STD_LOGIC_VECTOR (<TT>"HL10----")) </TT>-- is <TT>TRUE</TT></PRE>
<P><A NAME="pgfId=219769"></A>The following code is similar to the first
simple example of Section 10.1, but illustrates the use of the <TT>Std_Logic_1164</TT>
and <TT>NUMERIC_STD</TT> packages:</P>
<PRE><B>entity</B> Counter_1 <B>is</B> <B>end</B>;
<B> library</B> STD; <B>use</B> STD.TEXTIO.<B>all</B>;
<B> library</B> IEEE; <B>use</B> IEEE.STD_LOGIC_1164.<B>all</B>;
<B>use</B> work.NUMERIC_STD.<B>all</B>;
<B>architecture</B> Behave_2 <B>of</B> Counter_1 <B>is </B>
<B> signal</B> Clock : STD_LOGIC := '0';
<B> signal</B> Count : UNSIGNED (2 <B>downto</B> 0) := "000";
<B> begin</B>
<B> process</B> <B>begin</B>
<B> wait</B> <B>for</B> 10 ns; Clock <= <B>not</B> Clock;
<B> if</B> (now > 340 ns) <B>then</B> <B>wait</B>;
<B> end</B> <B>if</B>;
<B> end</B> <B>process</B>;
<B> process</B> <B>begin </B>
<B>wait</B> <B>until</B> (Clock = '0');
<B>if</B> (Count = 7)
<B>then</B> Count <= "000";
<B>else</B> Count <= Count + 1;
<B> end</B> <B>if</B>;
<B> end</B> <B>process</B>;
<B> process</B> (Count) <B>variable</B> L: LINE; <B>begin</B> write(L, now);
write(L, STRING'(" Count=")); write(L, TO_INTEGER(Count));
writeline(output, L);
<B> end</B> <B>process</B>;
<B>end</B>;</PRE>
<P><A NAME="pgfId=219867"></A>The preceding code looks similar to the code
in Section 10.1 (and the output is identical), but there is more going
on here:</P>
<UL>
<LI><A NAME="pgfId=219875"></A>Line 3 is a <TT>library</TT> clause and
a <TT>use</TT> clause for the <TT>std_logic_1164</TT> package, so you can
use the <TT>STD_LOGIC</TT> type and the <TT>NUMERIC_BIT</TT> package.
<LI><A NAME="pgfId=219876"></A>Line 4 is a <TT>use</TT> clause for <TT>NUMERIC_BIT</TT>
package that was previously analyzed into the library <TT>work</TT> . If
the package is instead analyzed into the library <TT>IEEE</TT> , you would
use the name <TT>IEEE.NUMERIC_BIT.all</TT> here. The <TT>NUMERIC_BIT</TT>
package allows you to use the type <TT>UNSIGNED</TT> .
<LI><A NAME="pgfId=219889"></A>Line 6 declares <TT>Clock</TT> to be type
<TT>STD_LOGIC</TT> and initializes it to <TT>'0'</TT> , instead of the default initial
value <TT>STD_LOGIC'LEFT</TT> (which is <TT>'U'</TT> ).
<LI><A NAME="pgfId=219896"></A>Line 7 declares <TT>Count</TT> to be a 3-bit
array of type <TT>UNSIGNED</TT> from <TT>NUMERIC_BIT</TT> and initializes
it using a bit-string literal.
<LI><A NAME="pgfId=219918"></A>Line 10 uses the overloaded <TT>'not'</TT>
operator from <TT>std_logic_1164</TT> .
<LI><A NAME="pgfId=219923"></A>Line 15 uses the overloaded '=' operator
from <TT>std_logic_1164</TT> .
<LI><A NAME="pgfId=219934"></A>Line 16 uses the overloaded '=' operator
from <TT>NUMERIC_BIT</TT> .
<LI><A NAME="pgfId=219945"></A>Line 17 requires a bit-string literal, you
cannot use Count <= 0 here.
<LI><A NAME="pgfId=219983"></A>Line 18 uses the overloaded <TT>'+'</TT>
operator from <TT>NUMERIC_BIT</TT> .
<LI><A NAME="pgfId=219984"></A>Line 22 converts <TT>Count</TT> , type <TT>UNSIGNED</TT>
, to type <TT>INTEGER</TT> .
</UL>
<P><HR ALIGN="LEFT"><SPAN CLASS="footnoteNumber">1.</SPAN> <A NAME="pgfId=539364"></A>IEEE
Std 1076.3-1997 was approved by the IEEE Standards Board on 20 March 1997.
The synthesis package code on the following pages is reprinted with permission
from IEEE Std 1076.3-1997, Copyright © 1997 IEEE. All rights reserved.
<HR ALIGN="LEFT"></P>
<P><A HREF="CH10.htm">Chapter start</A> <A HREF="CH10.11.htm">Previous page</A> <A HREF="CH10.13.htm">Next page</A>
</BODY>
<!--#include file="Copyright.html"--><!--#include file="footer.html"-->
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?