LRM読解(Chap.4 式 - Expression)

2008/09/09Verilog::文法import

式 - Expression [LRM 4]

LRM 4.1 Operators

式のまとめ

演算子と,実数(real型? integerとか)・論理型(login型? regとか)に対して演算可能かどうかの対応表を以下に記す.
詳細な説明は4.1.xに記載されているので参照されたい.個人的に気になった部分だけ抜粋し,紹介する.

expressionoperation namefor Realfor Logic
{} {{}}データ連結, 括り(Concatenation, replication)xo
+ - * / **算術演算(和/差/積/商/累乗)(Arithmetic)oo
%モジュロ演算子(Modulus)xo
> >= < <=比較演算子(Relational)oo
!論理否定(Logical negation)oo
&&論理積(Logical and)oo
||論理和(Logical or)oo
==論理一致(Logical equality)oo
!=論理不一致(Logical inequality)oo
===case一致(Case equality)xo
!==case不一致(Case inequality)xo
~Bit的な否定(Bit-wise negation)xo
&Bit的な論理積(Bit-wise and)xo
|Bit的な論理和(Bit-wise inclusive or)xo
Bit的な排他的論理和(Bit-wise exclusive or)xo
^~ or ~^Bit的な一致 (Bit-wise equivalence)xo
&圧縮論理積(Reduction and)xo
~&圧縮論理積/出力否定(NAND)(Reduction nand)xo
|圧縮論理和(Reduction or)xo
~|圧縮論理和/出力否定(NOR)(Reduction nor)xo
^圧縮排他的論理和(XOR)(Reduction xor)xo
~^ or ^~圧縮排他的論理和/否定(XNOR)(Reduction xnor)xo
<<論理的左シフト(Logical left shift)xo
>>論理的右シフト(Logical right shift)xo
<<<算術左シフト(Arithmetic left shift)xo
>>>算術右シフト(Arithmetic right shift)xo
? :3項演算子(Conditional)oo
orイベント和(Event or)oo

real数の論理式・関係演算結果は,1bitのスカラー値として得られます.


LRM 4.1.2 Binary operator precedence // 演算子の優先度

演算子優先度
+ - ! ~ (unary:単項演算子)
**
* / %
+ - (binary)
<< >> <<< >>>
< <= > >=
== != === !==*1
& ~&
^ ^~ ~^
| ~|
&&
||
? : (conditional operator)

LRM 4.1.8 Equality operators(同等式?)

数式例を表形式で示す.ただし,以下の点に注意すること.

  • 同等式の優先度は,関係式よりも低い.
  • 比較時にa,bのbit幅が異なる場合,短いほうの上位bitを拡張し,ゼロフィルして比較する.
  • 関係式の比較値に'x'/'z'が含まれている場合,結果はあいまいとなるので,比較結果としても'1'bx'を返す.
  • 同等式の場合,'x'/'z'の比較も行われるため,結果は必ず真(1)/偽(0)が返る

※resultは1bitであることに変わりは無い.

説明補足
a === b同等式, 一致bitごとに 0,1,z,x を比較. 全部一致で真(1)
a !== b同等式, 不一致bitごとに 0,1,z,x を比較. 全部一致で偽(0)
a == b関係式, 一致bitごとに 0,1 を比較. 全部一致で真(1).
a != b関係式, 不一致bitごとに 0,1 を比較. 全部一致で偽(0).

4.1.10 Bit-wise operators bit演算の真理値表を以下に示す.ANDは0が強く,ORは1が強く,その他は演算を行うと不定になる.→ハイインピは伝播しない.

  • binary AND
& 0 1 x z
00000
101xx
x0xxx
z0xxx
  • binary OR
| 0 1 x z
001xx
11111
xxxxx
zxxxx
  • binary EXOR
0 1 x z
001xx
110xx
xxxxx
zxxxx
  • binary EXNOR
^~ ~^ 0 1 x z
010xx
101xx
xxxxx
zxxxx
  • 単項演算子
~
01
10
xx
zx
LRM 4.1.12 Shift operators

論理シフトは,<<.>>です.算術シフトは,<<<,>>>です.
Verilog HDLでは,シフト演算子の定義はそれぞれ明確になっています.算術演算子は,演算結果がsignedである場合にだけ,MSB bitをシフトしてあいたビットに埋めます.それ以外の全てのケースでは,シフト後のあいたビットをゼロフィルします.

また,演算子の右辺について,下記の注意書きがありました.

If the right operand has an unknown or high impedence value, then the result shall be unknown.
The right operand is always treated as an unsigned number and has no effect on the signedness of the result.
The result signedness is determined by the left-hand operand and the remainder of the expression, as outlined in 4.5.1.


