verilog chap 4

Upload: sai-sandeep

Post on 04-Apr-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Verilog Chap 4

    1/26

    J u n e 1 9 9 3 4 -1

    E x p r e s s i o n s

    4Figure 4-0Example 4-0

    Syntax 4-0Table 4-0

    E x p r e s s i o n sT h i s c h a p t e r d e s c r ib e s t h e o p e r a t o r s a n d o p e r a n d s a v a i la b le i n t h e

    Ve r ilo g H D L , a n d h o w t o u s e t h e m t o fo r m e x p r e s s i o n s .

    An e x p r e s s i o n is a c o n s t r u c t t h a t c o m b i n e s o p e r a n d s w it h o p e r a t o r s t o

    p r o d u c e a r e s u l t t h a t i s a f u n c t i o n o f t h e v a l u e s o f t h e o p e r a n d s a n d t h e

    s e m a n t i c m e a n in g o f t h e o p e r a t o r . Al t e r n a t i ve ly , a n e x p r e s s i o n is a n y

    l e ga l o p e r a n d f or e x a m p l e , a n e t b i t - s e l e c t . W h e r e v e r a v a l u e i s n e e d e d

    i n a V e r i lo g H D L s t a t e m e n t , a n e x p r e s s i o n c a n b e g iv e n . H o w e v e r , s e ve r a l

    s t a t e m e n t c o n s t r u c t s lim i t a n e x p r e s s io n t o a c o n s t a n t e x p r e s s i o n . A

    c o n s t a n t e x p r e s s i o n c o n s i s t s o f c o n s t a n t n u m b e r s a n d p r e d e fi n e d

    p a r a m e t e r n a m e s o n l y, b u t c a n u s e a n y o f t h e o p e r a t o r s d e fin e d in

    T a b l e 4 - 1 .

    F o r t h e i r u s e i n e x p r e s s i o n s , integer a n d time d a t a t yp e s s h a r e t h e

    s a m e t r a it s a s t h e d a t a t y p e reg. D e s c r ip t i o n s p e r t a i n i n g t o r e g is t e r

    u s a ge a p p l y t o i n t e g e r s a n d t im e s a s w e ll.

    An o p e r a n d c a n b e o n e o f t h e f o l lo w in g :

    n u m b er (in clu d in g real)

    n e t

    r e g is t e r , i n t e g e r , t i m e

    n e t b i t - s e l e c t

    r e g i s t e r b i t - s e l e c t

    n e t p a r t - s e le c t

    r e g i s t e r p a r t - s e l e c t

    m e m o r y e l em e n t

    a c a l l t o a u s e r - d e f i n e d f u n c t i o n o r s y s t e m d e fi n e d f u n c t i o n t h a t

    r e t u r n s a n y o f t h e a b o v e

  • 7/31/2019 Verilog Chap 4

    2/26

    4 -2 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    4 . 1O p e r a t o r s

    Th e s y m b o l s f o r t h e V e r il o g H D L o p e r a t o r s a r e s i m ila r t o t h o s e i n t h e C

    la n g u a g e . T a b le 4 - 1 l is t s t h e s e o p e r a t o r s .

    Table 4-1: Operators for Verilog language

    {} concatenation+ - * / arithmetic% modulus> >= < right shift

    ? : conditional

    Verilog Language Operators

  • 7/31/2019 Verilog Chap 4

    3/26

    J u n e 1 9 9 3 4 -3

    E x p r e s s i o n sOp e r a t o r s

    No t a ll o f t h e o p e r a t o r s l is t e d a b o v e a r e va l id w it h r e a l e xp r e s s io n s .

    T a b l e 4 - 2 i s a li s t o f t h e o p e r a t o r s t h a t a r e l eg a l w h e n a p p l ie d t o r e a l

    n u m b e r s .

    Table 4-2: Legal operators for use in real expressions

    T h e r e s u l t o f u s i n g l o g ic a l o r r e l a t i o n a l o p e r a t o r s o n r e a l n u m b e r s is a

    s i n gl e - b i t s c a l a r va l u e .

    T a b l e 4 - 3 l is t s o p e r a t o r s t h a t a r e n o t a l l o w e d t o o p e r a t e o n r e a l

    n u m b e r s .

    Table 4-3: Operators not allowed for real expressions

    S e e S e c t i on 3 . 1 0 . 3 f o r m o r e i n fo r m a t i on o n u s e o f r e a l n u m b e r s .

    Operators for Real Expressions

    unary + unary - unary operators

    + - * / arithmetic

    > >= < shift

  • 7/31/2019 Verilog Chap 4

    4/26

    4 -4 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    4 . 1 . 1B i n a r y O p e r a t o r P r e c e d e n c e

    Th e p r e c e d e n c e o r d e r o f b i n a r y o p e r a t o r s (a n d t h e t e r n a r y o p e r a t o r ?:)is t h e s a m e a s t h e p r e c e d e n c e o r d e r f o r t h e m a t c h i n g o p e r a t o r s i n t h e C

    la n g u a g e . V e r il o g h a s t w o e q u a l i t y o p e r a t o r s n o t p r e s e n t i n C ; t h e y a r e

    d is c u s s e d i n S e c t i o n 4 . 1 . 6 . T a b l e 4 - 4 s u m m a r i z e s t h e p r e c e d e n c e r u l e s

    for Verilogs b i n a r y a n d t e r n a r y o p e r a t o r s .

    Table 4-4: Precedence rules for operators

    O p e r a t o r s o n t h e s a m e l in e i n Ta b l e 4 - 4 h a v e t h e s a m e p r e c e d e n c e . R o w s

    a r e i n o r d e r o f d e c r e a s in g p r e c e d e n c e , s o , f o r e x a m p l e , *, / , a n d % a ll

    h a v e t h e s a m e p r e c e d e n c e , w h ic h i s h i gh e r t h a n t h a t o f t h e b i n a r y + a n d

    - o p e r a t o r s .

    Al l op e r a t o r s a s s o c i a t e le f t t o r i gh t . A s s o c i a t i vi t y re f e r s t o t h e o r d e r i n

    w h i c h a l a n g u a g e e v a l u a t e s o p e r a t o r s h a v i n g t h e s a m e p r e c e d e n c e .

    Th u s , in th e followin g ex a m p le B, is a d d e d t o A a n d t h e n C i s s u b t r a c t e d

    fr o m t h e r e s u l t o f A+B.

    A + B - C

    Operator Precedence Rules

    ! ~ highest precedence

    * / %

    + -

    >

    < < = > > =

    == != === !==

    &

    ^ ^~

    |

    &&

    ||

    ?: (ternary lowest precedence

    operator)

  • 7/31/2019 Verilog Chap 4

    5/26

    J u n e 1 9 9 3 4 -5

    E x p r e s s i o n sOp e r a t o r s

    W h e n o p e r a t o r s d i ff e r i n p r e c e d e n c e , t h e o p e r a t o r s w it h h i gh e r

    p r e c e d e n c e a p p l y f ir s t . I n t h e f o ll o w in g e x a m p le , B i s d i v i d e d b y C

    (d i vi s i o n h a s h i g h e r p r e c e d e n c e t h a n a d d i t i o n ) a n d t h e n t h e r e s u l t i s

    a d d e d t o A.

    A + B / C

    P a r e n t h e s e s c a n b e u s e d t o c h a n g e t h e o p e r a t o r p r e c e d e n c e .

    (A + B) / C // not the same as A + B / C

    4 . 1 . 2Nu m e r i c Co n v e n t i o n s i n E x p re s s i o n s

    O p e r a n d s c a n b e e x p r e s s e d a s b a s e d a n d s iz e d n u m b e r s w i t h t h e

    f o llo w i n g r e s t r i c t i o n : T h e Ve r i l o g l a n g u a g e i n t e r p r e t s a n u m b e r o f t h e

    f o r m sss f nnn, w h e n u s e d d i re c t ly i n a n e x p r e s s io n , a s t h e u n s i g n e d

    n u m b e r r e p r e s e n t e d b y t h e t w o s c o m p l e m e n t o f nnn. E x a m p le 4 - 1

    s h o w s t w o w a y s t o w r it e t h e e x p r e s s i o n m i n u s 1 2 d i v id e d b y 3 . No t e

    t h a t -12 a n d -d12 b o t h e v a l u a t e t o t h e s a m e b i t p a t t e r n , b u t i n a n

    e x p r e s s i o n -d12 l o s e s it s i d e n t i t y a s a s i gn e d , n e g a t i v e n u m b e r .

    Example 4-1: Number format in expressions

    4 . 1 . 3Ari t h m e t i c Op e ra t o rs

    T h e b i n a r y a r it h m e t i c o p e r a t o r s a r e t h e f o ll ow i n g :

    + - * / % (the modulus operator)

    integer IntA;

    IntA = -12 / 3;

    IntA = -d 12 / 3;

    The result is -4.

    The result is 1431655761.

  • 7/31/2019 Verilog Chap 4

    6/26

    4 -6 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    In t e g e r d i v is io n t r u n c a t e s a n y f r a c t i o n a l p a r t . T h e m o d u l u s

    o p e r a t o r fo r e x a m p le , y % z, g iv e s t h e r e m a i n d e r w h e n t h e f i r s t

    o p e r a n d i s d i vid e d b y t h e s e c o n d , a n d t h u s i s z e r o w h e n z d i v i d e s y

    e x a c t l y. T h e r e s u lt o f a m o d u l u s o p e r a t i o n t a k e s t h e s i g n o f t h e f ir s t

    o p e r a n d . T a b le 4 - 5 giv e s e x a m p l e s o f m o d u lu s o p e r a t i on s .

    Table 4-5: Examples of modulus operations

    Th e u n a r y a r it h m e t i c o p e r a t o r s t a k e p r e c e d e n c e o v e r t h e b i n a r y

    o p e r a t o r s . T h e u n a r y o p e r a t o r s a r e t h e f o l lo w in g :

    + -

    F o r t h e a r i t h m e t i c o p e r a t o r s , i f a n y o p e r a n d b i t v a l u e i s t h e u n k n o w n

    valu e x, t h e n t h e e n t i r e r e s u l t v a lu e is x.

    4 . 1 . 4

    Ari t h m e t i c E x p re s s i o n s w i t h R e g i s t e rs a n d In t e g e rs

    An a r i t h m e t i c o p e r a t i o n o n a r e g is t e r d a t a t y p e b e h a v e s d i f fe r e n t l y t h a n

    a n a r i t h m e t i c o p e r a t i o n o n a n in t e g e r d a t a t y p e . T h e V e r i lo g l a n g u a g e

    s e e s a r e g is t e r d a t a t y p e a s a n u n s i gn e d v a l u e a n d a n i n t e g e r t y p e a s a

    s i g n e d v a lu e . As a r e s u l t , w h e n y o u a s s i gn a v a l u e o f t h e fo r m

    - < s i ze > < b a s e _fo r m a t > < n u m b e r > t o a r e g i s t e r a n d t h e n u s e t h a t r e g i s t e r

    a s a n e x p r e s s i o n o p e r a n d , y o u a r e a c t u a l ly u s i n g a p o s i t i ve n u m b e r t h a t

    is t h e t wo s c o m p le m e n t o f nnn. I n c o n t r a s t , w h e n y o u a s s i gn a v a lu e o f

    10 % 3 1 10/3 yields a remainder of 1

    11 % 3 2 11/3 yields a remainder of 2

    12 % 3 0 12/3 yields no remainder

    -10 % 3 -1 the result takes the sign of thefirst operand

    11 % -3 2 the result takes the sign of thefirst operand

    -4d12 % 3 1 -4d12 is seen as a large, positivenumber that leaves a remainderof 1 when divided by 3

    Result CommentsModulus

    Expression

  • 7/31/2019 Verilog Chap 4

    7/26

    J u n e 1 9 9 3 4 -7

    E x p r e s s i o n sOp e r a t o r s

    t h e fo r m - < s i ze > < b a s e _ fo r m a t > < n u m b e r > t o a n i n t e g e r a n d t h e n u s e t h a t

    i n t e g e r a s a n e x p r e s s i o n o p e r a n d , t h e e x p r e s s i o n e v a l u a t e s u s i n g s i gn e d

    a r i t h m e t i c. E x a m p l e 4 - 2 s h o w s v a r io u s w a y s t o d i vi d e m in u s t w e lv e b y

    t h r e e u s i n g i n t e g e r a n d r e g is t e r d a t a t y p e s in e x p r e s s i o n s .

    Example 4-2: Modulus operation with registers and integers

    4 . 1 . 5R e l a t i o n a l Op e ra t o rs

    T a b le 4 - 2 l is t s a n d d e f in e s t h e r e l a t i o n a l o p e r a t o r s .

    Table 4-6: The relational operators defined

    integer intA;

    reg [15:0] regA;

    intA = -4d12;

    regA = intA / 3;

    regA = -4d12;

    intA = regA / 3;

    intA = -4d12 / 3;

    regA = -12 / 3;

    result is -4 becauseintA is an integerdata type

    result is 21841 becauseregA is a registerdata type

    result is 21841 because

    -4d12 is effectively aregister data type

    result is -4 because -12is effectively an integerdata type

    Relational Operators

    a < b a less than ba > b a greater than ba = b a greater than or equal to b

  • 7/31/2019 Verilog Chap 4

    8/26

    4 -8 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    Th e r e l a t i o n a l o p e r a t o r s i n T a b l e 4 - 2 a l l yi e ld t h e s c a l a r v a l u e 0 if t h e

    s p e c i fie d r e l a t i o n i s f a ls e , o r t h e v a l u e 1 i f t h e s p e c ifi e d r e l a t i o n i s t r u e .

    If , d u e t o u n k n o wn b i t s i n t h e o p e r a n d s , t h e r e l a t i o n i s a m b i g u o u s , t h e n

    t h e r e s u l t i s t h e u n k n o w n va l u e (x).

    Pl e a s e n o t e : If Verilog-XL tes ts a va lue th at is x or z, t h e n t h e r e s u lt

    o f t h a t t e s t i s False .

    All t h e r e l a t io n a l o p e r a t o r s h a v e t h e s a m e p r e c e d e n c e . R e l a t io n a l

    o p e r a t o r s h a v e l ow e r p r e c e d e n c e t h a n a r i t h m e t ic o p e r a t o r s . T h e

    fo l lo w in g e x a m p l e s i ll u s t r a t e t h e i m p l ic a t i o n s o f t h i s p r e c e d e n c e r u l e :

    a < size - 1 // this construct is the same as

    a < (size - 1) // t his c onstruct, b ut . . .

    size - (1 < a) // this construct is not the same

    size - 1 < a // as this construct

    No t e t h a t w h e n size - (1 < a) e va l u a t e s , t h e r e l a t i o n a l e x p r e s s i o n

    e v a l u a t e s fi r s t a n d t h e n e i t h e r z e r o o r o n e is s u b t r a c t e d f r o m size. Wh en

    s i z e - 1 < a e v a l u a t e s , t h e size o p e r a n d is r e d u c e d b y o n e a n d t h e n

    c o m p a r e d w i t h a.

    4 . 1 . 6

    E q u a l i t y O p e r a t o r sTh e e q u a l it y o p e r a t o r s r a n k j u s t l o w e r i n p r e c e d e n c e t h a n t h e r e l a t io n a l

    o p e r a t o r s . T a b l e 4 - 2 l is t s a n d d e f in e s t h e e q u a l it y o p e r a t o r s .

    Table 4-7: The equality operators defined

    Al l fo u r e q u a l i t y o p e r a t o r s h a v e t h e s a m e p r e c e d e n c e . T h e s e f o u r

    o p e r a t o r s c o m p a r e o p e r a n d s b i t f o r b i t , w i t h z e r o f il lin g i f t h e t w o

    o p e r a n d s a r e o f u n e q u a l b i t - le n g t h . As w i t h t h e r e l a t i o n a l o p e r a t o r s , t h e

    r e s u l t i s 0 if f a l s e , 1 if t r u e .

    Equality Operators

    a ===b a equal to b, including x and za !==b a not equal to b, including x and za ==b a equal to b, result may be unknown

    a != b a not equal to b, result may be unknown

  • 7/31/2019 Verilog Chap 4

    9/26

    J u n e 1 9 9 3 4 -9

    E x p r e s s i o n sOp e r a t o r s

    F o r t h e == a n d != o p e r a t o r s , i f ei t h e r o p e r a n d c o n t a i n s a n x o r a z, t h e n

    t h e r e s u l t i s t h e u n k n o w n va l u e (x).

    F o r t h e === a n d !== o p e r a t o r s , t h e c o m p a r i s o n i s d o n e ju s t a s i t is i n

    t h e p r o c e d u r a l c a s e s t a t e m e n t . B it s t h a t a r e x o r z a r e i n c lu d e d in t h ec o m p a r i s o n a n d m u s t m a t c h fo r t h e r e s u lt t o b e t r u e . Th e r e s u l t o f t h e s e

    o p e r a t o r s i s a l w a y s a k n o w n v a l u e , e i th e r 1 o r 0.

    4 . 1 . 7Lo g i c a l Op e ra t o rs

    T h e o p e r a t o r s l o gic a l AN D (&&) a n d l o gi c a l O R (| | ) a r e l o gic a l

    c o n n e c t i ve s . E x p r e s s i o n s c o n n e c t e d b y && o r || a r e e v a lu a t e d l e ft t o

    r i gh t , a n d e v a l u a t i o n s t o p s a s s o o n a s t h e t r u t h o r fa ls e h o o d o f t h e r e s u lt

    i s k n o w n . T h e r e s u lt o f t h e e v a l u a t i o n o f a l og i c a l c o m p a r i s o n i s o n e

    (d efin ed as true ), z e r o (d e f i n e d a s f a l s e ), o r , i f t h e r e s u l t i s a m b i g u o u s ,

    t h e n t h e r e s u lt i s t h e u n k n o w n v a l u e (x) . F o r e x a m p l e , i f r e g i s t e r alphah o ld s t h e in t e g e r v a l u e 2 3 7 a n d beta h o l d s t h e v a l u e z e r o , t h e n t h e

    fo l lo w in g e x a m p l e s p e r f o r m a s d e s c r ib e d :

    regA = alpha && beta; // regA is set to 0

    regB = alpha || beta; // regB is set to 1

    T h e p r e c e d e n c e o f && i s g r e a t e r t h a n t h a t o f ||, a n d b o t h a r e l ow e r t h a n

    r e l a t i o n a l a n d e q u a l it y o p e r a t o r s . Th e f o ll o w in g e x p r e s s i o n AN D s t h r e e

    s u b - e x p r e s s i o n s w it h o u t n e e d i n g a n y p a r e n t h e s e s :

    a < size-1 && b != c && index != lastone

    H o w e v e r , i t is r e c o m m e n d e d f o r r e a d a b i l it y p u r p o s e s t h a t p a r e n t h e s e s b e

    u s e d t o s h o w v e r y c l e a r ly t h e p r e c e d e n c e i n t e n d e d , a s i n t h e f ol lo w i n g

    r e w r it e o f t h e a b o v e e x a m p le :

    (a < size-1) && (b != c) && (index != lastone)

    A t h i r d l o gi c a l o p e r a t o r i s t h e u n a r y l og ic a l n e g a t i on o p e r a t o r !. Th en e g a t io n o p e r a t o r c o n v e r t s a n o n - z e ro o r t r u e o p e r a n d in t o 0 a n d a z e r o

    o r f a l s e o p e r a n d i n t o 1. An a m b i g u o u s t r u t h v a lu e r e m a i n s a s x. Ac o m m o n u s e o f ! i s i n c o n s t r u c t i o n s l i k e t h e f o l lo wi n g :

    if (!inword)

  • 7/31/2019 Verilog Chap 4

    10/26

    4 -1 0 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    In s o m e c a s e s , t h e p r e c e d in g c o n s t r u c t m a k e s m o r e s e n s e t o s o m e o n e

    r e a d i n g t h e c o d e t h a n t h e e q u i va l e n t c o n s t r u c t s h o w n b e l o w :

    if (inword == 0)

    C o n s t r u c t i o n s l ik e if (!inword) r e a d q u i t e n i c e l y (if n ot in wor d ), b u tm o r e c o m p l ic a t e d o n e s c a n b e h a r d t o u n d e r s t a n d . T h e fi r s t f o rm is

    s l ig h t l y m o r e e f fic i e n t i n s i m u l a t i o n s p e e d t h a n t h e s e c o n d .

  • 7/31/2019 Verilog Chap 4

    11/26

    J u n e 1 9 9 3 4 -1 1

    E x p r e s s i o n sOp e r a t o r s

    4 . 1 . 8B i t - W i s e O p e r a t o r s

    T h e b i t o p e r a t o r s p e r f o r m b i t - w is e m a n i p u l a t i o n s o n t h e o p e r a n d s t h a ti s , t h e o p e r a t o r c o m p a r e s a b i t i n o n e o p e r a n d t o i t s e q u i va l e n t b i t in t h e

    o t h e r o p e r a n d t o c a l c u la t e o n e b i t fo r t h e r e s u l t . T h e l o g ic t a b l e s i n

    T a b le 4 - 8 s h o w t h e r e s u l ts f o r e a c h p o s s i b le c a l c u l a t i o n .

    Table 4-8: Bit-wise operators logic tables

    ~

    0 1

    1 0

    x x

    & 0 1 x

    0 0 0 0

    1 0 1 x

    x 0 x x

    | 0 1 x

    0 0 1 x

    1 1 1 1

    x x 1 x

    bit-wise unary negation bit-wise binary AND operator

    bit-wise binary inclusive

    OR operator

    ^ 0 1 x

    0 0 1 x

    1 1 0 x

    x x x x

    bit-wise binary exclusive

    OR operator

    ^~ 0 1 x

    0 1 0 x

    1 0 1 x

    x x x x

    bit-wise binary exclusive

    NOR operator

  • 7/31/2019 Verilog Chap 4

    12/26

    4 -1 2 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    C a r e s h o u l d b e t a k e n t o d i s t i n gu i s h t h e b i t - w is e o p e r a t o r s & a n d | from

    t h e l o g ic a l o p e r a t o r s && a n d ||. F o r e x a m p l e , i f x is 1 a n d y is 2, t h e n

    x & y is 0, wh ile x && y is 1. W h e n t h e o p e r a n d s a r e o f u n e q u a l b i t

    le n g t h , t h e s h o r t e r o p e r a n d i s z e r o - fi ll e d i n t h e m o s t s i g n i fi ca n t b i t

    p o s i t io n s .

    4 . 1 . 9R e d u c t i o n O p e r a t o r s

    Th e u n a r y r e d u c t io n o p e r a t o r s p e r f or m a b i t -w is e o p e r a t io n o n a s i n g le

    o p e r a n d t o p r o d u c e a s i n g le b i t r e s u l t . T h e f ir s t s t e p o f t h e o p e r a t i o n

    a p p l ie s t h e o p e r a t o r b e t w e e n t h e f ir s t b i t o f t h e o p e r a n d a n d t h e

    s e c o n d u s i n g t h e l o g i c t a b l e s i n T a b l e 4 - 9 . T h e s e c o n d a n d s u b s e q u e n t

    s t e p s a p p ly t h e o p e r a t o r b e t w e e n t h e o n e - b it r e s u lt o f t h e p r i o r s t e p a n d

    t h e n e x t b i t o f t h e o p e r a n d s t i ll u s i n g t h e s a m e l o gi c t a b l e .

    Table 4-9: Reduction operators logic tables

    & 0 1 x

    0 0 0 0

    1 0 1 x

    x 0 x x

    reduction unary

    AND operator

    | 0 1 x

    0 0 1 x

    1 1 1 1

    x x 1 x

    reduction unary inclusive

    OR operator

    ^ 0 1 x

    0 0 1 x

    1 1 0 x

    x x x x

    reduction unary exclusive

    OR operator

  • 7/31/2019 Verilog Chap 4

    13/26

  • 7/31/2019 Verilog Chap 4

    14/26

    4 -1 4 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    4 . 1 . 1 0S y n t a x R e s t r i c t i o n s

    Th e Ve r i l og l a n g u a g e i m p o s e s t wo s y n t a x r e s t r i c t io n s i n t e n d e d t o p r o t e c td e s c r i p t io n fi le s f r o m a t y p o g r a p h i c a l e r r o r t h a t i s p a r t i c u la r l y h a r d t o

    fi n d . T h e e r r o r c o n s i s t s o f t r a n s p o s in g a s p a c e a n d a s y m b o l. No t e t h a t

    t h e c o n s t r u c t s o n l in e 1 b e l o w d o n ot r e p r e s e n t t h e s a m e s y n t a x a s t h e

    s i m il a r c o n s t r u c t s o n li n e 2 .

    1 . a & &b a | |b

    2 . a && b a || b

    In o r d e r t o p r o t e c t u s e r s fr o m t h i s t y p e o f e r r o r , V e r i lo g r e q u ir e s t h e u s e

    o f p a r e n t h e s e s t o s e p a r a t e a r e d u c t io n or or and o p e r a t o r fr o m a b i t - wis e

    or o r and o p e r a t o r . T a b l e 4 - 1 2 s h o w s t h e s y n t a x t h a t r e q u ir e s

    p a r e n t h e s e s :

    Table 4-12: Syntax equivalents for syntax restriction

    a & &b a & (&b)

    a | |b a | (|b)

    Invalid Equivalent

    Syntax Syntax

  • 7/31/2019 Verilog Chap 4

    15/26

    J u n e 1 9 9 3 4 -1 5

    E x p r e s s i o n sOp e r a t o r s

    4 . 1 . 1 1S h i f t O p e r a t o r s

    T h e s h i ft o p e r a t o r s , >, p e r f o r m l e ft a n d r i g h t s h i ft s o f t h e i r l e f to p e r a n d b y t h e n u m b e r o f b i t p o s i t i o n s g i ve n b y t h e r i g h t o p e r a n d . B o t h

    s h i ft o p e r a t o r s f ill t h e v a c a t e d b i t p o s i t i on s w it h z e r o e s . E x a m p l e 4 - 3

    il lu s t r a t e s t h i s c o n c e p t .

    Example 4-3: Use of shift operator

    I n t h i s e x a m p l e , t h e r e g i s t e r result i s a s s i gn e d t h e b i n a r y v a l u e 0100,

    wh ich is 0001 s h i ft e d t o t h e l e f t t wo p o s i t i o n s a n d z e r o f il le d .

    4 . 1 . 1 2

    C o n d i t i o n a l O p e r a t o r

    T h e c o n d i t io n a l o p e r a t o r h a s t h r e e o p e r a n d s s e p a r a t e d b y t w o o p e r a t o r s

    i n t h e f ol lo w i n g f o r m a t :

    cond_expr ? true_expr : false_expr

    If cond_expr e v a l u a t e s t o f a l s e , t h e n false_expr i s e v a l u a t e d a n d u s e d

    a s t h e r e s u l t . If t h e c o n d i t i o n a l e x p r e s s i o n i s t r u e , t h e n true_expr is

    e v a l u a t e d a n d u s e d a s t h e r e s u l t. I f cond_expr i s a m b i g u o u s , t h e n b o t h

    true_expr a n d false_expr a r e e v a l u a t e d a n d t h e i r r e s u l t s a r e

    c o m p a r e d , b it b y b i t , u s in g T a b l e 4 - 1 3 t o c a l c u la t e t h e f in a l r e s u l t . I f t h el e n g t h s o f t h e o p e r a n d s a r e d i ff e r e n t , t h e s h o r t e r o p e r a n d i s l e n g t h e n e d

    t o m a t c h t h e l o n g e r a n d z e r o f il le d f r o m t h e l e f t (t h e h i gh - o r d e r e n d ) .

    module shift;

    reg [3:0] start, result;

    initial

    begin

    start = 1;

    result = (start

  • 7/31/2019 Verilog Chap 4

    16/26

    4 -1 6 J u n e 1 9 9 3

    E x p r e s s i o n sOp e r a t o r s

    Table 4-13: Conditional operator ambiguous condition results

    Th e f o ll o w in g e x a m p l e o f a t r i - s t a t e o u t p u t b u s i ll u s t r a t e s a c o m m o n u s e

    o f t h e c o n d i t i o n a l o p e r a t o r .

    wire [15:0] busa = drive_busa ? data : 16bz;

    Th e b u s c a l le d data i s d r i ve n o n t o busa wh en drive_busa is 1. If

    drive_busa i s u n k n o wn , t h e n a n u n k n o wn v a l u e i s d r i ve n o n t o busa.

    O t h e r w is e , busa i s n o t d r i ve n .

    4 . 1 . 1 3

    C o n c a t e n a t i o n s

    A c o n c a t e n a t i o n i s t h e jo i n i n g t o g e t h e r o f b i t s r e s u l t i n g fr o m t wo o r m o r e

    e x p r e s s i o n s . Th e c o n c a t e n a t i o n i s e x p r e s s e d u s i n g t h e b r a c e c h a r a c t e r s{ a n d }, w it h c o m m a s s e p a r a t in g t h e e x p r e s s i o n s w it h i n . T h e n e x t

    e x a m p l e c o n c a t e n a t e s f o u r e x p r e s s i o n s :

    {a, b[3:0], w, 3b101}

    Th e p r e v i o u s e x a m p l e is e q u iv a l e n t t o t h e f o l l owi n g e x a m p l e :

    {a, b[3], b[2], b[1], b[0], w, 1b1, 1b0, 1b1}

    U n s i ze d c o n s t a n t n u m b e r s a r e n o t a l l ow e d in c o n c a t e n a t io n s . T h i s isb e c a u s e t h e s i ze o f e a c h o p e r a n d i n t h e c o n c a t e n a t i o n i s n e e d e d t o

    c a l c u l a t e t h e c o m p l e t e s iz e o f t h e c o n c a t e n a t i on .

    C o n c a t e n a t i o n s c a n b e e x p r e s s e d u s i n g a r e p e t i t io n m u l t ip l ie r a s s h o w n

    in t h e n e x t e x a m p l e .

    {4{w}} // This is equivalent to {w, w, w, w}

    ?: 0 1 x z

    0 0 x x x

    1 x 1 x x

    x x x x x

    z x x x x

  • 7/31/2019 Verilog Chap 4

    17/26

    J u n e 1 9 9 3 4 -1 7

    E x p r e s s i o n sO p e r a n d s

    T h e n e x t e x a m p l e il lu s t r a t e s n e s t e d c o n c a t e n a t i o n s .

    {b, {3{a, b}}} // This is equivalent to

    // {b, a, b, a, b, a, b}

    T h e r e p e t i t io n m u l t ip l ie r m u s t b e a c o n s t a n t e x p r e s s i o n .

    4 . 2O p e r a n d s

    As s t a t e d b e fo r e , t h e r e a r e s e ve r a l t y p e s o f op e r a n d s t h a t c a n b e

    s p e c ifi e d i n e x p r e s s i o n s . T h e s im p l e s t t y p e i s a r e f er e n c e t o a n e t o r

    r e g i s t e r i n i t s c o m p l e t e f o r m t h a t i s , j u s t t h e n a m e o f t h e n e t o r r e g is t e r

    i s g i ve n . I n t h i s c a s e , a l l o f t h e b i t s m a k i n g u p t h e n e t o r r e g i s t e r va l u ea r e u s e d a s t h e o p e r a n d .

    I f j u s t a s i n g le b i t o f a v e c t o r n e t o r r e g i s t e r i s r e q u i r e d , t h e n a b i t - s e l e c t

    o p e r a n d is u s e d . A p a r t - s e l e c t o p e r a n d i s u s e d t o r e fe r e n c e a g r o u p o f

    a d j a c e n t b i ts i n a v ec t o r n e t o r r e g is t e r .

    A m e m o r y e l e m e n t c a n b e r e f e r e n c e d a s a n o p e r a n d .

    A c o n c a t e n a t i o n o f o t h e r o p e r a n d s (i n c l u d i n g n e s t e d c o n c a t e n a t i o n s ) c a n

    b e s p e c ifie d a s a n o p e r a n d .

    A fu n c t i o n c a l l is a n o p e r a n d .

    4 . 2 . 1Ne t a n d R e g i s t e r B i t A dd re s s i n g

    B it - s e l e c t s e x t r a c t a p a r t i c u la r b i t f r o m a v ec t o r n e t o r r e g is t e r . T h e b i t

    c a n b e a d d r e s s e d u s i n g a n e x p r e s s i o n . T h e n e x t e x a m p le s p e c i fi es t h e

    s i n g le b i t o f acc t h a t is a d d r e s s e d b y t h e o p e r a n d index.

    acc[index]

    Th e a c t u a l b it t h a t is a c c e s s e d b y a n a d d r e s s i s , in p a r t , d e t e r m i n e d b y

    t h e d e c la r a t i o n o f acc. F o r i n s t a n c e , e a c h o f t h e d e c la r a t io n s o f acc

    s h o w n in t h e n e x t e x a m p l e c a u s e s a p a r t i c u l a r v a l u e o f index t o a c c e s s

    a d i f f e r e n t b i t :

    reg [15:0] acc;

    reg [1:16] acc;

    I f t h e b i t s e l e c t i s o u t o f t h e a d d r e s s b o u n d s o r i s x, t h e n t h e v a l u e

    r e t u r n e d b y t h e r e f e r e n c e i s x.

  • 7/31/2019 Verilog Chap 4

    18/26

    4 -1 8 J u n e 1 9 9 3

    E x p r e s s i o n sO p e r a n d s

    S e v e r a l c o n t i gu o u s b i t s i n a v e c t o r r e g i s t e r o r n e t c a n b e a d d r e s s e d , a n d

    a r e k n o wn a s p a r t - s e l e c t s . A p a r t - s e l e c t o f a v e c t o r r e g is t e r o r n e t i s

    g iv e n wi t h t h e f o l lo wi n g s y n t a x :

    vect[ms_expr:ls_expr]

    B o t h e x p r e s s i o n s m u s t b e c o n s t a n t e x p r e s s i o n s . T h e fi r s t e x p r e s s i o n

    m u s t a d d r e s s a m o r e s i gn if ic a n t b i t t h a n t h e s e c o n d e x p r e s s i o n .

    C o m p i le r e r r o r s r e s u lt i f e it h e r o f t h e s e r u l e s i s b r o k e n .

    Th e n e x t e x a m p l e a n d t h e b u l le t i t e m s t h a t f o llo w i t i ll u s t r a t e t h e

    p r i n c i p le s o f b i t a d d r e s s in g . T h e c o d e d e c l a r e s a n 8 - b i t r e g is t e r c a l le d

    vect a n d i n i t ia l iz e s i t t o a v a l u e o f 4 . T h e b u l l e t i t e m s d e s c r i b e h o w t h e

    s e p a r a t e b i t s o f t h a t ve c t o r c a n b e a d d r e s s e d .

    reg [7:0] vect;

    vect = 4;

    i f t h e v a l u e o f addr i s 2 , t h e n vect[addr] r e t u r n s 1

    i f t h e v a l u e o f addr i s o u t o f b o u n d s , t h e n vect[addr] r e t u r n s x

    if addr is 0 , 1 , o r 3 t h r o u g h 7 , vect[addr] r e t u r n s 0

    vect[3:0] r e t u r n s t h e b i t s 0100

    vect[5:1] r e t u r n s t h e b i t s 00010

    vect[< e x p r e s s i o n t h a t r e t u r n s x>] r e t u r n s x

    vect[< e x p r e s s i o n t h a t r e t u r n s z>] r e t u r n s x

    i f a n y b i t o f addr is x/z, t h e n t h e v a lu e o f addr is x

    4 . 2 . 2M e m o r y A d d r e s s i n g

    S e c t i o n 3 . 8 d i s c u s s e d t h e d e c l a r a t i o n o f m e m o r i e s . T h is s e c t io n

    d is c u s s e s m e m o r y a d d r e s s in g . Th e n e x t e x a m p l e d e c l a r e s a m e m o r y o f

    1 0 2 4 8 - b i t w o r d s :

    reg [7:0] mem_name[0:1023];

    Th e s y n t a x fo r a m e m o r y a d d r e s s c o n s i s t s o f t h e n a m e o f t h e m e m o r y

    a n d a n e xp r e s s i on fo r t h e a d d r e s s s p e c i fi e d wi t h t h e f o l l owi n g f or m a t :

    mem_name[addr_expr]

  • 7/31/2019 Verilog Chap 4

    19/26

    J u n e 1 9 9 3 4 -1 9

    E x p r e s s i o n sO p e r a n d s

    Th e addr_expr c a n b e a n y e x p r e s s i o n ; t h e r e f o r e , m e m o r y i n d i r e c t i o n s

    c a n b e s p e c i fi e d i n a s i n g le e x p r e s s i o n . Th e n e x t e x a m p l e i ll u s t r a t e s

    m e m o r y in d i r e c t io n :

    mem_name[mem_name[3]]

    I n t h e a b o v e e x a m p le , mem_name[3]a d d r e s s e s w o r d t h r e e o f t h e m e m o r y

    c a l l e d mem_name. T h e v a l u e a t w o r d t h r e e i s t h e i n d e x i n t o mem_name

    t h a t is u s e d b y t h e m e m o r y a d d r e s s mem_name[mem_name[3]]. As wi t h

    b i t - s e le c t s , t h e a d d r e s s b o u n d s g iv e n i n t h e d e c l a r a t i o n o f th e m e m o r y

    d e t e r m i n e t h e e f fe c t o f t h e a d d r e s s e x p r e s s io n . I f t h e i n d e x i s o u t o f t h e

    a d d r e s s b o u n d s o r is x, t h e n t h e v a l u e o f t h e r e f e r e n c e i s x.

    T h e r e i s n o m e c h a n is m t o e x p r e s s b it - s e l e c t s o r p a r t - s e l ec t s o f m e m o r y

    e l e m e n t s d i r e c t l y . I f t h i s i s r e q u ir e d , t h e n t h e m e m o r y e l e m e n t h a s t o b e

    f i r s t t r a n s f e r r e d t o a n a p p r o p r i a t e l y s i z e d t e m p o r a r y r e g i s t e r .

    4 . 2 . 3S t r i n g s

    S t r in g o p e r a n d s a r e t r e a t e d a s c o n s t a n t n u m b e r s c o n s i s t i n g o f a

    s e q u e n c e o f 8 - b i t A SC II c o d e s , o n e p e r c h a r a c t e r , w it h n o s p e c i a l

    t e r m i n a t i on c h a r a c t e r .

    An y Ve r i lo g H D L o p e r a t o r c a n m a n i p u l a t e s t r i n g o p e r a n d s . T h e o p e r a t o r

    b e h a v e s a s t h o u g h t h e e n t i r e s t r i n g w e r e a s i n g le n u m e r i c v a l u e .

    E x a m p l e 4 - 4 d e c l a r e s a s t r i n g va r i a b l e l a r g e e n o u g h t o h o ld 1 4

    c h a r a c t e r s a n d a s s i g n s a v a l u e t o it . T h e e x a m p l e t h e n m a n i p u la t e s t h es t r in g u s i n g t h e c o n c a t e n a t i o n o p e r a t o r .

    N o t e t h a t w h e n a v a r i a b l e i s l a r ge r t h a n r e q u i r e d t o h o ld t h e v a lu e b e i n g

    a s s i gn e d , t h e c o n t e n t s a ft e r t h e a s s i gn m e n t a r e p a d d e d o n t h e l e ft w i t h

    z e r o s . T h is i s c o n s i s t e n t w i t h t h e p a d d in g t h a t o c c u r s d u r i n g a s s i gn m e n t

    o f n o n - s t r i n g v a l u e s .

  • 7/31/2019 Verilog Chap 4

    20/26

    4 -2 0 J u n e 1 9 9 3

    E x p r e s s i o n sO p e r a n d s

    Example 4-4: Concatenation of strings

    Th e r e s u l t o f r u n n i n g V e r il o g o n t h e a b o v e d e s c r i p t i o n i s :

    4 . 2 . 4S t r i n g Op e ra t i o n s

    Th e c o m m o n s t r i n g o p e r a t i o n s copy , c o n c a t e n a t e , a n d c o m p a r e a r e

    s u p p o r t e d b y Ve r i lo g o p e r a t o r s . C o p y i s p r o v id e d b y s i m p l e a s s i gn m e n t .

    C o n c a t e n a t i o n i s p r o v id e d b y t h e c o n c a t e n a t io n o p e r a t o r . C o m p a r i s o n i s

    p r o v id e d b y t h e e q u a l it y o p e r a t o r s . E x a m p le 4 - 4 a n d E x a m p l e 4 - 5

    il lu s t r a t e a s s i g n m e n t , c o n c a t e n a t i o n , a n d c o m p a r i s o n o f s t r i n g s .

    Wh e n m a n i p u la t i n g s t r i n g v a l u e s in v ec t o r v a r ia b l e s , a t l e a s t 8*n b i t s

    a r e r e q u i r e d i n t h e v e c t o r , w h e r e n is t h e n u m b e r o f c h a r a c t e r s i n t h e

    s t r i n g.

    4 . 2 . 5S t ri n g Va lu e Pa d d i n g a n d P o t e n t i a l P ro b le m s

    Wh e n s t r in g s a r e a s s i gn e d t o v a r ia b le s , t h e v a l u e s s t o r e d a r e p a d d e d o n

    t h e l e f t w it h ze r o s . P a d d i n g c a n a f fe c t t h e r e s u l t s o f c o m p a r i s o n a n d

    c o n c a t e n a t i o n o p e r a t i o n s . T h e c o m p a r i s o n a n d c o n c a t e n a t i o n o p e r a t o r s

    d o n o t d i s t i n g u i s h b e t w e e n z e r o s r e s u l t i n g fr o m p a d d in g a n d t h e o r i gi n a l

    s t r i n g c h a r a c t e r s .

    module string_test;

    reg [8*14:1] stringvar;

    initial

    begin

    stringvar = Hello world;

    $display(%s is stored as %h,

    stringvar,stringvar);

    stringvar = {stringvar,!!!};

    $display(%s is stored as %h,

    stringvar,stringvar);

    end

    endmodule

    Hello world is stored as 00000048656c6c6f20776f726c64

    Hello world!!! is stored as 48656c6c6f20776f726c64212121

  • 7/31/2019 Verilog Chap 4

    21/26

    J u n e 1 9 9 3 4 -2 1

    E x p r e s s i o n sO p e r a n d s

    E x a m p l e 4 - 5 i llu s t r a t e s t h e p o t e n t ia l p r o b l e m .

    Example 4-5: Comparing string variables

    T h e c o m p a r is o n in t h e e x a m p l e a b o v e fa i ls b e c a u s e d u r i n g t h e

    a s s i gn m e n t t h e s t r in g v a r ia b l es ge t p a d d e d a s i ll u s t r a t e d in t h e n e x t

    e x a m p l e :

    s1 = 000000000048656c6c6f

    s2 = 00000020776f726c6421

    T h e c o n c a t e n a t i o n o f s1 a n d s2 i n c l u d e s t h e z e r o p a d d i n g , r e s u l t in g i n

    th e fo llowin g valu e:

    000000000048656c6c6f00000020776f726c6421

    S i n c e t h e s t r in g He l l o wo r l d c o n t a i n s n o z e r o p a d d i n g , t h e c o m p a r i s o n

    f a i ls , a s s h o w n b e l o w :

    T h e a b o v e c o m p a r i s o n y ie l d s a r e s u lt o f ze r o , w h i c h i s e q u iv a l e n t t o f a l s e .

    reg [8*10:1] s1, s2;

    initial

    begin

    s1 = Hello;

    s2 = world!;

    if ({s1,s2} == Hello world!)

    $display(strings are equal);

    end

    s1 s2 Hello world!

    000000000048656c6c6f00000020776f726c6421 == 48656c6c6f20776f726c6421

    Hello world!

  • 7/31/2019 Verilog Chap 4

    22/26

  • 7/31/2019 Verilog Chap 4

    23/26

    J u n e 1 9 9 3 4 -2 3

    E x p r e s s i o n sExpression Bit Lengths

    T h e c o m m a n d - l i n e o p t i o n +mindelays se le c t s t h e m i n i m u m e x p r e s s i o n

    i n a l l e x p r e s s i o n s w h e r e min:typ:max v a l u e s h a v e b e e n s p e c ifi e d .

    L i k e w i s e , +typdelays s e l e c t s a l l t h e t y p i c a l e x p r e s s i o n s a n d

    +maxdelays s e l e c t s a l l t h e m a x i m u m e x p r e s s i o n s . Ve r il og -XL d e fa u lt s t o

    t h e s e c o n d v a l u e wh e n a t wo o r t h r e e - p a r t d e l a y e x p r e s s i on is s p e c i fie d .

    V a l u e s e x p r e s s e d i n min:typ:max fo r m a t c a n b e u s e d i n e x p r e s s i o n s .

    T h e n e x t e x a m p l e s h o w s a n e x p r e s s i o n t h a t d e f in e s a s i n g le t r i p l et o f

    d e l a y v a l u e s . T h e m i n i m u m v a l u e i s t h e s u m o f a+d; t h e t y p i c a l v a l u e is

    b+e; t h e m a x i m u m v a l u e i s c+f, a s f o l l o w s :

    a:b:c + d:e:f

    T h e n e x t e x a m p l e s h o w s s o m e t y p ic a l e x p r e s s io n s t h a t a r e u s e d t o

    s p e c i f y min:typ:max fo r m a t va l u e s :

    val - 32d 50: 32d 75: 32d 100

    Th e min:typ:max fo r m a t c a n b e u s e d w h e r e v e r e x p r e s s i o n s c a n a p p e a r ,

    b o t h i n s o u r c e t e x t fi le s a n d i n i n t e r a c t iv e c o m m a n d s .

    4 . 4E x p r e s s i o n B i t L e n g t h s

    C o n t r o l li n g t h e n u m b e r o f b i t s t h a t a r e u s e d i n e x p r e s s i o n e va l u a t i o n s

    is im p o r t a n t if c o n s i s t e n t r e s u lt s a r e t o b e a c h i e ve d . S o m e s it u a t i on sh a v e a s i m p l e s o l u t io n , f o r e x a m p l e , if a b i t - w is e AN D o p e r a t i o n i s

    s p e c ifi e d o n t w o 1 6 - b i t r e g is t e r s , t h e n t h e r e s u l t i s a 1 6 - b it v a l u e .

    H o w e v e r , i n s o m e s i t u a t i o n s i t i s n o t o b v io u s h o w m a n y b i t s a r e u s e d t o

    e v a l u a t e a n e x p r e s s i o n , w h a t s iz e t h e r e s u l t s h o u l d b e , o r w h e t h e r

    s i g n e d o r u n s ig n e d a r i t h m e t i c s h o u l d b e u s e d .

    F o r e x a m p l e , w h e n is it n e c e s s a r y t o p e r f o r m t h e a d d i t io n o f t w o 1 6 - b it

    r e g is t e r s i n 1 7 b i t s t o h a n d l e a p o s s i b l e c a r r y o v e r fl o w ? Th e a n s w e r

    d e p e n d s o n t h e c o n t e x t i n w h ic h t h e a d d i ti o n t a k e s p l a c e . I f t h e 1 6 - b i t

    a d d i t io n i s m o d e l in g a r e a l 1 6 - b i t a d d e r t h a t l o s e s o r d o e s n o t c a r e a b o u t

    t h e c a r r y o v e r fl o w , t h e n t h e m o d e l m u s t p e r fo r m t h e a d d i t i o n i n 1 6 b i t s .

    I f t h e a d d i t io n o f t wo 1 6 - b i t u n s ig n e d n u m b e r s c a n r e s u l t i n a s ig n i fi c a n t

    1 7 t h b i t , t h e n a s s i gn t h e a n s w e r t o a 1 7 - b it r e g is t e r .

  • 7/31/2019 Verilog Chap 4

    24/26

    4 -2 4 J u n e 1 9 9 3

    E x p r e s s i o n sExpression Bit Lengths

    4 . 4 . 1A n E x a m p l e o f a n E x p r e s s i o n B i t L e n g t h P r o b l e m

    D u r i n g t h e e v a l u a t i o n o f a n e x p r e s s i o n , i n t e r i m r e s u lt s t a k e t h e s i z e o f t h e l a r g e s t o p e r a n d (i n t h e c a s e o f a n a s s i gn m e n t , t h is a l s o i n c l u d e s t h e

    le ft - h a n d s i d e ). Yo u m u s t t h e r e f o r e t a k e c a r e t o p r e v e n t lo s s o f a

    s i g n i fic a n t b i t d u r i n g e x p r e s s i o n e v a l u a t i on . T h i s s e c t io n d e s c r i b e s a n

    e x a m p l e o f t h e p r o b l e m s t h a t c a n o c c u r .

    Th e e x p r e s s i o n (a + b >> 1) y ie ld s a 1 6 - b it r e s u l t , b u t c a n n o t b e

    a s s i g n e d t o a 1 6 - b i t r e g is t e r w i t h o u t t h e p o t e n t i a l lo s s o f t h e h i gh - o r d e r

    b i t . I f a a n d b a r e 1 6 - b it r e g is t e r s , t h e n t h e r e s u lt o f (a+b) is 1 6 b i t s

    wi d e u n l e s s t h e r e s u lt is a s s i gn e d t o a r e g is t e r w i d e r t h a n 1 6 b i t s . If

    answer is a 1 7 - b i t r e g i s t e r , t h e n (answer = a + b) y i el d s a f u l l 1 7 - b i t

    r e s u l t . B u t i n t h e e x p r e s s i o n (a + b >> 1), t h e s u m of (a + b)

    p r o d u c e s a n in t e r i m r e s u l t t h a t i s o n l y 1 6 b i t s w id e . Th e r e f o r e , t h e

    a s s i g n m e n t o f (a + b >> 1) t o a 1 6 - b it r e g is t e r l o s e s t h e c a r r y b i t b e f o r e

    t h e e v a l u a t i o n p e r f o r m s t h e o n e - b i t r ig h t s h i ft .

    Th e r e a r e t w o s o l u t i o n s t o a p r o b l em o f t h i s t y p e . O n e is t o a s s i gn t h e

    s u m o f (a+b) t o a 1 7 - b i t r e g is t e r b e f o r e p e r f o r m i n g t h e s h i ft a n d t h e n

    s h i ft t h e 1 7 - b i t a n s w e r in t o t h e 1 6 - b i t s t h a t y o u r m o d e l r e q u i r e s . An

    e a s i e r s o lu t i o n i s t o u s e t h e f ol lo w i n g t r i c k .

    Th e p r o b l e m :

    E v a l u a t e t h e e x p r e s s i o n (a+b)>>1 , a s s i g n in g t h e r e s u lt t o a 1 6 - b i t

    r e g is t e r w i t h o u t l os i n g t h e c a r r y b i t . V a r i a b l e s a a n d b a r e b o t h 1 6 - b i t

    r e g i s t e r s .

    Th e s o l u t i o n :

    Ad d t h e integer ze r o t o t h e e x p r e s s i o n . T h e e x p r e s s i o n e va l u a t e s a s

    follows:

    1 . 0 + (a+b) e v a l u a t e s t h e r e s u l t i s a s w id e a s t h e w i d e s t t e r m ,w h i c h is t h e 3 2 - b i t ze r o

    2 . t h e 3 2 -b it s u m o f 0 + (a+b) i s s h i ft e d r i g h t o n e b i t

    Th i s t r i c k p r e s e r v e s t h e c a r r y b i t u n t i l t h e s h if t o p e r a t i o n c a n m o v e i t

    b a c k d o w n i n t o 1 6 b i t s .

    4 . 4 . 2Ve r i l o g R u l e s f o r E x p re s s i o n B i t Le n g t h s

    In t h e Ve r i l o g l a n g u a g e , t h e r u le s g o v e r n i n g t h e e x p r e s s i o n b i t l e n g t h s

    h a v e b e e n fo r m u l a t e d s o t h a t m o s t p r a c t i ca l s i t u a t i o n s h a v e a n a t u r a l

    so lu t ion .

    Th e n u m b e r o f b i t s o f a n e x p r e s s i o n (k n o wn a s t h e s i z e o f t h e e x p r e s s i o n )

    is d e t e r m i n e d b y t h e o p e r a n d s i n v o l v e d in t h e e x p r e s s i o n a n d t h e c o n t e x t

    in w h i c h t h e e x p r e s s i o n i s g iv e n .

  • 7/31/2019 Verilog Chap 4

    25/26

    J u n e 1 9 9 3 4 -2 5

    E x p r e s s i o n sExpression Bit Lengths

    A s e l f- d e t e r m i n e d e x p r e s s i o n i s o n e w h e r e t h e b i t l e n g t h o f t h e

    e x p r e s s i o n i s s o l e ly d e t e r m in e d b y t h e e x p r e s s i o n i t s e l f fo r e x a m p le , a n

    e x p r e s s i o n r e p r e s e n t i n g a d e l a y v a l u e .

    A c o n t e x t - d e t e r m in e d e x p r e s s i o n i s o n e w h e r e t h e b i t l e n g t h o f t h ee x p r e s s i o n i s d e t e r m i n e d b y t h e b i t l e n g t h o f t h e e x p r e s s i o n a n d b y t h e

    f a c t t h a t i t i s p a r t o f a n o t h e r e x p r e s s i o n . F o r e x a m p l e , t h e b i t s i ze o f t h e

    r i gh t - h a n d s i d e e x p r e s s i o n o f a n a s s i gn m e n t d e p e n d s o n i t s e lf a n d t h e

    s i z e o f t h e l e ft - h a n d s i d e .

  • 7/31/2019 Verilog Chap 4

    26/26

    E x p r e s s i o n sExpression Bit Lengths

    Ta b l e 4 - 1 4 s h o w s h o w t h e f o rm o f a n e x p r e s s i o n d e t e r m i n e s t h e b i t

    le n g t h s o f t h e r e s u lt s o f t h e e x p r e s s i o n . I n T a b l e 4 - 1 4 , i, j, a n d k

    r e p r e s e n t e x p r e s s io n s o f a n o p e r a n d , a n d L(i) r e p r e s e n t s t h e b i t l e n g t h

    o f t h e o p e r a n d r e p r e s e n t e d b y i.

    Table 4-14: Bit lengths resulting from expressions

    Expression Bit length Comments

    unsized same asconstant integer

    number (usually 32)

    sized as givenconstant

    number

    i op j max (L(i), L(j))

    where op is:+ - * / %

    & | ^ ^~

    +i and -i L(i)

    ~i L(i)

    i op j 1 bit all operands are

    where op is self-determined=== !== == != && ||> >= < > j L(i) j is self-determinedi