📄 ch04.1.htm
字号:
the result of the expression can be determined as zero without evaluatingthe sub-expression <CODE>regB | regC</CODE> .</P><P><P CLASS="SubSection"><A NAME="pgfId=711"></A>Arithmetic operators</P><P><P CLASS="Body"><A NAME="pgfId=568"></A>The binary arithmetic operatorsare the following:</P><P><TABLE BORDER="1" CELLSPACING="2" CELLPADDING="2"><CAPTION ALIGN="TOP"><P CLASS="TableTitle"><A NAME="pgfId=775"></A>Table 4-5: Arithmeticoperators defined</CAPTION><TR><TD><P CLASS="CellBody"><A NAME="pgfId=841"></A><CODE>a + b</CODE></TD><TD><P CLASS="CellBody"><A NAME="pgfId=842"></A>a plus b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=972"></A>a<CODE> - </CODE>b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=973"></A>a minus b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=974"></A>a<CODE> * b</CODE></TD><TD><P CLASS="CellBody"><A NAME="pgfId=975"></A>a multiply by b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=976"></A><CODE>a / b</CODE></TD><TD><P CLASS="CellBody"><A NAME="pgfId=977"></A>a divide by b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=978"></A><CODE>a % b</CODE></TD><TD><P CLASS="CellBody"><A NAME="pgfId=979"></A>a modulo b</TD></TR></TABLE><P CLASS="Body"><A NAME="pgfId=572"></A>The integer division shall truncateany fractional part. The modulus operator, for example <CODE>y % z</CODE>, gives the remainder when the first operand is divided by the second, andthus is zero when <CODE>z</CODE> divides <CODE>y</CODE> exactly. The resultof a modulus operation shall take the sign of the first operand.</P><P><P CLASS="Body"><A NAME="pgfId=576"></A>The unary arithmetic operatorsshall take precedence over the binary operators. The unary operators arethe following:</P><P><TABLE BORDER="1" CELLSPACING="2" CELLPADDING="2"><CAPTION ALIGN="TOP"><P CLASS="TableTitle"><A NAME="pgfId=571"></A>Table 4-6: Unary operatorsdefined</CAPTION><TR><TD><P CLASS="CellBody"><A NAME="pgfId=840"></A>+m</TD><TD><P CLASS="CellBody"><A NAME="pgfId=980"></A>unary plus m (same as m)</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=981"></A>-m</TD><TD><P CLASS="CellBody"><A NAME="pgfId=982"></A>unary minus m</TD></TR></TABLE><P CLASS="Body"><A NAME="pgfId=575"></A>For the arithmetic operators, ifany operand bit value is the unknown value <CODE>x</CODE> , or high impedancevalue <CODE>z</CODE> , then the entire result value shall be <CODE>x</CODE>.</P><P><P CLASS="Body"><A NAME="pgfId=952"></A><A HREF="#pgfId=555">Table 4-7</A>gives examples of modulus operations.</P><P><TABLE BORDER="1" CELLSPACING="2" CELLPADDING="2"><CAPTION ALIGN="TOP"><P CLASS="TableTitle"><A NAME="pgfId=555"></A>Table 4-7: Examples ofmodulus operations</CAPTION><TR><TH><P CLASS="CellHeading"><A NAME="pgfId=1046"></A>Modulus Expression</TH><TH><P CLASS="CellHeading"><A NAME="pgfId=1047"></A>Result</TH><TH><P CLASS="CellHeading"><A NAME="pgfId=1048"></A>Comments</TH></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1049"></A>10 % 3</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1050"></A>1</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1051"></A>10/3 yields a remainder of1</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1052"></A>11 % 3</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1053"></A>2</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1054"></A>11/3 yields a remainder of2</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1055"></A>12 % 3</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1056"></A>0</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1057"></A>12/3 yields no remainder</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1058"></A>-10 % 3</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1059"></A>-1</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1060"></A>the result takes the sign ofthe first operand</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1061"></A>11 % -3</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1062"></A>2</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1067"></A>the result takes the sign ofthe first operand</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1064"></A>-4'd12 % 3</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1065"></A>1</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1066"></A>-4'd12 is seen as a large,positive number that leaves a remainder of 1 when divided by 3</TD></TR></TABLE><P CLASS="SubSection"><A NAME="pgfId=520"></A>Arithmetic expressions withregisters and integers</P><P><P CLASS="Body"><A NAME="pgfId=942"></A>An arithmetic operation on areg type register shall be treated differently than an arithmetic operationon an integer data type. A reg data type shall be treated as an <I>unsigned</I>value and an integer data type shall be treated as a <I>signed</I> value.Thus, if a sized constant with a negative value is stored in a reg typeregister, a positive constant which is a two's complement of the sized constantshall be the value stored in the reg type register. When this register isused in an arithmetic expression, the positive constant shall be used asthe value of the register. In contrast, if a sized constant with a negativevalue is stored in an integer type register and used in an arithmetic expression,the expression shall evaluate using signed arithmetic.</P><P><P CLASS="Body"><A NAME="pgfId=933"></A><A HREF="#pgfId=939">Table 4-8</A>lists how arithmetic operators interpret each data type</P><P><TABLE BORDER="1" CELLSPACING="2" CELLPADDING="2"><CAPTION ALIGN="TOP"><P CLASS="TableTitle"><A NAME="pgfId=939"></A>Table 4-8: Data typeinterpretation by arithmetic operators</CAPTION><TR><TH><P CLASS="CellHeading"><A NAME="pgfId=1290"></A>Data Type</TH><TH><P CLASS="CellHeading"><A NAME="pgfId=1291"></A>Interpretation</TH></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1292"></A>net</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1293"></A>unsigned</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1294"></A>reg</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1302"></A>unsigned</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1296"></A>integer</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1303"></A>signed, two's complement</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1298"></A>time</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1304"></A>unsigned</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1300"></A>real, realtime</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1305"></A>signed, floating point</TD></TR></TABLE></P><P><P CLASS="Body"><A NAME="pgfId=971"></A>The example below shows variousways to divide<I> </I>"minus twelve by three"--using <B>integer</B>and <B>reg</B> data types in expressions.</P><PRE><B>integer</B> intA;<B>reg</B> [15:0] regA;intA = -4'd12;regA = intA / 3; // expression result is -4, // intA is an integer data typeregA = -4'd12; // regA is 65524intA = regA / 3; // expression result is 21841, // regA is a reg data type.intA = -4'd12 / 3; // expression result is 1431655761. // -4'd12 is effectively a reg data typeregA = -12 / 3; // expression result is -4, -12 is effectively // an integer data type. regA is 65532</PRE><P><P CLASS="SubSection"><A NAME="pgfId=584"></A>Relational operators</P><P><P CLASS="Body"><A NAME="pgfId=574"></A><A HREF="#pgfId=499">Table 4-9</A>lists and defines the relational operators.</P><P><TABLE BORDER="1" CELLSPACING="2" CELLPADDING="2"><CAPTION ALIGN="TOP"><P CLASS="TableTitle"><A NAME="pgfId=499"></A>Table 4-9: The relationaloperators defined</CAPTION><TR><TD><P CLASS="CellBody"><A NAME="pgfId=501"></A>a < b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=504"></A>a less than b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=516"></A>a > b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=517"></A>a greater than b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=518"></A>a <= b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=519"></A>a less than or equal to b</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=521"></A>a >= b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=573"></A>a greater than or equal to b</TD></TR></TABLE><P CLASS="Body"><A NAME="pgfId=1068"></A>An expression using these <I>relationaloperators</I> shall yield the scalar value <CODE>0</CODE> if the specifiedrelation is <I>false</I> , or the value <CODE>1</CODE> if it is <I>true</I>. If, due to unknown or high impedance bits in the operands, the relationis <I>ambiguous</I> , then the result shall be one bit unknown value <CODE>(x)</CODE>.</P><P><P CLASS="Body"><A NAME="pgfId=914"></A>When two operands of unequalbit-lengths are used, the smaller operand shall be zero filled on the mostsignificant bit side to extend to the size of the larger operand.</P><P><P CLASS="Body"><A NAME="pgfId=589"></A>All the relational operatorsshall have the same precedence. Relational operators shall have lower precedencethan arithmetic operators.</P><P><P CLASS="Body"><A NAME="pgfId=764"></A>The following examples illustratethe implications of this precedence rule:</P><PRE>a < foo - 1 // this expression is the same asa < (foo - 1) // this expression, but . . .foo - (1 < a) // this one is not the same as foo - 1 < a // this expression</PRE><P><P CLASS="Body"><A NAME="pgfId=591"></A>When <CODE>foo - (1 < a)</CODE>evaluates, the relational expression evaluates first and then either zeroor one is subtracted from <CODE>foo</CODE> . When <CODE>foo - 1 < a</CODE>evaluates, the value of <CODE>foo</CODE> operand is reduced by one and thencompared with <CODE>a</CODE> .</P><P><P CLASS="SubSection"><A NAME="pgfId=593"></A>Equality operators</P><P><P CLASS="Body"><A NAME="pgfId=1072"></A>The <I>equality operators</I>shall rank lower in precedence than the relational operators. <A HREF="#pgfId=585">Table 4-10</A>lists and defines the equality operators.</P><P><TABLE BORDER="1" CELLSPACING="2" CELLPADDING="2"><CAPTION ALIGN="TOP"><P CLASS="TableTitle"><A NAME="pgfId=585"></A>Table 4-10: The equalityoperators defined</CAPTION><TR><TD><P CLASS="CellBody"><A NAME="pgfId=587"></A>a === b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=588"></A>a equal to b, including x and z</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=592"></A>a !== b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=868"></A>a not equal to b, including x and z</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1063"></A>a == b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1069"></A>a equal to b, result maybe unknown</TD></TR><TR><TD><P CLASS="CellBody"><A NAME="pgfId=1070"></A>a != b</TD><TD><P CLASS="CellBody"><A NAME="pgfId=1071"></A>a not equal to b, result maybe unknown</TD></TR></TABLE><P CLASS="Body"><A NAME="pgfId=597"></A>All four equality operators shallhave the same precedence. These four operators compare operands bit forbit, with zero filling if the two operands are of unequal bit-length. Aswith the relational operators, the result shall be <CODE>0</CODE> if comparisonfails, 1 if it succeeds.</P><P><P CLASS="Body"><A NAME="pgfId=598"></A>For the<I> logical equality</I>and <I>logical inequality</I> operators (<CODE> ==</CODE> and <CODE>!=</CODE>), if either operand contains an <CODE>x</CODE> or a <CODE>z</CODE> , thenthe result shall be the unknown value (<CODE> x</CODE> ).</P><P><P CLASS="Body"><A NAME="pgfId=599"></A>For the <I>case equality</I>and <I>case inequality</I> operators(<CODE> ===</CODE> and <CODE>!==</CODE>), the comparison shall be done just as it is in the procedural case statement(see 9.5). Bits which are <CODE>x</CODE> or <CODE>z</CODE> shall be includedin the comparison and must match for the result to be true. The result ofthese operators shall always be a known value, either <CODE>1</CODE> or<CODE>0</CODE> .</P><P><P CLASS="SubSection"><A NAME="pgfId=601"></A>Logical operators</P><P><P CLASS="Body"><A NAME="pgfId=602"></A>The operators <I>logical and</I>(<CODE> &&</CODE> ) and <I>logical or</I> (||) are logical connectives.The result of the evaluation of a logical comparison shall be <CODE>1</CODE>(defined as true), <CODE>0</CODE> (defined as false), or, if the resultis ambiguous, then the result shall be the unknown value (<CODE> x</CODE>). The precedence of <CODE>&&</CODE> is greater than that of <CODE>||</CODE>, and both are lower than relational and equality operators.</P><P><P CLASS="Body"><A NAME="pgfId=984"></A>A third logical operator is theunary <I>logical negation</I> operator <CODE>!</CODE> . The negation operatorconverts a non-zero or true operand into <CODE>0</CODE> and a zero or falseoperand into <CODE>1</CODE> . An ambiguous truth value remains as <CODE>x</CODE>.</P><P><P CLASS="Body"><A NAME="pgfId=983"></A>1. If register <CODE>alpha</CODE>holds the integer value 237 and <CODE>beta</CODE> holds the value zero,then the following examples perform as described:</P><PRE>regA = alpha && beta; // regA is set to 0regB = alpha || beta; // regB is set to 1</PRE><P><P CLASS="Body"><A NAME="pgfId=604"></A>2. The following expression performsa logical and of three sub-expressions without needing any parentheses:</P><PRE>a < size-1 && b != c && index != lastone</PRE><P><P CLASS="Body"><A NAME="pgfId=606"></A>However, it is recommended for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -