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が不定となる.

LRM読解(Chap.3 データタイプ~型の種類)

2008/09/02Verilog::文法import

データタイプ~型の種類[LRM Chap.3]

値 [3.1]

Verilog-HDLには下記の4つの値しか存在しません. *1ただし,NET型では論理値に強度(Strength)を与えることができます.(LRM Chap.7)

文字説明
0論理値0,もしくはfalse状態を示す
1論理値1,もしくはtrue状態を示す
x論理値不定を示す
zHigh Impedance状態を示す

NET型とVariable型 [3.2]

NET

値を保持することは無く,PAD間をつなぐための配線材相当である.syntaxを引用する.

net_declaration ::= (From Annex A - A.2.1.3)
    net_type [ signed ] [ delay3 ] list_of_net_identifiers ;
  | net_type [ drive_strength ] [ signed ] [ delay3 ] list_of_net_decl_assignments ;
  | net_type [ vectored | scalared ] [ signed ] range [ delay3 ] list_of_net_identifiers ;
  | net_type [ drive_strength ] [ vectored | scalared ] [ signed ] range [ delay3 ] list_of_net_decl_assignments ;
  | trireg [ charge_strength ] [ signed ] [ delay3 ] list_of_net_identifiers ;
  | trireg [ drive_strength ] [ signed ] [ delay3 ] list_of_net_decl_assignments ;
  | trireg [ charge_strength ] [ vectored | scalared ] [ signed ] range [ delay3 ] list_of_net_identifiers ;
  | trireg [ drive_strength ] [ vectored | scalared ] [ signed ] range [ delay3 ] list_of_net_decl_assignments ;

net_type ::= (From Annex A - A.2.2.1)
    supply0 | supply1
  | tri | triand | trior | tri0 | tri1
  | wire | wand | wor

drive_strength ::= (From Annex A - A.2.2.2)
   ( strength0 , strength1 )
  | ( strength1 , strength0 )
  | ( strength0 , highz1 )
  | ( strength1 , highz0 )
  | ( highz0 , strength1 )
  | ( highz1 , strength0 )

strength0 ::= supply0 | strong0 | pull0 | weak0

strength1 ::= supply1 | strong1 | pull1 | weak1

charge_strength ::= ( small ) | ( medium ) | ( large )

delay3 ::= (From Annex A - A.2.2.3)
  # delay_value | # ( delay_value [ , delay_value [ , delay_value ] ] )

delay2 ::=
  # delay_value | # ( delay_value [ , delay_value ] )

delay_value ::=
    unsigned_number
  | parameter_identifier
  | specparam_identifier
  | mintypmax_expression

list_of_net_decl_assignments ::= (From Annex A - A.2.3)
  net_decl_assignment { , net_decl_assignment }

list_of_net_identifiers ::=
  net_identifier [ dimension { dimension }]
  { , net_identifier [ dimension { dimension }] }

net_decl_assignment ::= (From Annex A - A.2.4)
  net_identifier = expression

dimension ::= (From Annex A -A.2.5)
  [ dimension_constant_expression : dimension_constant_expression ]

range ::=
  [ msb_constant_expression : lsb_constant_expression ]

(かなりテキトウな理解)
要は,配線と思えばよい?.

  • wire - 単純に端子?間を結ぶ
  • tri - tri-state?何に使うんだろうな...

Variable

syntaxを引用する.値を保持できる器となります.

integer_declaration ::= (From Annex A - A.2.1.3)
  integer list_of_variable_identifiers ;

real_declaration ::=
  real list_of_real_identifiers ;

realtime_declaration ::=
  realtime list_of_real_identifiers ;

reg_declaration ::=
  reg [ signed ] [ range ] list_of_variable_identifiers ;

time_declaration ::=
  time list_of_variable_identifiers ;

real_type ::= (From Annex A - A.2.2.1)
    real_identifier [ = constant_expression ]
  | real_identifier dimension { dimension }

variable_type ::=
    variable_identifier [ = constant_expression ]
  | variable_identifier dimension { dimension }

list_of_real_identifiers ::= (From Annex A - A.2.3)
  real_type { , real_type }

list_of_variable_identifiers ::=
  variable_type { , variable_type }

dimension ::= (From Annex A - A.2.5)
  [ dimension_constant_expression : dimension_constant_expression ]

range ::=
  [ msb_constant_expression : lsb_constant_expression ]

変数としては,下記の種類がある.integer, real, realtime, reg, time.



Vector [3.3]

NET/Variable宣言時に,型とシンボルの間に記述する.書式では'range'と記していたもの.VectorのReg/Netは,2を法とする,2^nの演算に従う.(n=ベクタのビット長)

range ::=
  [ msb_constant_expression : lsb_constant_expression ]

MSB>LSBである必要は無い.,2^(abs(MSB-LSB)).Noteにて,ここで確保可能なサイズについて記されている.

1) Implementations may set a limit on the maximum length of a vector, but they will at least be 65536 (216) bits.
2) Implementations do not have to detect overflow of integer operations.

1)最低でも,ベクタ長は 2^16(65536) をサポートすること.
2)整数演算のオーバフローは検出する必要は無い.



Arrays [3.10]

net/variable宣言時に,シンボル名の後ろに"[mm:ll]"で指定する.また,LRM 3.10.noteより,

Implementations may limit the maximum size of an array, but they shall at least be 16777216 (224).

Parameters [3.11]

Parametersは,variable型にも,net型にも属さない.また,変数ではなく,定数である.
module parameters と specify parameters とがある.Parameter宣言は,net型/parameter型/variable型で,既に宣言された名称で再定義することはできない.

Parameters

syntax

local_parameter_declaration ::= (From Annex A - A.2.1.1)
  localparam [ signed ] [ range ] list_of_param_assignments ;
  | localparam integer list_of_param_assignments ;
  | localparam real list_of_param_assignments ;
  | localparam realtime list_of_param_assignments ;
  | localparam time list_of_param_assignments ;

parameter_declaration ::=
  parameter [ signed ] [ range ] list_of_param_assignments ;
  | parameter integer list_of_param_assignments ;
  | parameter real list_of_param_assignments ;
  | parameter realtime list_of_param_assignments ;
  | parameter time list_of_param_assignments ;

list_of_param_assignments ::= (From Annex A - A.2.3)
  param_assignment { , param_assignment }

param_assignment ::= (From Annex A - A.2.4)
  parameter_identifier = constant_expression

range ::= (From Annex A - A.2.5)
  [ msb_constant_expression : lsb_constant_expression ]
  • list_of_param_assignmentsは,module itemの中か,module_parameter_port_list*2の中のみ記述可能.
  • param_assignmentsが,module_parameter_port_list内に存在しない場合,param_assignmentsはlocal parameterとなり,どんな手段でもoverride不可能となる.

Parameterの定数再定義について

runtimeに書き換えることはできません.しかし,module parameterは,compilation時に,module定義時の値と異なる値に書き換えることは可能です.これはmodule instanceのカスタムを許容します.
parameterの修正は,defparam文か,module instance文で修正可能です.

parameter文の代表的な使い方は,delay(遅延時間)や変数のビット幅(width)を定義するのに使います.Chap..12で詳細に記します.


module parameterはタイプ指定とrange指定(range)を持つことができます。以下の規則に従って、module parameterのタイプとrange(range)があるものとします.

  • タイプもrange指定もないparameter宣言は,すべての値のオーバーライドも適用された後の,パラメタに割り当てられた最終値のタイプとrangeをデフォルトとするものとします。
  • range指定はあるが,タイプ指定のないパラメタは,符号なしのパラメタ定義rangeになるでしょう.
    符号とrangeは,値のオーバーライドによる影響を受けさせないものとします.
  • range指定が無い,タイプ指定を伴うパラメタは,タイプ指定になるでしょう。
  • 符号ありparameterは,全ての値のオーバーライドを適用した後に,パラメタに割り当てられた最終値のrangeをデフォルトとするものとします、。
  • 符号あり,型指定ありかつrange指定ありのparameterは,符号あり・定義されたrangeとなるでしょう.符号・rangeは,値のオーヴァーライドによる影響を受けない.
  • range指定が無く,かつ,(符号あり型指定されているか,型指定が無い)parameter宣言は,LSB=0, MSBは最終的にparametaにアサインされた値が収まる大きさ以下となるでしょう.
  • range指定が無く,かつ,(符号あり型指定されているか,型指定が無い),かつ最終的にparametaにアサインされた値が符号なしとなるようなparameter宣言は,LSB=0, MSBは31となるでしょう.

// an implied range with an lsb equal to 0 and an msb equal to an implementation-dependent value of at least 31.

※直訳すぎてスミマセン.宣言時に省略した場合や上書きしたときのコンパイラの挙動について仕様が切られているようです.



Local parameters - localparam [3.11.2]

Verilog HDL localparamについて.local parameterは,defparam文や,並び順または名前付きの直接書き換えができません.local parameterは, - local parameter(s) are identical to parameters except that they can not directly be modified with the defparam statement or by the ordered or named parameter value assignment.

Local parameters can be assigned to a constant expression containing a parameter which can be modified with the defparam statement or by the ordered or named parameter value assignment.

See 12.1.3 for details. The syntax for local parameter declarations is given in Syntax 3-4.


Specify parameters[3.11.3]
specparam_declaration ::= (From Annex A - A.2.2.1)
  specparam [ range ] list_of_specparam_assignments ;

list_of_specparam_assignments ::= (From Annex A- A.2.3)
  specparam_assignment { , specparam_assignment }

specparam_assignment ::= (From Annex A - A.2.4)
  specparam_identifier = constant_mintypmax_expression
  | pulse_control_specparam

pulse_control_specparam ::=
  PATHPULSE$ = ( reject_limit_value [ , error_limit_value ] ) ;
  | PATHPULSE$specify_input_terminal_descriptor$specify_output_terminal_descriptor
  = ( reject_limit_value [ , error_limit_value ] ) ;

error_limit_value ::=
  limit_value

reject_limit_value ::=
  limit_value

limit_value ::=
  constant_mintypmax_expression

range ::= (From Annex A - A.2.5)
  [ msb_constant_expression : lsb_constant_expression ]

Table 8. Differences between specparams and parameters

Specparams(specify parameter)Specparams(specify parameter)
Use keyword specparamUse keyword parameter
Shall be declared inside a module or specify blockShall be declared outside specify blocks
May only be used inside a module or specify blockMay not be used inside specify blocks
May be assigned specparams and parametersMay not be assigned specparams
Use SDF annotation to override valuesUse defparam or instance declaration parameter value passing to override values


Name spaces [3.12]

*1 : 例外としてevent型(LRM9.7.3)があります.

*2 : referto SS.12

LRM読解(Chap.2 字句定義/語彙規約)

2008/08/31Verilog::文法import

字句定義/語彙規約 ~ Lexical conventions (LRM-Chap.2)

LRMから,少し抜粋.

数値表現

| binary  | [size]*'[s|S]*(b|B)[01]+
| octal   | [size]*'[s|S]*(o|O)[0-7]+
| hexa    | [size]*'[s|S]*(h|H)[0-9a-fA-F]+
| decimal | ([size]*'[s|S]*(d|D)[0-9]+)|([0-9]+)

LRM 2.5.1 Integer constants

Example 3 Using sign with constant numbers

8'd-6   // 構文違反.
-8'd6   // 6の2の補数を示します.8bitサイズを保持します.
4'shf   // これはbinaryで 4bitの'1111'と書き直せます.
        // 2の補数表現では-1になります.
        // 即ち,これは -4'h1と等価です.
-4'sd15 // 同様にして,これは 4'd1と等価です.(-(-4'd1))

LRM 2.6 Strings

一行内で,double quotes("")で閉じられた部分を文字列とみなします.unsigned int定数として 8bit ASCII文字を取り扱うことができます.

文字列変数?の定義と使い方

stringvarというレジスタ変数を用意し,"Hello world!"を代入してみましょう.

 reg [8*12:1] stringvar;
 initial begin
   stringvar = "Hello world!";
 end

8bit widthの[12:1]配列,と見ればいいんですかねぇ.[8*(欲しい文字数):1]とすればよいようです.C言語と違い,NULL挿入はされない模様です.


LRM 2.6.2 の NOTE

意訳で敵どうだけれど,まぁ,こんなもんで.

When a variable is larger than required to hold a value being assigned,
the contents on the left are padded with zeros after the assignment.
This is consistent with the padding that occurs during assignment of nonstring values.

変数のほうが,文字列よりも大きい場合には,左側がゼロパディングされます.これは,定数のパディングと矛盾しないようになっているからである.(意訳)

If a string is larger than the destination string variable,
the string is truncated to the left, and the leftmost characters will be lost.
文字列のほうが長い場合には,文字列の左側が失われます.
LRM 2.6.3 特殊文字

C言語と同様に,下記の特殊文字が定義されます.エスケープ記号とか..

Escape Stringescape stringで示される文字
\n改行文字
\tタブ文字
\\\(バックスラッシュ, \x5C)
\"double quote(",\x22)
\dddキャラクタコード指定. dは0~7の数値を示す. ただし, 1~3個で構成する(3個必要なわけではない).

LRM 2.7 Identifiers, keywords, and system names

識別子は、それに参照をつけることができるようにユニークな名前をオブジェクトに与えるのに使用されます。識別子は、簡単な識別子かエスケープされた識別子(LRM 2.7.1参照)のどちらかです。簡単な識別子は 文字、数字、ドル記号($)、およびアンダースコア(_)の組み合わせである。簡単な識別氏の一文字目は,数値やドル記号にすべきではないです.英字か,アンダースコアなら可能です.識別子は大文字と小文字を区別するでしょう.

原文

An identifier is used to give an object a unique name so it can be referenced.
An identifier is either a simple identifier or an escaped identifier (see 2.7.1).
A simple identifier shall be any sequence of letters, digits, dollar signs ($), and underscore characters (_).
The first character of a simple identifier shall not be a digit or $;
it can be a letter or an underscore. Identifiers shall be case sensitive.

naming rule?
  • System task/functionの名前は,'$'で始まる.[LRM2.7.4]

2.8 Attribute[LRM 2.8]

syntax

attribute_instance ::= (From Annex A - A.9.1)
  (* attr_spec { , attr_spec } *)

attr_spec ::=
  attr_name = constant_expression
  | attr_name

attr_name ::=
  identifier

2008/08/22(金)[ISE] Verilog Simulation(ISim)

概要とか問題とか

今回,試行錯誤して遊んだメモなので,ワークフローではありません.いじくっているうちに慣れたり覚えたりしますよねぇ?.基礎知識が不足しているからこうして下積みを・・・(^^;

Post Route Simulationにて,配置配線で問題が生じたかを確認する.
Timing Constraintsの設定はClockしかしていない.設定不足か?
また,behavior simulationで得られた期待どおりの波形が見えない(出力信号).

ざっとまわして,いくつか出てきた.どれもこれもsetup time/hold time違反の模様.コレを見て,配置配線前にエラーが出ていなかったので,おかしいなぁと思いつつ,Warningのとり方を調べていた.

WARNING:Simulator:29 - at 6.000350773 ms: Warning:  Timing violation in
   /TST_BSC/uut/\TPU_1/TADD_buf_14 /  $setuphold<setup>( I:6.000350768 ms,
   CLK:6.000350773 ms,132.000 ps,48.000 ps)
WARNING:Simulator:29 - at 6.000350773 ms: Warning:  Timing violation in
   /TST_BSC/uut/\TPU_1/TADD_buf_15 /  $setuphold<setup>( I:6.000350736 ms,
   CLK:6.000350773 ms,132.000 ps,48.000 ps)
WARNING:Simulator:29 - at 6.000380773 ms: Warning:  Timing violation in
   /TST_BSC/uut/\RAM1K_1/RAMB16_S18_inst /  $setuphold<setup>( ADDR:6.000380571
   ms, CLK:6.000380773 ms,377.000 ps,131.000 ps)
WARNING:Simulator:29 - at 6.000380839 ms: Warning:  Timing violation in
   /TST_BSC/uut/\RAM1K_1/RAMB16_S18_inst /  $setuphold<hold>( CLK:6.000380773
   ms, ADDR:6.000380839 ms,377.000 ps,131.000 ps)

半日以上うろうろしていたけれども,最終的には問題ないという見解に落ち着く.おそらくskewにより,使用しない信号を拾うタイミングで発生していると思われる.ポートから受けたデータをDFFでラッチし,その次のサイクルで内部の所望のレジスタへと転送するように実装してある.ところが,今回の箇所は,ポートからラッチするタイミングでviolation警告が出る.DFFにラッチするタイミングがわずかに早くなり,その後段にあるDFFへと信号が先行してしまって,posedge CLKが入ったのではないだろうか.
裏づけを取るにはシミュレーション時に該当信号をモニタする必要があるわけだが,その手段についてもわからなかった(現在進行形).

とりあえず,検証モジュール(タスク)で,インスタンス名をドットでつないで内部信号をassignして観測しようとしています.が,なんだか調子悪いような・・・.DCMを使っているせいもあり,5mSec強の待ち時間を作りこんでしまった.当該回路を取り去ると挙動が変わる恐れもあり,実行していない.*1

Warningの意味

合成/シミュレーションデザイン ガイド

WARNING:Simulator:29 - at 6.000350773 ms: Warning:  Timing violation in
   /TST_BSC/uut/\TPU_1/TADD_buf_15 /  $setuphold<setup>( I:6.000350736 ms,
   CLK:6.000350773 ms,132.000 ps,48.000 ps)

clock skewによるwarningと判断する.SHからのWRアサート→ネゲートを検出して,データをラッチして,次のCLKでTPU moduleに書き込み要求を出す.(module向けのWRはネゲートされたまま.DFFの貫通が起きればその限りではない?が,次のCLKで書きにいくのでおk.bufferにしてあるから1clk間化けても大丈夫)



*1 : シミュレーション時間がかかりすぎるので,削るようにもしたほうがいいナ.

少し解決

シミュレーションに時間を要していた件,DCM出力が出てこないのは時間のせいと思い込んでいましたが,リセットがかかっていなかった模様.
リセット信号がnegedgeなのにt=0から0で100nSecおいて1にしてました.そりゃ非同期リセットかからない罠('A`.

次に,timing violation.下位モジュールで別クロックを作ってしまっていました.同じクロックと期待して表示させたのに0.7nSecもずれている.10nSec周期なので,7%誤差ですね.コレを削ればviolationも消えるかもしれませんし,そもそも非同期になっていては設計想定が崩れます.

DCMにするか,クロックバッファを通してやるのが無難でしょうかねぇ.DCMもったいないかな・・・?分周して同窓にあわせようとすれば,これしかないか.出力にDFFかましても位相ずれるには変わりないしねぇ.


ISE 10.1のmanual類

日本語版は,ここらへんに落ちてます.

http://japan.xilinx.com/support/software_manuals.htm

module

2008/08/20Verilog::文法import

module文(synthesizable/TestBench)

概要

階層化設計の一塊.入出力信号・パラメータを引数として、実体化する.
順序回路・組み合わせ回路の0個以上の組み合わせの回路ブロックを記述する.

書式

ANSI式*1を以下に示す.

module <module-name> (
   input foo,
   output bar,
   inout bus
  );
  // local signal/variable...
  begin
   // 式
  end
endmodule

それ以前の書式は以下のように記述する.

module <module-name> ;
   input foo,
   output bar,
   inout bus
// (略)
endmodule


*1 : ANSI-Cの関数記述相当.K&R時代は変数だけを並べて,型+変数名は後に記述した.Verilogでも、module名だけでセミコロンを打ち,信号名を並べてからbegin文を置く記述も可能.

moduleのパラメタ変更方法

パラメタの使い方を理解しておらず,つい最近に例を見かけて知ったので記載しておく.
defineとは違い、インスタンス化する際に値を変化させて利用することができる,と推測する.

  • defparam文
  • instance化する際に, 渡す.
     module-name #(
    		.paramete-name(value),
    		...
    	) instance-name (
    		.signal-name(symbol),
    		....
    	) ;
    

※インデントはxilinx webpack 10.1sp2の自動生成ツールの出力を真似た.