LRM 4.1.13 Conditional operator

3項演算子についての記載です.ここでもz,xの値が存在するため,注意が必要なようです.

syntax

conditional_expression ::= (From Annex A - A.8.3)
  expression1 ? { attribute_instance } expression2 : expression3

expression1 ::=
  expression

expression2 ::=
  expression

expression3 ::=
  expression

expression1を評価し,真(非0)ならばexpression2を評価し,結果を左辺に渡す.
偽(0)ならば,expression3を評価し,結果を左辺に渡す.
expression1が'x'や'z'の場合,expression2とexpression3を評価して,それぞれの結果をビットごとに下記の表に基づいて演算し,左辺に渡す.ただし,実数型ではないことを前提とする.(実数の場合は0となるでしょう?)
演算時にbit幅が異なる場合,短いほうの上位にゼロビットを埋めて長さをあわせる.

both expression2 and expression3 shall be evaluated and their results shall be combined, bit by bit,
using Table 28 to calculate the final result unless expression2 or expression3 is real,
in which case the result shall be 0.
?: 0 1 x z
00xxx
1x1xx
xxxxx
zxxxx

※どちらも同じ値のときだけ値が返る.それ以外はどちらとも考えられるため,不定を返す.
おそらく論理合成するとどちらかの値が出るとは思うのだけれど.

注意
If~Then~Elseと同等と思ったら大間違い!!
exp1の結果に依存するが,不定が入らないように!!!
回路を作る側から考えると,入力不定なのだから,出力はいずれかの値を返すとしかいえない.セレクタのセレクト信号が不定な状態をイメージすればよいのだろう.if文だと,不定の場合はelseに流れてくれるので,ある意味シミュレーション不足になるのかもしれない.シミュレーション結果の不定は,原則は撲滅しましょう,ということでしょうね.


LRM 4.1.14 Concatenations

{}を用いると,bitレベルの結合ができる.ここで,繰り返し指定も可能である.書式は{}の前に繰り返し回数を記述する.ただし,不定値を返すようなものや,0は指定してはいけない(自然数だけ).
例を以下に示す.

{4{w}} // This is equivalent to {w, w, w, w}
a[31:0] = {1’b1, {0{1’b0}} }; //illegal. RHS becomes {1’b1,;
a[31:0] = {1’b1, {1’bz{1’b0}} }; //illegal. RHS becomes {1’b1,;
a[31:0] = {1’b1, {1’bx{1’b0}} }; //illegal. RHS becomes {1’b1,;



*1 : 関係式のほうが強い.同等式は弱いLRM4.1.8

4.2 Operands [4.2]

4.2.1 Vector bit-select and part-select addressing [4.2.1]

real型/realtime型で宣言された変数のビット選択・部分選択は不正と判断されます.

constant part-select

vector net, vector regのみ可.

  vect[msb_expr:lsb_expr]

ただし,いずれの式(msb_expr,lsb_expr)も定数を返す必要がある.変数になった場合は不定を返すことになるだろう.また,msb_exprのほうが,lsb_exprよりも重いbit(more significant bit)をアサインする必要がある.*2


indexed part-select

vector net, vector reg, integer variable, time variableが可.

reg [15:0] big_vect;    // Big endian
reg [0:15] little_vect; // Little endian

    big_vect[lsb_base_expr +: width_expr]
 little_vect[msb_base_expr +: width_expr]
    big_vect[msb_base_expr -: width_expr]
 little_vect[lsb_base_expr -: width_expr]

ただし,width_exprは定数式で,実行時に決定することは許されない.lsb_base_exprとmsb_base_exprは実行時に決定されても良い*3

例を張っておく.宣言時にMSB:LSBで指定しているはずなので,指定した単項演算子+と-がどちらに伸びていくかは把握できるはず.伸びていかないほうの数字を固定して,伸びていく方向にwidthだけビットが取り出されると,理解すればよさそう.

initial begin
if ( big_vect[0 +:8] == big_vect[7 : 0]) begin end
if (little_vect[0 +:8] == little_vect[0 : 7]) begin end
if ( big_vect[15 -:8] == big_vect[15 : 8]) begin end
if (little_vect[15 -:8] == little_vect[8 :15]) begin end
if (sel >0 && sel < 8)
  dword[8*sel +:8] = big_vect[7:0]; // Replace the byte selected.

LRM 4.2.3 Strings

4.2.3.3 Null string handling

ダブルクォートで囲った文字には NULL文字(\0)が付かないけれども,ダブルクォートだけで中身が無い場合("")は,'\0'となるらしい.


4.3 Minimum, typical, and maximum delay expressions

表題のとおり,遅延時間のmin./typ./max.を演算する.



4.4 Expression bit lengths

ビット長拡張について.和算のときは,左辺を含めた演算対象のビット幅のうち,最も大きい幅を用いる.

Expression Bit length Comments
Unsized constant number*4Same as integer
Sized constant numberAs given
i op j, where op is: + - * / % & | ^ ^~ ~^max(L(i),L(j))
op i, where op is: + - ~L(i)
i op j, where op is: === !== == != && || > >= < <=1 bitOperands are sized to max(L(i),L(j))
op i, where op is: & ~& | ~| ^ ~^ ^~ !1 bitAll operands are self-determined
i op j, where op is: >> << **L(i)j is self-determined
i ? j : kmax(L(j),L(k))i is self-determined
{i,...,j}L(i)+..+L(j)All operands are self-determined
{i{j,..,k}}i * (L(j)+..+L(k))All operands are self-determined

乗算時のビット幅に注意.積の場合は,(L(i) + L(j))を期待するが,表のとおり拡張はされない.結果を受ける側(左辺)で必要なビットを用意してasignすること.

例を見たが,納得できない.三つ目.

reg [3:0] a;
reg [5:0] b;
reg [15:0] c;

initial begin
  a = 4’hF;
  b = 6’ha;
  $display("a*b=%x", a*b); // expression size is self determined
  c = {a**b};              // expression a**b is self determined
  $display("a**b=%x", c);  // due to {}
  c = a**b;                // expression size is determined by c
  $display("c=%x", c);
end

//Result:
//  a*b=16 // 96 was truncated since expression size is 6
//  a**b=1 // expression size is 4 bits (size of a)
//  c=21   // example size is 16 bits (size of c)
//  コレ, なぜ21? 4'hF の 10乗だよねぇ?

4.5 Signed expressions

符号拡張について.C言語のキャストと同じように,Verilog-HDLにも型変換のためのシステム関数がある.

$signed - 返り値は符号つきである.
$unsigned - 返り値は符号なしである.
4.5.1 Rules for expressions types
  • operandにのみ依存し,左辺の影響は受けない.
  • 10進数はsigned(符号あり)とみなす.
  • 基数書式つきはunsigned(符号なし)とみなす.ただし,明示的に's'がついている場合は符号ありとする.
  • Bit-selectの結果は,operandにかかわらず,unsigned(符号なし)とみなす.
  • Part-selectの結果は,operandにかかわらず,unsigned(符号なし)とみなす.端から端まで選択した場合も,符号ナシとして扱われる.
Note:
 Concatenate results are unsigned, regardless of the operands.
 Comparison results (1, 0) are unsigned, regardless of the operands.
 Reals converted to integers by type coercion are signed
 The sign and size of any self-determined operand is determined by the operand itself and independent of the remainder of the expression.
 For non-self-determined operands the following rules apply:
 if any operand is real, the result is real;
 if any operand is unsigned, the result is unsigned, regardless of the operator;
 if all operands are signed, the result will be signed, regardless of operator, except as noted.

4.5.2 Steps for evaluating an expression
  • 基本ルールにのっとって,数式のビット長を決定する.
  • 4.5.1のルールを用いて,式の符号を決定する.
  • 式のoperandの型を,式にあわせて強制する.ただし,self-determinedしていないoperandのみ.
  • 同様に,self-determinedしていないbit長を拡張する.必要であれば,符号拡張処理を行う.
-Determine the expression size based upon the standard rules of expression size determination.
-Determine the sign of the expression using the rules outlined in 4.5.1.
-Coerce the type of each operand of the expression (excepting those which are self-determined) to the type of the expression.
-Extend the size of each operand (excepting those which are self-determined) to the size of the expression.
 Perform sign extension if and only if the operand type (after type coercion) is signed.

まともに訳せていないようだな....bit長決定→符号決定→演算のために項ごとに型キャスト→演算のためにbit長拡張/符号拡張


4.5.3 Steps for evaluating an assignment
  • LRM 4.4のルールに基づいて,右辺のサイズを決定する.
  • 必要であれば,右辺のサイズを拡張する.このとき,右辺がsignedで,かつbit長拡張が必要な場合に符号拡張を行う.
  • Determine the size of the RHS by the standard assignment size determination rules (see 4.4)
  • If needed, extend the size of the RHS, performing sign extension if and only if the type of the RHS is signed.

4.5.4 x,zを含むときの符号拡張

signed bitにx,zが入っているときは,bit拡張するときに それぞれx,zで埋める.signed bit以外のx,zについては,触れずにおいておく.

*2 : 添え字の値の大小ではなく,複数ビットとして値を扱う際のbitの並び,だろう.

*3 : 合成結果は,バレルシフタになるんでしょうねぇ.

*4 : 32bitを超える場合はMSBが不定となる.