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> =&gt; '0');

<B>begin</B> -- resize the input ARG to length NEW_SIZE

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>if</B> (NEW_SIZE &lt; 1) <B>then</B> <B>return</B> NAU; <B>end</B> <B>if</B>; -- Return null array.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>if</B> XARG'LENGTH = 0 <B>then</B> <B>return</B> RESULT; <B>end</B> <B>if</B>; -- Null to empty.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>if</B> (RESULT'LENGTH &lt; ARG'LENGTH) <B>then</B> -- Check lengths.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RESULT(RESULT'LEFT <B>downto</B> 0) := XARG(RESULT'LEFT <B>downto</B> 0);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>else </B>-- Need to pad the result with some '0's.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RESULT(RESULT'LEFT <B>downto</B> XARG'LEFT + 1) := (<B>others</B> =&gt; '0');&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RESULT(XARG'LEFT <B>downto</B> 0) := XARG;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>end</B> <B>if</B>; <B>return</B> RESULT;

<B>end</B> RESIZE;

<B>function</B> &quot;+&quot; (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 &lt; 1 return a null array.

<B>if</B> ((L'LENGTH &lt; 1) <B>or</B> (R'LENGTH &lt; 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> &quot;+&quot;;

<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'-&gt;'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> &lt;&gt;) <B>of</B> STD_LOGIC;

<B>type</B> SIGNED <B>is</B> <B>array</B> (NATURAL <B>range</B> &lt;&gt;) <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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Z&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;W&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-

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

(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | U |&nbsp;

(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | X |&nbsp;

(FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE,FALSE, TRUE), -- | 0 |&nbsp;

(FALSE,FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE, TRUE), -- | 1 |&nbsp;

(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | Z |&nbsp;

(FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, TRUE), -- | W |&nbsp;

(FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE,FALSE, TRUE), -- | L |&nbsp;

(FALSE,FALSE,FALSE, TRUE,FALSE,FALSE,FALSE, TRUE, TRUE), -- | H |&nbsp;

( 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&nbsp;&nbsp;&nbsp; (<TT>&quot;10HLXWZ-&quot;),&nbsp;</TT>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; STD_LOGIC_VECTOR&nbsp;&nbsp;&nbsp; (<TT>&quot;HL10----&quot;))&nbsp;&nbsp;&nbsp; </TT>-- is <TT>TRUE</TT></PRE>



<P><A NAME="pgfId=219769"></A>The following code is similar to the first

simple example of Section&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; library</B> STD; <B>use</B> STD.TEXTIO.<B>all</B>;&nbsp;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; library</B> IEEE; <B>use</B> IEEE.STD_LOGIC_1164.<B>all</B>;

<B>use</B> work.NUMERIC_STD.<B>all</B>;&nbsp;

<B>architecture</B> Behave_2 <B>of</B> Counter_1 <B>is&nbsp;</B>

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signal</B> Clock : STD_LOGIC := '0';&nbsp;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signal</B> Count : UNSIGNED (2 <B>downto</B> 0) := &quot;000&quot;;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin</B>&nbsp;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; process</B> <B>begin</B>

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait</B> <B>for</B> 10 ns; Clock &lt;= <B>not</B> Clock;&nbsp;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if</B> (now &gt; 340 ns) <B>then</B> <B>wait</B>;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</B> <B>if</B>;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</B> <B>process</B>;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; process</B> <B>begin&nbsp;</B>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>wait</B> <B>until</B> (Clock = '0');

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>if</B> (Count = 7)&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>then</B> Count &lt;= &quot;000&quot;;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <B>else</B> Count &lt;= Count + 1;&nbsp;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</B> <B>if</B>;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end</B> <B>process</B>;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; process</B> (Count) <B>variable</B> L: LINE; <B>begin</B> write(L, now);&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write(L, STRING'(&quot; Count=&quot;)); write(L, TO_INTEGER(Count));&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writeline(output, L);&nbsp;

<B>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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&nbsp;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 &lt;= 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>&nbsp;<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 &copy; 1997 IEEE. All rights reserved.&nbsp;

<HR ALIGN="LEFT"></P>



<P><A HREF="CH10.htm">Chapter&nbsp;&nbsp;start</A>&nbsp;&nbsp;&nbsp;<A HREF="CH10.11.htm">Previous&nbsp;&nbsp;page</A>&nbsp;&nbsp;&nbsp;<A HREF="CH10.13.htm">Next&nbsp;&nbsp;page</A>

</BODY>



<!--#include file="Copyright.html"--><!--#include file="footer.html"-->

⌨️ 快捷键说明

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