⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch09.5.htm

📁 Verilog DHL教程
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML EXPERIMENTAL 970324//EN"><HTML><HEAD><META NAME="GENERATOR" CONTENT="Adobe FrameMaker 5.5/HTML Export Filter"><LINK REL="STYLESHEET" HREF="ch09.css"><TITLE> 9.5	Case statement </TITLE></HEAD><BODY BGCOLOR="#ffffff"><DIV><HR><P><A HREF="ch09.htm">Chapter&nbsp;&nbsp;start</A>&nbsp;&nbsp;&nbsp;<A HREF="ch09.4.htm">Previous&nbsp;&nbsp;page</A>&nbsp;&nbsp;<A HREF="ch09.6.htm">Next&nbsp;&nbsp;page</A></P></DIV><H1 CLASS="Section"><A NAME="pgfId=541"> </A>9.5	<A NAME="11088"> </A>C<A NAME="marker=57"> </A>ase statement </H1><P CLASS="Body"><A NAME="pgfId=545"> </A>The <I CLASS="Emphasis">case</I> statement is a <A NAME="marker=137"> </A>multi-way decision statement that tests whether an expression matches one of a number of other expressions, and branches accordingly. The <A NAME="marker=138"> </A>case statement has the following <A NAME="marker=139"> </A>syntax:</P><P CLASS="Body"><A NAME="pgfId=628"> </A></P><DIV><IMG SRC="ch09-19.gif"></DIV><P CLASS="BNFCapBody"><A NAME="pgfId=629"> </A>Syntax&nbsp;9-6: Syntax for case statement</P><P CLASS="Body"><A NAME="pgfId=558"> </A>The <I CLASS="Emphasis">default</I><A NAME="marker=143"> </A> statement shall be optional. Use of multiple default statements in one case statement shall be illegal.</P><P CLASS="Body"><A NAME="pgfId=396"> </A>The case expression and the case item expression can be computed at runtime; neither expression is required to be a constant expression.</P><DIV><H2 CLASS="Example"><A NAME="pgfId=559"> </A></H2><P CLASS="Body"><A NAME="pgfId=384"> </A>A simple example of the use of the case statement is the decoding of register <CODE CLASS="code">rega</CODE> to produce a value for <CODE CLASS="code">result</CODE> as follows:</P><P CLASS="Body"><A NAME="pgfId=632"> </A></P><DIV><IMG SRC="ch09-20.gif"></DIV><P CLASS="Body"><A NAME="pgfId=634"> </A>The <I CLASS="Emphasis">case item expressions</I> shall be evaluated and compared in the exact order in which they are given. During the linear search, if one of the <CODE CLASS="code">case</CODE> item expressions matches the case expression given in parentheses, then the statement associated with that case item shall be executed. If all comparisons fail, and the default item is given, then the default item statement shall be executed. If the default statement is not given, and all of the comparisons fail, then none of the case item statements shall be executed.</P><P CLASS="Body"><A NAME="pgfId=635"> </A>Apart from syntax, the <A NAME="marker=145"> </A><A NAME="marker=146"> </A><CODE CLASS="code">case</CODE> statement differs from the multi-way if-else-if construct in two important ways:</P><OL><P CLASS="NumberedLista"><A NAME="pgfId=636"> </A>a)	The conditional expressions in the if-else-if construct are more general than comparing one expression with several others, as in the case statement.</P><P CLASS="NumberedListb"><A NAME="pgfId=637"> </A>b)	The case statement provides a definitive result when there are <CODE CLASS="code">x</CODE> and <CODE CLASS="code">z</CODE> values in an expression.</P></OL><P CLASS="Body"><A NAME="pgfId=638"> </A>In a case expression comparison, the comparison only succeeds when each bit matches exactly with respect to the values <CODE CLASS="code">0</CODE>, <CODE CLASS="code">1</CODE>, <CODE CLASS="code">x</CODE>, and <CODE CLASS="code">z</CODE>. As a consequence, care is needed in specifying the expressions in the <CODE CLASS="code">case</CODE> statement. The bit length of all the expressions shall be equal so that exact bit-wise matching can be performed. The length of all the <CODE CLASS="code">case</CODE> item expressions, as well as the case expression in the parentheses, shall be made equal to the length of the longest of the case expression and the case item expression. </P><P CLASS="Note"><A NAME="pgfId=504"> </A>NOTE--The default length of <CODE CLASS="code">x</CODE> and <CODE CLASS="code">z</CODE> is same as the default length of an integer.</P><P CLASS="Body"><A NAME="pgfId=640"> </A>The reason for providing a case expression comparison that handles the <CODE CLASS="code">x</CODE> and <CODE CLASS="code">z</CODE> values is that it provides a mechanism for detecting such values and <A NAME="marker=148"> </A>reducing the pessimism that can be generated by their presence. </P></DIV><DIV><H2 CLASS="Example"><A NAME="pgfId=387"> </A></H2><P CLASS="Body"><A NAME="pgfId=485"> </A>1. <A NAME="marker=149"> </A>The following example illustrates the use of case statement to properly handle <CODE CLASS="code">x</CODE> and <CODE CLASS="code">Z </CODE>values.</P><P CLASS="Body"><A NAME="pgfId=641"> </A></P><DIV><IMG SRC="ch09-21.gif"></DIV><P CLASS="Body"><A NAME="pgfId=643"> </A>In the example above, if <CODE CLASS="code">select[1]</CODE> is <CODE CLASS="code">0</CODE> and <CODE CLASS="code">flaga</CODE> is <CODE CLASS="code">0</CODE>, then whatever the value of <CODE CLASS="code">select[2]</CODE> is <CODE CLASS="code">x</CODE> or <CODE CLASS="code">z</CODE>, <CODE CLASS="code">result</CODE> should be <CODE CLASS="code">0</CODE>--which is resolved by the third case.</P><P CLASS="Body"><A NAME="pgfId=644"> </A>2. The example below shows another way to use a case statement to detect <CODE CLASS="code">x</CODE> and <CODE CLASS="code">z</CODE> values.</P><P CLASS="Body"><A NAME="pgfId=645"> </A></P><DIV><IMG SRC="ch09-22.gif"></DIV><P CLASS="SubSection"><A NAME="pgfId=647"> </A><A NAME="95402"> </A><A NAME="marker=153"> </A>Case statement with don't-cares</P><P CLASS="Body"><A NAME="pgfId=649"> </A>Two other types of case statements are provided to allow handling of don't-care conditions in the case comparisons. One of these treats high-impedance values (<CODE CLASS="code">z</CODE>) as don't-cares, and the other treats both high-impedance and unknown (<CODE CLASS="code">x</CODE>) values as don't-cares.</P><P CLASS="Body"><A NAME="pgfId=650"> </A>These case statements can be used in the same way as the traditional case statement, but they begin with keywords <B CLASS="Keyword">casez</B><A NAME="marker=154"> </A> and <B CLASS="Keyword">casex</B><A NAME="marker=155"> </A> respectively.</P><P CLASS="Body"><A NAME="pgfId=651"> </A>Don't-care values (<CODE CLASS="code">z</CODE> values for casez, <CODE CLASS="code">z</CODE> and <CODE CLASS="code">x</CODE> values for casex) in any bit of either the case expression or the case items shall be treated as don't-care conditions during the comparison, and that bit position shall not be considered. The don't care conditions in case expression can be used to dynamically control which bits should be compared at any time.</P><P CLASS="Body"><A NAME="pgfId=653"> </A>The syntax of literal numbers allows the use of the question mark <CODE CLASS="code">(?)</CODE><A NAME="marker=156"> </A> in place of <CODE CLASS="code">z</CODE> in these case statements. This provides a convenient format for specification of <A NAME="marker=157"> </A>don't-care bits in case statements.</P></DIV><DIV><H2 CLASS="Example"><A NAME="pgfId=818"> </A></H2><P CLASS="Body"><A NAME="pgfId=486"> </A>1. The following is an <A NAME="marker=158"> </A>example of the casez statement. It demonstrates an instruction decode, where values of the most significant bits select which task should be called. If the most significant bit of <CODE CLASS="code">ir</CODE> is a <CODE CLASS="code">1</CODE>, then the task <CODE CLASS="code">instruction1</CODE> is called, regardless of the values of the other bits of <CODE CLASS="code">ir</CODE>.</P><P CLASS="Body"><A NAME="pgfId=655"> </A></P><DIV><IMG SRC="ch09-23.gif"></DIV><P CLASS="Body"><A NAME="pgfId=657"> </A>2. The following is an <A NAME="marker=160"> </A>example of the casex statement. It demonstrates an extreme case of how don't-care conditions can be dynamically controlled during simulation. In this case, if <CODE CLASS="code">r = 8&#180;b01100110</CODE>, then the task <CODE CLASS="code">stat2</CODE> is called.</P><P CLASS="Body"><A NAME="pgfId=658"> </A></P><DIV><IMG SRC="ch09-24.gif"></DIV><P CLASS="SubSection"><A NAME="pgfId=875"> </A>Constant expression in case statement</P><P CLASS="Body"><A NAME="pgfId=876"> </A>A constant expression can be used for case expression. The value of the constant expression shall be compared against case item expressions. </P></DIV><DIV><H2 CLASS="Example"><A NAME="pgfId=487"> </A></H2><P CLASS="Body"><A NAME="pgfId=488"> </A>The following example demonstrates the usage by modeling a 3-bit priority encoder.</P><P CLASS="Body"><A NAME="pgfId=877"> </A></P><DIV><IMG SRC="ch09-25.gif"></DIV><P CLASS="Body"><A NAME="pgfId=878"> </A>Note that the case expression is a constant expression (1). The case items are expressions (bit-selects), and are compared against the constant expression for a match. </P></DIV><HR><P><A HREF="ch09.htm">Chapter&nbsp;&nbsp;start</A>&nbsp;&nbsp;&nbsp;<A HREF="ch09.4.htm">Previous&nbsp;&nbsp;page</A>&nbsp;&nbsp;<A HREF="ch09.6.htm">Next&nbsp;&nbsp;page</A></P></BODY></HTML>

⌨️ 快捷键说明

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