lecture 4 chap 5 types instructors: fu-chiung cheng ( 鄭福炯 ) associate professor computer...

30
Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭鄭鄭 ) Associate Professor Computer Science & Engineering Tatung University

Upload: ruth-allison

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Lecture 4 Chap 5 Types

Instructors: Fu-Chiung Cheng

(鄭福炯 )Associate Professor

Computer Science & EngineeringTatung University

Page 2: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Operator Precedence

Miscellaneous **, abs, notmultiplying *, /, mod, remsign +, -adding +, -shifting sll, srl, sla, sra, ror, rolrelational =, /=, <, <=, >, >=logical and, or, nand, nor, xor, xnor

Page 3: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Operator Precedence

• Logical operators are the lowest precedence.• Logical operators have the same precedence• It is impossible to mix logic operators. a <= b or c and d -- illegal

a <= (b or c) and d -- OK• nand and nor cannot be chained y <= a nand b nand c -- illegal

y <= (a nand b) nand c -- OKy <= a or b or c -- OK

A. nand and nor are non-associative operators B. non-inverting operators are associative

Page 4: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• Don’t mix Shift operatorsa sll 2 srl 2; -- illegal(a sll 2) srl 2; -- OK use parentheses(strip off the leftmost two bits of bus a)

• shift > logicalif a sll 2 > 5 then …if (a sll 2) > 5 then

Page 5: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Signed + and -

• a + -b; -- illegal.• -a + b; -- OK (-a)+b signed +,- > +,-• -a*b; -- OK -(a*b) signed +,- < *,/• a + b*c; --OK a+(b*c);• -a mod b; -- OK -(a mod b);

a = 3 b=4 -a mod 4 = -3(-a) mod 4 = 1

• a ** b ** c ==> a ** (b ** c)

Page 6: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Boolean operators

• Boolean expression are transferred to sum-of-prducts form.• Example: a nand b ==> not a or not b a nor b ==> not a and not b a xor b ==> (not a and b) or (a and not b) a xnor b ==> (a and b) or (not a and not b) (equal)

Page 7: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Comparison Operators

• equality test: = and /= xnor gates• ordering test: <, <=, >, >= subtractor• 4-bit equality: a(3)

b(3)

a(2)

b(2)

a(1)

b(1)

a(0)

b(0)

Eq

Page 8: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Comparison Operators

• 4-bit ordering tester a<b: use subtractor a-b a<b if msb=1 • optimization can be achieved by removed the unused subtractor outputs.• other ordering test conversion: A. a >b ==> b<a B. a >=b ==> not (a <b) C. a <=b ==> not (b<a) • a <0: check the msb bit.• a>=0: check the msb bit.

-4

45

A

B

msb

A<B

Page 9: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Array Comparison Operators

• Array equality: if a’length /= b’length then a /= b if a’length = b’length • Array inequality = not (array equality). a(3)

b(3)

a(2)

b(2)

a(1)

b(1)

a(0)

b(0)

Eq

==

==

Page 10: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Array Comparison Operators

• Array less-than: a(2 downto 0) < b(3 downto 0)

MU

X

=

<

A(2)

b(3)

<

MU

X

=

<

A(0)

b(1)

MU

X=

<

b(2)

A(1)

b(0)

1

Page 11: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• Shift left logical 4 bit (sll 4):

msb lsb 0 0 0 0

Page 12: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• Shift right logical 4 bit (srl 4):

msb lsb0 0 0 0

Page 13: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• Shift left arithemetic 4 bit (sla 4):

msb lsb

Page 14: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• Shift right 4 arithmetic bit (sra 4):

msb lsb

Page 15: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• rotate left 1 bit (rol 1):

msb lsb

Page 16: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Shift operators

• rotate right 1 bit (ror 1):

msb lsb

Page 17: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators:

• + addition• - subtraction• + plus sign• - minus sign• * multiplication• / division• mod modulo arithmetic• rem remainder after division• ** exponentiation• abs absolute value

Page 18: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: plus sign

• Implement: wire (no circuitry).

Page 19: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: minus sign

• Implement: 2’s-complementor A. 1’s complementB. + 1.

• Use ripple-borrow subtractors.• Half-Subtractor: dif <= x xor bi; bo <= x or bi;• Truth table:

x bi dif bo

0 0 0 0

1 0 1 1

0 1 1 1

1 1 0 1

Page 20: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: minus sign

• 4-bit 2’s-complementor: H/S

H/S

H/S

H/S

A(3)

A(2)

A(1)

A(0)

0 bi dif

x bo

x bo

x bo

x bo

bi dif

bi dif

bi dif

-A

Page 21: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: abs

• Implement: mux + two complementor.

MU

X

a

sign

abs(a)-

Page 22: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: adder

• Implement: ripple-carry adder.• Full-Adder: sum <= x xor y xor ci; co <= (x and y) or (x and ci)

or (y and ci);

F/AA(3)

A(2)

A(1)

A(0)

0

x co

ci sum

A+B

F/Ax co

ci sum

F/Ax co

ci sum

F/Ax co

ci sum

B(3)

B(2)

B(1)

B(0)

Page 23: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: subtracter

• Implement: ripple-borrow subtracter.• Full-subtracter: dif <= x xor y xor ci; bo <= (not x and y)

or (not x and bi) or (y and bi);

A(3)

A(2)

A(1)

A(0)

0

A-BB(3)

B(2)

B(1)

B(0)

F/S

F/S

F/S

F/Sbi dif

x bo

x bo

x bo

x bo

bi dif

bi dif

bi dif

Page 24: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: multiplier

• Implement: array multiplier• see pp. 94 and 95

Page 25: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: divisor

• Not fully implementation, support only divide by 2^k • Implement: shifter (unsigned division by 2)

lsb

msb 0

Page 26: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: divisor

• Not fully implementation, support only divide by 2^k • Implement: shifter + mux (signed division)

MU

X

sign

-

MU

X

sign

-unsigned

divisor

Page 27: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: modulo

• modulo is similar to division (not fully support).• Implement: wire (unsigned and unsigned modulo by 128)

msb

lsb

0

Page 28: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Arithmetic operators: remainder

• Implement: mux + modulo

MU

X

sign

-

MU

X

sign

-unsignedmodulo

Page 29: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Exponentiation operator:

• Implement: only x**2 is allowed.• X**2 = x*x

Page 30: Lecture 4 Chap 5 Types Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Concatenation operator:

• conatenation operator allow an array to be built up out of smaller arrays.• Implement: not circuitry required• Example: signal a,b : std_logic_vector(15 downto 0); signal z: std_logic_vector(31 downto 0); … z <= a & b; 16

16

32